Js-beautify: Weird indentation when using switch-case

Created on 2 Aug 2010  Â·  11Comments  Â·  Source: beautify-web/js-beautify

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).

Most helpful comment

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!

All 11 comments

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

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!

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.

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?

Awesome, thank you so much.

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

Hate the throw in sloppy whitespace for a switch statement.

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

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;
}

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

Perfect, thank you :)

This indentation issue is different with the same code on windows vs OSX. Case in point is https://github.com/gpujs/gpu.js, when I run yarn make it beautifies the ./src folder, using this script: https://github.com/gpujs/gpu.js/blob/012cfff566691a8fa360e0af650a45c616ceff4a/gulpfile.js#L85

Was this page helpful?
0 / 5 - 0 ratings