Skip to content

no-case-declarations

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.

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; }.

Reported
...
switch (a) { case 1: let b = 1; const c = 2; function d() {} class E {} var f = 1; break; default: { let g; } }
...
js:
lint:
rules:
no-case-declarations: warn # off (default), warn, error

Enabled 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.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
eslint no-case-declarations full