noSwitchDeclarations
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/correctness/noSwitchDeclarations¥Diagnostic Category:
lint/correctness/noSwitchDeclarations -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
no-case-declarations相同¥Same as
no-case-declarations
-
¥How to configure
{ "linter": { "rules": { "correctness": { "noSwitchDeclarations": "error" } } }}¥Description
禁止在 switch 子句中使用词汇声明。
¥Disallow lexical declarations in switch clauses.
switch 子句中的词汇声明可在整个 switch 中访问。但是,它仅在分配时才会初始化,这只有在到达定义它的 switch 子句时才会发生。
¥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.
要确保词汇声明仅适用于当前 switch 子句,请将声明封装在块中。
¥To ensure that the lexical declarations only apply to the current switch clause wrap your declarations in a block.
¥Examples
¥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
¥Valid
switch (foo) { case 0: { const x = 1; break; } case 1: // `x` is not visible here break;}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号