Skip to content

eslint/no-fallthrough Pedantic ​

🚧 An auto-fix is still under development.

What it does ​

Disallow fallthrough of case statements

This rule is aimed at eliminating unintentional fallthrough of one case to the other. As such, it flags any fallthrough scenarios that are not marked by a comment.

Why is this bad? ​

The switch statement in JavaScript is one of the more error-prone constructs of the language thanks in part to the ability to “fall through” from one case to the next. For example:

js
switch (foo) {
  case 1:
    doSomething();

  case 2:
    doSomethingElse();
}

In this example, if foo is 1, then execution will flow through both cases, as the first falls through to the second. You can prevent this by using break, as in this example:

js
switch (foo) {
  case 1:
    doSomething();
    break;

  case 2:
    doSomethingElse();
}

That works fine when you don’t want a fallthrough, but what if the fallthrough is intentional, there is no way to indicate that in the language. It’s considered a best practice to always indicate when a fallthrough is intentional using a comment which matches the `/falls?\s?through/i`` regular expression but isn’t a directive:

js
switch (foo) {
  case 1:
    doSomething();
  // falls through

  case 2:
    doSomethingElse();
}

switch (foo) {
  case 1:
    doSomething();
  // fall through

  case 2:
    doSomethingElse();
}

switch (foo) {
  case 1:
    doSomething();
  // fallsthrough

  case 2:
    doSomethingElse();
}

switch (foo) {
  case 1: {
    doSomething();
    // falls through
  }

  case 2: {
    doSomethingElse();
  }
}

In this example, there is no confusion as to the expected behavior. It is clear that the first case is meant to fall through to the second case.

Example ​

Examples of incorrect code for this rule:

js
/*oxlint no-fallthrough: "error"*/

switch (foo) {
  case 1:
    doSomething();

  case 2:
    doSomething();
}

Examples of correct code for this rule:

js
/*oxlint no-fallthrough: "error"*/

switch (foo) {
  case 1:
    doSomething();
    break;

  case 2:
    doSomething();
}

function bar(foo) {
  switch (foo) {
    case 1:
      doSomething();
      return;

    case 2:
      doSomething();
  }
}

switch (foo) {
  case 1:
    doSomething();
    throw new Error("Boo!");

  case 2:
    doSomething();
}

switch (foo) {
  case 1:
  case 2:
    doSomething();
}

switch (foo) {
  case 1:
  case 2:
    doSomething();
}

switch (foo) {
  case 1:
    doSomething();
  // falls through

  case 2:
    doSomething();
}

switch (foo) {
  case 1: {
    doSomething();
    // falls through
  }

  case 2: {
    doSomethingElse();
  }
}

Note that the last case statement in these examples does not cause a warning because there is nothing to fall through into.

References ​

Released under the MIT License.