question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disallow space before colon in SwitchStatement

See original GitHub issue

Hi guys! Today there is no way to disallow space before colon in switch-case statement:

switch (foo) {
  case 'bar' : 
    return '^---oh, that space...';
}

I already use keyword-spacing, but I can’t configure it for my case. I tried key-spacing too, but it still doesn’t work. My configuration looks like this:

    ...
    "key-spacing": "error",
    "keyword-spacing": "error",
    ...

You can also reproduce this by http://eslint.org/demo/ with enabled keyword-spacing & key-spacing.

I’ve asked about this in gitter and I was recommended to create an issue. I don’t know, should it be a new rule or modification of an existing?

Thank you!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
mysticateacommented, Feb 12, 2017

I’ll champion, too. I try to make a concrete proposal.

label-colon-spacing (or label-spacing)

This rule enforce usage of spaces before/after the colon of labels. The labels is both LabeledStatement and SwitchCase nodes’.

A_LABEL : while (true) foo();
//     ^ ^

switch (a) {
    case A : foo(); break;
    //    ^ ^
    case B :
    //    ^ (if there is a line terminator after the colon, this rule does nothing for spacing after the colon.)
    default : foo(); break;
    //     ^ ^
}

Options

{
    "label-colon-spacing": ["error", {"after": true, "before": false}]
}
  • after
    • true (default) requires spaces after the colon of labels.
    • false disallows spaces after the colon of labels.
  • before
    • true requires spaces before the colon of labels.
    • false (default) disallows spaces before the colon of labels.

Examples

👎 incorrect code for this rule with the default {"after": true, "before": false} option.

A_LABEL :foo();

switch (a) {
    case A :foo();
    default :foo();
}

👍 correct code for this rule with the default {"after": true, "before": false} option.

A_LABEL: foo();

switch (a) {
    case A: foo();
    default: foo();
}

👎 incorrect code for this rule with the {"after": false, "before": true} option.

A_LABEL: foo();

switch (a) {
    case A: foo();
    default: foo();
}

👍 correct code for this rule with the default {"after": false, "before": true} option.

A_LABEL :foo();

switch (a) {
    case A :foo();
    default :foo();
}
0reactions
kaicataldocommented, Apr 22, 2017

This has now been accepted! I support adding a rule for this, however it’s not entirely clear to me where we stand with the proposal. It looks like there were two votes for a rule that specifically checks switch cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

switch-colon-spacing - Pluggable JavaScript Linter - ESLint
This rule controls spacing around colons of case and default clauses in switch statements. This rule does the check only if the consecutive...
Read more >
Bug: conflict between space-before-blocks and switch-colon ...
For space-before-blocks to acknowledge the rule settings of switch-colon-spacing and behave accordingly. What actually happened? The two rules ...
Read more >
spaces not needed in case labels? - Stack Overflow
There needs to be whitespace separating the case word from it's value in order for the compiler to recognize the case . As...
Read more >
declaration-colon-space-before - Stylelint
Require a single space or disallow whitespace before the colon of declarations. The fix option can automatically fix all of the problems reported...
Read more >
Formatting - Coding Style - Read the Docs
No space before the colon of case . If the default case should never execute, simply assert . case blocks in switch statements...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found