Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird indentation when using switch-case #15

Closed
mathiasbynens opened this issue Aug 2, 2010 · 11 comments
Closed

Weird indentation when using switch-case #15

mathiasbynens opened this issue Aug 2, 2010 · 11 comments

Comments

@mathiasbynens
Copy link
Contributor

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
Copy link
Contributor

einars commented Aug 2, 2010

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

@mathiasbynens
Copy link
Contributor Author

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
Copy link
Contributor

einars commented Aug 2, 2010

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
Copy link

k3n commented Oct 11, 2012

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
Copy link

k3n commented Oct 17, 2012

Awesome, thank you so much.

@drewhamlett
Copy link

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
Copy link
Member

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

@Frozenfire92
Copy link

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
Copy link
Contributor

einars commented Jul 23, 2014

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

@Frozenfire92
Copy link

Perfect, thank you :)

@robertleeplummerjr
Copy link

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

SimenHolmestad added a commit to SimenHolmestad/.emacs.d that referenced this issue Feb 20, 2020
There seems to be some disagreements on this issue. For example here:
beautifier/js-beautify#15. I think having
it this way seem nicer.
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants