noSwitchDeclarations (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v1.0.0 - Diagnostic Category:
lint/correctness/noSwitchDeclarations - This rule is recommended, meaning it is enabled by default.
- This rule has a safe fix.
- The default severity of this rule is error.
- Sources:
- Same as
no-case-declarations
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "correctness": { "noSwitchDeclarations": "error" } } }}Description
Section titled “Description”Disallow declarations directly inside switch clauses.
A let, const, function, or class declared in one clause is visible throughout the
entire switch, but it is initialized only when the clause containing it runs. Another
clause can therefore access the declaration before initialization and cause a runtime error.
Wrap each clause in a block to limit the declaration to that clause.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”switch (foo) { case 0: const x = 1; break; case 2: x; // `x` can be used while it is not initialized break;}code-block.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
ℹ Safe fix: Wrap the declaration in a block.
1 1 │ switch (foo) {
2 │ - ····case·0:
2 │ + ····case·0:·{
3 3 │ const x = 1;
4 │ - ········break;
4 │ + ········break;
5 │ + ····}
5 6 │ case 2:
6 7 │ x; // `x` can be used while it is not initialized
switch (foo) { case 0: function f() {} break; case 2: f(); // `f` can be called here break;}code-block.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
ℹ Safe fix: Wrap the declaration in a block.
1 1 │ switch (foo) {
2 │ - ····case·0:
2 │ + ····case·0:·{
3 3 │ function f() {}
4 │ - ········break;
4 │ + ········break;
5 │ + ····}
5 6 │ case 2:
6 7 │ f(); // `f` can be called here
switch (foo) { case 0: class A {} break; default: new A(); // `A` can be instantiated here break;}code-block.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
ℹ Safe fix: Wrap the declaration in a block.
1 1 │ switch (foo) {
2 │ - ····case·0:
2 │ + ····case·0:·{
3 3 │ class A {}
4 │ - ········break;
4 │ + ········break;
5 │ + ····}
5 6 │ default:
6 7 │ new A(); // `A` can be instantiated here
switch (foo) { case 0: { const x = 1; break; } case 1: // `x` is not visible here break;}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.