no-extra-label
禁止不必要的标签
此规则报告的一些问题可通过 --fix
命令行选项自动修复
如果循环不包含嵌套循环或开关,则不需要标记循环。
¥If a loop contains no nested loops or switches, labeling the loop is unnecessary.
A: while (a) {
break A;
}
你可以通过移除标签并使用不带标签的 break
或 continue
来获得相同的结果。这些标签可能会让开发者感到困惑,因为他们希望标签能跳得更远。
¥You can achieve the same result by removing the label and using break
or continue
without a label.
Probably those labels would confuse developers because they expect labels to jump to further.
规则详情
¥Rule Details
该规则旨在消除不必要的标签。
¥This rule is aimed at eliminating unnecessary labels.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-extra-label: "error"*/
A: while (a) {
break A;
}
B: for (let i = 0; i < 10; ++i) {
break B;
}
C: switch (a) {
case 0:
break C;
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-extra-label: "error"*/
while (a) {
break;
}
for (let i = 0; i < 10; ++i) {
break;
}
switch (a) {
case 0:
break;
}
A: {
break A;
}
B: while (a) {
while (b) {
break B;
}
}
C: switch (a) {
case 0:
while (b) {
break C;
}
break;
}
何时不使用
¥When Not To Use It
如果你不想收到有关标签使用情况的通知,那么禁用此规则是安全的。
¥If you don’t want to be notified about usage of labels, then it’s safe to disable this rule.
相关规则
版本
此规则是在 ESLint v2.0.0-rc.0 中引入。