eslint/default-case Restriction
What it does
Enforces that all switch
statements include a default
case, unless explicitly marked with a configured comment.
Why is this bad?
Without a default
case, it is unclear whether the omission was intentional or an oversight. Adding a default
or a special comment makes the code more explicit and reduces mistakes.
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
.
Options
First option:
- Type:
object
- Properties:
commentPattern
:string
(default:/^no default$/i
) - A regex pattern used to detect comments that mark the absence of adefault
case as intentional.
Example configuration:
{
"default-case": ["error", { "commentPattern": "^skip\\sdefault" }]
}
Examples of incorrect code for this rule:
/* default-case: ["error"] */
switch (foo) {
case 1:
break;
}
Examples of correct code for this rule:
/* default-case: ["error"] */
switch (a) {
case 1:
break;
default:
break;
}
switch (a) {
case 1:
break;
// no default
}
commentPattern
Examples of incorrect code for this rule with the { "commentPattern": "^skip\\sdefault" }
option:
/* default-case: ["error", { "commentPattern": "^skip\\sdefault" }] */
switch (a) {
case 1:
break;
// no default
}
Examples of correct code for this rule with the { "commentPattern": "^skip\\sdefault" }
option:
/* default-case: ["error", { "commentPattern": "^skip\\sdefault" }] */
switch (a) {
case 1:
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"
}
}