no-case-declarations
What it does
Section titled “What it does”Reports a let, const, using, function or class declared directly in a case or default clause. A declaration inside a block in the clause (case 1: { let a; }) is not reported, and neither is var.
Why it matters
Section titled “Why it matters”A lexical declaration in a case clause is scoped to the whole switch block, not to the clause, so the other clauses can see the name and, when execution jumps over the declaration, reading it throws a ReferenceError. The code reads as if each clause had its own scope, and it does not.
To fix, put the body of the clause in braces: case 1: { let b = 1; break; }.
Example
Section titled “Example”...switch (a) { case 1: let b = 1; const c = 2; function d() {} class E {} var f = 1; break; default: { let g; } }...Configuration
Section titled “Configuration”js: lint: rules: no-case-declarations: warn # off (default), warn, errorEnabled by the presets: js/eslint-recommended, js/biome-recommended.
Suppress one line with sudsy-disable-next-line no-case-declarations in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| eslint | no-case-declarations |
full |