Non-Case Label In A Switch Clause
ID |
javascript.switch_non_case_label |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
reliability, switch, typo |
Description
Reports a labelled statement written directly in a switch clause whose label nothing jumps to. A misspelt default is exactly this shape, and the language accepts it silently.
The label has to be unreferenced to be reported. A labelled loop inside a case is a legitimate, if rare, construct — outer: for (…) { break outer; } needs the label to escape the loop from within — so a label that some break or continue inside it names is left alone. That single condition separates the typo from the deliberate use: nobody writes a label with no jump on purpose.
Only a label directly in a case or default clause is judged, which is the position a mistyped clause keyword lands in. A label nested deeper — inside a block, a loop or a nested function within the case — cannot be a mistyped clause and is not reported.
Rationale
// Bad
switch (day) {
case 0:
weekend = true;
break;
defaut: // a label, not the default clause
weekday = true;
}
-
defaut:,dfault:andDefault:all parse as a label followed by a statement. The switch quietly has no default clause and the intended fallback is dead code. -
Nothing warns. The block still reads like a default, so the mistake survives review, and the symptom is a value that keeps its initial state for inputs nobody listed.
-
The same shape hides a stray label left behind by an edit, which is dead syntax either way.
Remediation
Write the clause keyword, or — when the label really was meant as one — reference it from the break or continue it exists for.
// Good
switch (day) {
case 0:
weekend = true;
break;
default:
weekday = true;
}
// Also good: a label that is actually a jump target
switch (day) {
case 0:
outer: for (const item of items) {
if (item === null) {
break outer;
}
}
break;
default:
break;
}