no-labels

禁止标记语句

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"*/

var f = {
    label: "foo"
};

while (true) {
    break;
}

while (true) {
    continue;
}

选项

¥Options

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

¥The options allow labels with loop or switch statements:

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

    ¥"allowLoop" (boolean, default is false) - If this option was set true, this rule ignores labels which are sticking to loop statements.

  • "allowSwitch"boolean,默认为 false) - 如果此选项设置为 true,则此规则将忽略附加到 switch 语句的标签。

    ¥"allowSwitch" (boolean, default is false) - If this option was set true, this rule ignores labels which are sticking to switch statements.

实际上,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 中引入。

资源

ESLint 中文网
粤ICP备13048890号