noUselessSwitchCase
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/complexity/noUselessSwitchCase¥Diagnostic Category:
lint/complexity/noUselessSwitchCase -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
unicorn/no-useless-switch-case相同¥Same as
unicorn/no-useless-switch-case
-
¥How to configure
{ "linter": { "rules": { "complexity": { "noUselessSwitchCase": "error" } } }}¥Description
禁止在 switch 语句中使用无用的 case。
¥Disallow useless case in switch statements.
switch 语句可以有可选的 default 子句。
¥A switch statement can optionally have a default clause.
仅当 case 子句中没有匹配项时,才会执行 default 子句。因此,位于 default 子句之前的空 case 子句是无用的。
¥The default clause will be still executed only if there is no match in the case clauses.
An empty case clause that precedes the default clause is thus useless.
¥Examples
¥Invalid
switch (foo) { case 0: default: break; case 1: break;}code-block.js:2:5 lint/complexity/noUselessSwitchCase FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Useless case clause.
1 │ switch (foo) {
> 2 │ case 0:
│ ^^^^^^^
3 │ default:
4 │ break;
ℹ because the default clause is present:
1 │ switch (foo) {
2 │ case 0:
> 3 │ default:
│ ^^^^^^^^
> 4 │ break;
│ ^^^^^^
5 │ case 1:
6 │ break;
ℹ Unsafe fix: Remove the useless case.
1 1 │ switch (foo) {
2 │ - ····case·0:
3 2 │ default:
4 3 │ break;
switch (foo) { default: case 0: break; case 1: break;}code-block.js:3:5 lint/complexity/noUselessSwitchCase FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Useless case clause.
1 │ switch (foo) {
2 │ default:
> 3 │ case 0:
│ ^^^^^^^
> 4 │ break;
│ ^^^^^^
5 │ case 1:
6 │ break;
ℹ because the default clause is present:
1 │ switch (foo) {
> 2 │ default:
│ ^^^^^^^^
3 │ case 0:
4 │ break;
ℹ Unsafe fix: Remove the useless case.
1 1 │ switch (foo) {
2 │ - ····default:
3 │ - ····case·0:
2 │ + ····default:
4 3 │ break;
5 4 │ case 1:
¥Valid
switch (foo) { case 0: break; default: break;}switch (foo) { case 0: break;}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号