Weird indentation when using switch-case #15

Closed
mathiasbynens opened this Issue Aug 2, 2010 · 10 comments

Projects

None yet

6 participants

@mathiasbynens
switch (v) { case 1: break; case 2: break; case 3: break; default: break; }

…becomes…

switch (v) {
case 1:
    break;
case 2:
    break;
case 3:
    break;
default:
    break;
}

Maybe this is intentional, but I would expect the case to be indented by 1 level (based on the settings).

@einars

It's by design, as per Crockford conventions, http://javascript.crockford.com/code.html , so — not a bug.

@mathiasbynens

IMHO, that really doesn’t make sense. Crockford’s reasoning to omit the extra indents is the following: “Each case is aligned with the switch. This avoids over-indentation.” ORLY?

I see your point though. If it's by design, and you like it that way, just leave it the way it is!

@einars
switch (foo) {
    case something:
        break;
}
 ^^^^^^^ huge ugly empty space

...and the real code suddenly gets pushed 2 indents away, which usually is much more ugly than just treating switch/case as an unindented special case.

Obviously, that's all in the eye of the beholder.

@k3n

I disagree with Crockford, with his idea it's exceedingly more difficult to ascertain the actual ending of the switch statement. By his line of reasoning, why indent any control structures at all? They all yield an indention penalty.

I think this would be better, if one just so happened to work on an 800x600 resolution:

switch (v) {
    case 1:
    // code goes here
    break;

    default:
    // code goes here
    break;
}

Would love to have an option for this; either the "normal" way (like I have seen switch statements written my entire life), or the way I demonstrated above; anything but the default. Would you be open to a patch, if you don't personally agree or see the need for it?

@k3n

Awesome, thank you so much.

@drewhamlett

Can we have an option for this? Maybe just add it to jslint happy?

Hate the throw in sloppy whitespace for a switch statement.

@bitwiseman

@drewhjava Feel free to open a bug referencing this one requesting this option

@Frozenfire92

This issue doesn't seem solved?

I still get the ugly switch beautification. Was this not fixed?

e.g.

switch (v) {
case 1:
    break;
case 2:
    break;
case 3:
    break;
default:
    break;
}
@einars

Ehh. I added a "jslint-happy" checkbox to the jsbeautifier.org.

@Frozenfire92

Perfect, thank you :)

@Kitanotori Kitanotori referenced this issue in Glavin001/atom-beautify Jul 20, 2016
Open

Bug: jsx indentation with 'switch' statements #597

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment