Eslint
规则 | Rules

no-unused-labels

Disallow Unused Labels (no-unused-labels)

"extends": "eslint:recommended"配置文件中的属性启用此规则。

--fix命令行上的选项可以自动修复一些被这条规则反映的问题。

在代码中任何地方声明和未使用的标签很可能是由于重构不完全导致的错误。

OUTER_LOOP: for (const student of students) { if (checkScores(student.scores)) { continue; } doSomething(student }

在这种情况下,可能OUTER_LOOP:已经被删除了。这些标签在代码中占用空间并可能导致读者混淆。

规则细节

此规则旨在消除未使用的标签。

此规则的错误代码示例:

/*eslint no-unused-labels: "error"*/ A: var foo = 0; B: { foo( } C: for (let i = 0; i < 10; ++i) { foo( }

此规则的正确代码示例:

/*eslint no-unused-labels: "error"*/ A: { if (foo()) { break A; } bar( } B: for (let i = 0; i < 10; ++i) { if (foo()) { break B; } bar( }

何时不使用它

如果您不想收到关于未使用的标签的通知,那么禁用此规则是安全的。

相关规则

  • 没有多余的标签

  • 无标签

  • 无标签,无功

版本

该规则在 ESLint 2.0.0-rc.0中引入。

资源

https://eslint.org/docs/rules/no-unused-labels