Proposal: Require an empty line between "switch-case" statements (newline-between-switch-case)
See original GitHub issueProposal:
Require an empty line between switch-break
statements (newline-between-switch-break) #6619
(newline-between-switch-break
)
Options
This rule has an object option:
"spaceAfterBreak": true
(default)."spaceBeforeBreak": false
Relative
- The curlies should be covered by
padded-blocks
.
switch (value) {
case 100000000000000:
break;
default:
break;
}
- Comments should be covered by
lines-around-comment
:
For consistency reasons comments are ignored and do not count as empty lines.
switch (value) {
case 100000000000000:
break;
// comment
default:
break;
}
Examples
/*eslint newline-between-switch-break: "error"*/
or
/*eslint newline-between-switch-break: [ "error", {"spaceAfterBreak": true }]*/
Example of correct code for this rule:
switch (value) {
case 100000000000000:
break;
case 200000000000000:
break;
default:
break;
}
Example of incorrect code for this rule:
switch (value) {
case 100000000000000:
value
break;
case 200000000000000:
break;
default:
break;
}
/*eslint newline-between-switch-break: ["error", { "spaceBeforeBreak": true }]*/
switch (value) {
case 100000000000000:
value += 100;
break;
}
Example of incorrect code for this rule:
switch (value) {
case 100000000000000:
value += 100;
break;
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:21 (13 by maintainers)
Top Results From Across the Web
eslint: New-line after case statement inside switch
I want to add an eslint rule, which forces a new line after each case-statement inside a switch- ...
Read more >padding-line-between-statements - Pluggable JavaScript Linter
This rule requires or disallows blank lines between the given 2 kinds of statements. Properly blank lines ... "case" is case clauses in...
Read more >Selection statements - if , else and switch - Microsoft Learn
The if , else and switch statements select statements to execute from many possible paths based on the value of an expression.
Read more >"switch case" clauses should not have too many lines of code
The switch statement should be used only to clearly define some new branches in the control flow. As soon as a case clause...
Read more >Switch statement - Wikipedia
NET, Java, and in many other types of language, using such keywords as switch , case , select or inspect . Switch statements...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Nice examples! That helps clarify the problem. I’m envisioning the
newline-before-return
equivalent forbreak
andcontinue
as being almost identical to thereturn
version, specifically in that it would exclude any statement that is alone in its block.What about this?
"newline-before-flow-control": ["error", "always/never"]
: require an empty line beforebreak
orcontinue
statements"newline-between-switch-case": ["error", "always/never", { "fallthrough": "never"}]
: require an empty line between thecase
s (anddefault
), if present) of aswitch
statement. The object argument for fallthrough would only be applicable if the rule were set to"always"
.Is that code formatted more like what you want?
I wanted this rule (newline-between-switch-case), but didn’t want to wait for this to be accepted, so have implemented in a plugin - https://github.com/lukeapage/eslint-plugin-switch-case Please feel free to use the code as the basis for putting this rule in core (might need a bit more tidying/documentation/code formatting)