Index

no-unused-labels

禁止未使用的标签

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

🔧 Fixable

此规则报告的一些问题可通过 --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();
}

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

何时不使用

🌐 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 中引入。

资源