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.

Request: add option for no-fallthrough with newlines

See original GitHub issue

Environment

Node version: v16.14.0 npm version: v8.3.1 Local ESLint version: v8.11.0 (Currently used) Global ESLint version: v7.23.0 Operating System: darwin 21.1.0

What parser are you using?

Default (Espree)

What did you do?

var x = 3;
switch(x){
  case 0:
  case 1: break;

  case 2:
    
  case 3:
    break;
}

ESLint Demo Link

What did you expect to happen?

No errors

What actually happened?

  8:3  error  Expected a 'break' statement before 'case'  no-fallthrough

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

It might also make sense to support comments:

switch(x) {
  // currently accepted
  case 0:
  case 1:
    break;

  // this issue
  case 2:

  case 3:
    break;

  // general proposal
  /* a note about 4 */
  case 4:
  /* a note about 5 */
  case 5:
    break;
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:21 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
reviewhercommented, Mar 18, 2022

Coming from other programming languages, ESLint’s current definition of fallthrough is inconsistent. @SheetJSDev talked about C/C++. Other languages:

PHP: PSR2 guidelines define fallthrough as requiring at least one statement. This is the same as C/C++

Java: Sun style defines a fallthrough as “doesn’t include a break statement”, so technically you always need a fallthrough comment between cases, no matter how much whitespace is in between.

Go: forced breaks at the end of each case clause, even if there is no whitespace. Special keyword fallthrough to fall through.

Perl: given / when statements do not fall through. Special keyword continue to fall through.

C#: compilation error if there is no break between case clauses, irrespective of the number of lines. Special statement goto case ____ to fall through

Ruby: case / when statements do not fall through

Rust: match does not have fallthrough support.

Pascal: case statement has no fallthrough

In summary, “everything but eslint” defines fallthrough in one of two ways:

“intrinsic”: each case is separate. case a: case b: are seen as two distinct parts irrespective of the amount of whitespace and newlines between them.

“statement”: at least one statement between case clauses with no final break.

ESLint’s behavior is surprising and feels inconsistent.

2reactions
reviewhercommented, Mar 23, 2022

I genuinely tried to find an example outside of ESLint and came up short. Even in JS land:

JSHint takes the “statement” interpretation (you can verify on https://jshint.com/ )

JSLint takes the “statement” interpretation (you can verify on https://www.jslint.com/ )

ESLint “blazing a new trail” is not inherently bad, but there arguably should be a very compelling reason.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-fallthrough - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
sed: read whole file into pattern space without failing on single ...
Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more...
Read more >
Warning Options (Using the GNU Compiler Collection (GCC))
When an unrecognized warning option is requested (e.g., -Wunknown-warning ), GCC emits a diagnostic stating that the option is not recognized.
Read more >
Appending newlines characters (i.e. \r\n) to a form field sent in ...
So adding new line characters to the value of the text input element is basically superfluous. Alternative solutions might include: adding a ...
Read more >
5 switch statement patterns · YourBasic Go
5 switch statement patterns. yourbasic.org/golang. Basic switch with default; No condition; Case list; Fallthrough; Exit with break; Execution order ...
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