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:
switch (foo) {
case 1:
break;
}
Examples of correct code for this rule:
switch (a) {
case 1:
/* code */
break;
default:
/* code */
break;
}
switch (a) {
case 1:
/* code */
break;
// no default
}
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:
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:
oxlint --deny default-case
{
"rules": {
"default-case": "error"
}
}