no-unused-labels
禁止未使用的标签
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
此规则报告的一些问题可通过 --fix
命令行选项自动修复
由于重构不完整,已声明但未在代码中任何地方使用的标签很可能是错误。
¥Labels that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring.
OUTER_LOOP:
for (const student of students) {
if (checkScores(student.scores)) {
continue;
}
doSomething(student);
}
在这种情况下,可能已经忘记了删除 OUTER_LOOP:
。这样的标签会占用代码中的空间,并可能导致读者混淆。
¥In this case, probably removing OUTER_LOOP:
had been forgotten.
Such labels take up space in the code and can lead to confusion by readers.
规则详情
¥Rule Details
该规则旨在消除未使用的标签。
¥This rule is aimed at eliminating unused labels.
此规则报告的问题可以自动修复,除非标签和后续语句之间有任何注释,或者删除标签会导致后续语句成为指令(例如 "use strict"
)。
¥Problems reported by this rule can be fixed automatically, except when there are any comments between the label and the following statement, or when removing a label would cause the following statement to become a directive such as "use strict"
.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-unused-labels: "error"*/
A: var foo = 0;
B: {
foo();
}
C:
for (let i = 0; i < 10; ++i) {
foo();
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-unused-labels: "error"*/
A: {
if (foo()) {
break A;
}
bar();
}
B:
for (let i = 0; i < 10; ++i) {
if (foo()) {
break B;
}
bar();
}
何时不使用
¥When Not To Use It
如果你不想收到有关未使用标签的通知,那么禁用此规则是安全的。
¥If you don’t want to be notified about unused labels, then it’s safe to disable this rule.
相关规则
版本
此规则是在 ESLint v2.0.0-rc.0 中引入。