noSwitchDeclarations (since v12.0.0)
This rule is recommended by Rome.
Disallow lexical declarations in switch
clauses.
Lexical declarations in switch
clauses are accessible in the entire switch
. However, it only gets initialized when it is assigned, which will only happen if the switch
clause where it is defined is reached.
To ensure that the lexical declarations only apply to the current switch
clause wrap your declarations in a block.
Source: https://eslint.org/docs/latest/rules/no-case-declarations
Examples
Invalid
switch (foo) {
case 0:
const x = 1;
break;
case 2:
x; // `x` can be used while it is not initialized
break;
}
correctness/noSwitchDeclarations.js:3:9 lint/correctness/noSwitchDeclarations FIXABLE ━━━━━━━━━━━━
✖ Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
1 │ switch (foo) {
2 │ case 0:
> 3 │ const x = 1;
│ ^^^^^^^^^^^
4 │ break;
5 │ case 2:
ℹ The declaration is defined in this switch clause:
1 │ switch (foo) {
> 2 │ case 0:
│ ^^^^^^^
> 3 │ const x = 1;
> 4 │ break;
│ ^^^^^^
5 │ case 2:
6 │ x; // `x` can be used while it is not initialized
ℹ Suggested fix: Wrap the declaration in a block.
1 1 │ switch (foo) {
2 │ - ····case·0:
2 │ + ····case·0:·{
3 3 │ const x = 1;
4 4 │ break;
5 │ - ····case·2:
5 │ + ····}
6 │ + ····case·2:
6 7 │ x; // `x` can be used while it is not initialized
7 8 │ break;
switch (foo) {
case 0:
function f() {}
break;
case 2:
f(); // `f` can be called here
break;
}
correctness/noSwitchDeclarations.js:3:9 lint/correctness/noSwitchDeclarations FIXABLE ━━━━━━━━━━━━
✖ Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
1 │ switch (foo) {
2 │ case 0:
> 3 │ function f() {}
│ ^^^^^^^^^^^^^^^
4 │ break;
5 │ case 2:
ℹ The declaration is defined in this switch clause:
1 │ switch (foo) {
> 2 │ case 0:
│ ^^^^^^^
> 3 │ function f() {}
> 4 │ break;
│ ^^^^^^
5 │ case 2:
6 │ f(); // `f` can be called here
ℹ Suggested fix: Wrap the declaration in a block.
1 1 │ switch (foo) {
2 │ - ····case·0:
2 │ + ····case·0:·{
3 3 │ function f() {}
4 4 │ break;
5 │ - ····case·2:
5 │ + ····}
6 │ + ····case·2:
6 7 │ f(); // `f` can be called here
7 8 │ break;
switch (foo) {
case 0:
class A {}
break;
default:
new A(); // `A` can be instantiated here
break;
}
correctness/noSwitchDeclarations.js:3:9 lint/correctness/noSwitchDeclarations FIXABLE ━━━━━━━━━━━━
✖ Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
1 │ switch (foo) {
2 │ case 0:
> 3 │ class A {}
│ ^^^^^^^^^^
4 │ break;
5 │ default:
ℹ The declaration is defined in this switch clause:
1 │ switch (foo) {
> 2 │ case 0:
│ ^^^^^^^
> 3 │ class A {}
> 4 │ break;
│ ^^^^^^
5 │ default:
6 │ new A(); // `A` can be instantiated here
ℹ Suggested fix: Wrap the declaration in a block.
1 1 │ switch (foo) {
2 │ - ····case·0:
2 │ + ····case·0:·{
3 3 │ class A {}
4 4 │ break;
5 │ - ····default:
5 │ + ····}
6 │ + ····default:
6 7 │ new A(); // `A` can be instantiated here
7 8 │ break;
Valid
switch (foo) {
case 0: {
const x = 1;
break;
}
case 1:
// `x` is not visible here
break;
}