ESLint reports a let, const or function inside a switch case
Unexpected lexical declaration in case block.Short answer
no-case-declarations fires when let, const, function or class is declared directly inside a case clause. The name is visible in the whole switch but is only set when that case runs. Wrap the case body in braces, or move the declaration out of the switch. Do not turn the rule off; it is part of the recommended set.
Fix it fast
1Find out which cause you have
Run ESLint on the file and note which case the declaration sits in. The rule name in the output is no-case-declarations.
npx eslint path/to/reducer.js2Match what you see, then jump to its fix
What does the error look like?
Unexpected lexical declaration in case block.
Cause 1 of 2
let or const declared directly in a case clause
1Check
The rule docs explain that the declaration is visible in the entire switch block but only gets initialised when the case that declares it is reached.
grep -n -A2 "case " path/to/reducer.js2Fix
Wrap the clause in braces so each declaration has its own block.
switch (action.type) {
case 'add': {
const item = action.payload;
return [...state, item];
}
case 'clear': {
return [];
}
}3Verify
Run ESLint again; the report is gone.
npx eslint path/to/reducer.jsConfirmed by eslint.org
Cause 2 of 2
A function or class is declared inside a case or default clause
1Check
The rule's source treats function declarations, class declarations and let or const declarations as lexical; var is not flagged.
grep -n -B1 -A3 "default:" path/to/reducer.js2Fix
Put the declaration inside a braced block in that clause, or move the helper above the switch so every case can use it.
function toLabel(x) { return String(x); }
switch (kind) {
default: {
return toLabel(value);
}
}3Verify
Run ESLint on the file.
npx eslint path/to/reducer.jsConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Write every case body that declares a variable as a braced block from the start.
- Keep switch cases short; move big case bodies into named functions.
- Keep the rule on: the ESLint docs mark no-case-declarations as part of the recommended config.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix