Skip to content

eslint/default-case-last Style

What it does

Requires the default clause in switch statements to be the last one.

Why is this bad?

By convention and for readability, the default clause should be the last one in a switch. While it is legal to place it before or between case clauses, doing so is confusing and may lead to unexpected "fall-through" behavior.

Options

No options available for this rule

Examples

Examples of incorrect code for this rule:

js
/* default-case-last: "error" */

switch (foo) {
  default:
    bar();
    break;
  case "a":
    baz();
    break;
}

switch (foo) {
  case 1:
    bar();
    break;
  default:
    baz();
    break;
  case 2:
    qux();
    break;
}

Examples of correct code for this rule:

js
/* default-case-last: "error" */

switch (foo) {
  case 1:
    bar();
    break;
  case 2:
    qux();
    break;
  default:
    baz();
    break;
}

switch (foo) {
  case "x":
    bar();
    break;
  case "y":
  default:
    baz();
    break;
}

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny default-case-last
json
{
  "rules": {
    "default-case-last": "error"
  }
}

References

Released under the MIT License.