Index

no-labels

禁止标记语句

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

JavaScript 中的带标签语句与 breakcontinue 一起使用,用于控制多个循环的流程。例如:

🌐 Labeled statements in JavaScript are used in conjunction with break and continue to control flow around multiple loops. For example:

outer:
    while (true) {

        while (true) {
            break outer;
        }
    }

break outer 语句确保这段代码不会导致无限循环,因为控制权会返回到应用了 outer 标签后的下一条语句。如果将此语句改为仅为 break,控制权将流回到外部的 while 语句,并导致无限循环。

🌐 The break outer statement ensures that this code will not result in an infinite loop because control is returned to the next statement after the outer label was applied. If this statement was changed to be just break, control would flow back to the outer while statement and an infinite loop would result.

虽然在某些情况下很方便,但标签往往很少使用,并且被一些人认为是一种更容易出错且更难理解的流量控制的补救形式。

🌐 While convenient in some cases, labels tend to be used only rarely and are frowned upon by some as a remedial form of flow control that is more error prone and harder to understand.

规则详情

🌐 Rule Details

此规则旨在消除 JavaScript 中带标签语句的使用。每当遇到带标签的语句,或者当 breakcontinue 与标签一起使用时,它都会发出警告。

🌐 This rule aims to eliminate the use of labeled statements in JavaScript. It will warn whenever a labeled statement is encountered and whenever break or continue are used with a label.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-labels: "error"*/

label:
    while(true) {
        // ...
    }

label:
    while(true) {
        break label;
    }

label:
    while(true) {
        continue label;
    }

label:
    switch (a) {
    case 0:
        break label;
    }

label:
    {
        break label;
    }

label:
    if (a) {
        break label;
    }

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint no-labels: "error"*/

const f = {
    label: "foo"
};

while (true) {
    break;
}

while (true) {
    continue;
}

选项

🌐 Options

这些选项允许带有循环或 switch 语句的标签:

🌐 The options allow labels with loop or switch statements:

  • "allowLoop"boolean,默认值是 false)- 如果此选项设置为 true,则此规则会忽略附着在循环语句上的标签。
  • "allowSwitch"boolean,默认值是 false)- 如果此选项设置为 true,此规则会忽略附着在 switch 语句上的标签。

实际上,JavaScript 中带有标签的语句可以用于除循环和 switch 语句之外的其他情况。然而,这种方式极为罕见,知之者不多,所以这会让开发者感到困惑。

🌐 Actually labeled statements in JavaScript can be used with other than loop and switch statements. However, this way is ultra rare, not well-known, so this would be confusing developers.

allowLoop

适用于 { "allowLoop": true } 选项的正确代码示例:

🌐 Examples of correct code for the { "allowLoop": true } option:

在线运行
/*eslint no-labels: ["error", { "allowLoop": true }]*/

label:
    while (true) {
        break label;
    }

allowSwitch

适用于 { "allowSwitch": true } 选项的正确代码示例:

🌐 Examples of correct code for the { "allowSwitch": true } option:

在线运行
/*eslint no-labels: ["error", { "allowSwitch": true }]*/

label:
    switch (a) {
        case 0:
            break label;
    }

何时不使用

🌐 When Not To Use It

如果你需要在任何地方使用带标签的语句,那么你可以安全地禁用此规则。

🌐 If you need to use labeled statements everywhere, then you can safely disable this rule.

版本

此规则是在 ESLint v0.4.0 中引入。

资源