Skip to content

eslint/default-case Restriction ​

What it does ​

Require default cases in switch statements

Why is this bad? ​

Some code conventions require that all switch statements have a default case, even if the default case is empty. The thinking is that it’s better to always explicitly state what the default behavior should be so that it’s clear whether or not the developer forgot to include the default behavior by mistake.

You may optionally include a // no default after the last case if there is no default case. The comment may be in any desired case, such as // No Default.

Examples ​

Examples of incorrect code for this rule:

javascript
switch (foo) {
  case 1:
    break;
}

Examples of correct code for this rule:

javascript
switch (a) {
  case 1:
    /* code */
    break;

  default:
    /* code */
    break;
}
javascript
switch (a) {
  case 1:
    /* code */
    break;

  // no default
}
javascript
switch (a) {
  case 1:
    /* code */
    break;

  // No Default
}

Options ​

commentPattern ​

{ "commentPattern": string }

This option is for specifying an alternative regular expression which will override the default /^no default$/i comment test pattern.

For example if { "commentPattern": "^skip\\sdefault" } were used then the following example would not violate the rule:

javascript
switch (a) {
  case 1:
    /* code */
    break;

  // skip default
}

How to use ​

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

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

References ​

Released under the MIT License.