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.

Rule Proposal: no-unscoped-case

See original GitHub issue

Please describe what the rule should do:

This rule enforces using curly braces in switch case clauses to scope them. It pairs well with the no-case-declarations rule as that rule will not warn when case clauses are wrapped in lexical blocks.

This rule will specifically enforce a style that allows use of let/const statements inside case statements — even with the no-case-declarations rule enabled — which allows easier, iterative, refactoring and potentially less mutation of data.

What category of rule is this? (place an “X” next to just one item)

[X] Enforces code style [ ] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

// this will warn
switch (foo) {
    case 1:
        // code here
        break;
}

// this will not
switch (foo) {
    case 1: {
       // code here
        break;
    }
}

Why should this rule be included in ESLint (instead of a plugin)?

It’s highly complementary to another - existing - rule as well as being a fundamental code style rule for a highly used basic language construct. Given the other rule I was actually surprised that it didn’t exist already.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
alxndrcommented, Mar 17, 2017

I wonder if this would be possible to implement using the AST Selectors introduced in v3.18.0.


edit: Yup, pretty straightforward with "no-restricted-syntax". This selector should match case clauses without blocks: "SwitchCase > :not(BlockStatement)"

Might be a little more useful to look for assignment within a case clause which doesn’t use a block: "SwitchCase > ExpressionStatement > AssignmentExpression"

2reactions
mysticateacommented, Oct 7, 2016

This is a rough draft:

switch-case-curly

This rule enforces whether you should write braces on case:/default: clauses. This rule has a string option.

  • "always" (default) requires braces.
  • "never" disallows braces.

If "never" is given, you can specify 2nd object option (e.g. ["never", {"allowDeclarations": true}]):

  • allowDeclarations allows braces if the case:/default: clause has declarations to avoid confliction with no-case-declarations.

Examples:

always

/*eslint switch-case-curly: error */

// ✔ GOOD
switch (a) {
    case 1: {
        doSomething();
        break;
    }
    case 2:
    {
        doSomething();
        break;
    }
    default: {
        doSomething();
        break;
    }
}

// ✘ BAD
switch (a) {
    case 1:
        doSomething();
        break;

    default:
        doSomething();
        break;
}

never

/*eslint switch-case-curly: [error, never] */

// ✔ GOOD
switch (a) {
    case 1:
        doSomething();
        break;

    default:
        doSomething();
        break;
}

// ✘ BAD
switch (a) {
    case 1: {
        doSomething();
        break;
    }
    default: {
        doSomething();
        break;
    }
}

never and allowDeclarations

/*eslint switch-case-curly: [error, never, {allowDeclarations: true}] */

// ✔ GOOD
switch (a) {
    case 1:
        doSomething();
        break;

    case 2:
        const b = 1;
        doSomething(b);
        break;

    case 3: {
        const c = 1;
        doSomething(c);
        break;
    }

    default:
        doSomething();
        break;
}

// ✘ BAD
switch (a) {
    case 1: {
        doSomething();
        break;
    }
    default: {
        doSomething();
        break;
    }
}

Hmm,

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proposed rule: Order Competition Rule - SEC.gov
proposed rule would prohibit a restricted competition trading center from internally executing certain orders of individual investors at a ...
Read more >
no-lone-blocks doesn't catch lone blocks in SwitchCase ...
I first create this issue with the bug-report.md template but changed it to the rule-change-proposal.md following @platinumazure's advice ...
Read more >
SEC Reopening Comment Period on Several Key Proposed ...
The SEC on Oct. 7, 2022, reopened the comment period for 11 rule making proposals, one request for comment and eight Self-Regulatory ...
Read more >
SEC Proposes Amendments to the Shareholder Proposal Rules
The SEC proposed modifying certain bases for excluding shareholder proposals from company proxy statements. If adopted, these amendments are ...
Read more >
Better Together: Examining the Unified Proposed Rule to ...
Better Together: Examining the Unified Proposed Rule to Modernize the Community Reinvestment Act · H.R. 2768, the “American Housing and Economic Mobility Act.”...
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