no-fallthrough

禁止 case 语句的失败

Recommended

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

JavaScript 中的 switch 语句是该语言中更容易出错的结构之一,这部分归功于 “跌倒” 从一个 case 到另一个的能力。例如:

¥The switch statement in JavaScript is one of the more error-prone constructs of the language thanks in part to the ability to “fall through” from one case to the next. For example:

switch(foo) {
    case 1:
        doSomething();

    case 2:
        doSomethingElse();
}

在此示例中,如果 foo1,则执行将流经这两种情况,因为第一种情况会一直到第二种情况。你可以通过使用 break 来防止这种情况,如下例所示:

¥In this example, if foo is 1, then execution will flow through both cases, as the first falls through to the second. You can prevent this by using break, as in this example:

switch(foo) {
    case 1:
        doSomething();
        break;

    case 2:
        doSomethingElse();
}

当你不想要失败时,这很好用,但如果失败是故意的,则无法在语言中表明这一点。最好的做法是始终使用与 /falls?\s?through/i 正则表达式匹配但不是指令的注释来指示何时有意进行失败:

¥That works fine when you don’t want a fallthrough, but what if the fallthrough is intentional, there is no way to indicate that in the language. It’s considered a best practice to always indicate when a fallthrough is intentional using a comment which matches the /falls?\s?through/i regular expression but isn’t a directive:

switch(foo) {
    case 1:
        doSomething();
        // falls through

    case 2:
        doSomethingElse();
}

switch(foo) {
    case 1:
        doSomething();
        // fall through

    case 2:
        doSomethingElse();
}

switch(foo) {
    case 1:
        doSomething();
        // fallsthrough

    case 2:
        doSomethingElse();
}

switch(foo) {
    case 1: {
        doSomething();
        // falls through
    }

    case 2: {
        doSomethingElse();
    }
}

在此示例中,对于预期行为没有混淆。很明显,第一种情况是要落入第二种情况的。

¥In this example, there is no confusion as to the expected behavior. It is clear that the first case is meant to fall through to the second case.

规则详情

¥Rule Details

该规则旨在消除一个案例无意中的失败。因此,它会标记任何未由注释标记的失败场景。

¥This rule is aimed at eliminating unintentional fallthrough of one case to the other. As such, it flags any fallthrough scenarios that are not marked by a comment.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

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

switch(foo) {
    case 1:
        doSomething();

    case 2:
        doSomething();
}

此规则的正确代码示例:

¥Examples of correct code for this rule:

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

switch(foo) {
    case 1:
        doSomething();
        break;

    case 2:
        doSomething();
}

function bar(foo) {
    switch(foo) {
        case 1:
            doSomething();
            return;

        case 2:
            doSomething();
    }
}

switch(foo) {
    case 1:
        doSomething();
        throw new Error("Boo!");

    case 2:
        doSomething();
}

switch(foo) {
    case 1:
    case 2:
        doSomething();
}

switch(foo) {
    case 1: case 2:
        doSomething();
}

switch(foo) {
    case 1:
        doSomething();
        // falls through

    case 2:
        doSomething();
}

switch(foo) {
    case 1: {
        doSomething();
        // falls through
    }

    case 2: {
        doSomethingElse();
    }
}

请注意,这些示例中的最后一个 case 语句不会导致警告,因为没有任何内容可陷入。

¥Note that the last case statement in these examples does not cause a warning because there is nothing to fall through into.

选项

¥Options

此规则有一个对象选项:

¥This rule has an object option:

  • commentPattern 选项设置为正则表达式字符串以更改测试以测试有意的失败注释。如果 fallthrough 注释与指令匹配,则该指令优先于 commentPattern

    ¥Set the commentPattern option to a regular expression string to change the test for intentional fallthrough comment. If the fallthrough comment matches a directive, that takes precedence over commentPattern.

  • allowEmptyCase 选项设置为 true 以允许空箱,而不管布局如何。默认情况下,仅当空 case 和下一个 case 位于同一行或连续行时,此规则才不需要空 case 后的 fallthrough 注释。

    ¥Set the allowEmptyCase option to true to allow empty cases regardless of the layout. By default, this rule does not require a fallthrough comment after an empty case only if the empty case and the next case are on the same line or on consecutive lines.

  • reportUnusedFallthroughComment 选项设置为 true,以在案例因无法访问而无法失败时禁止出现失败注释。这主要是为了帮助避免由于重构而出现误导性注释。

    ¥Set the reportUnusedFallthroughComment option to true to prohibit a fallthrough comment from being present if the case cannot fallthrough due to being unreachable. This is mostly intended to help avoid misleading comments occurring as a result of refactoring.

commentPattern

{ "commentPattern": "break[\\s\\w]*omitted" } 选项的正确代码示例:

¥Examples of correct code for the { "commentPattern": "break[\\s\\w]*omitted" } option:

在线运行
/*eslint no-fallthrough: ["error", { "commentPattern": "break[\\s\\w]*omitted" }]*/

switch(foo) {
    case 1:
        doSomething();
        // break omitted

    case 2:
        doSomething();
}

switch(foo) {
    case 1:
        doSomething();
        // caution: break is omitted intentionally

    default:
        doSomething();
}

allowEmptyCase

{ "allowEmptyCase": true } 选项的正确代码示例:

¥Examples of correct code for the { "allowEmptyCase": true } option:

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

switch(foo){
    case 1:

    case 2: doSomething();
}

switch(foo){
    case 1:
    /*
    Put a message here 
    */
    case 2: doSomething();
}

reportUnusedFallthroughComment

{ "reportUnusedFallthroughComment": true } 选项的错误代码示例:

¥Examples of incorrect code for the { "reportUnusedFallthroughComment": true } option:

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

switch(foo){
    case 1:
        doSomething();
        break;
    // falls through
    case 2: doSomething();
}

function f() {
    switch(foo){
        case 1:
            if (a) {
                throw new Error();
            } else if (b) {
                break;
            } else {
                return;
            }
        // falls through
        case 2:
            break;
    }
}

{ "reportUnusedFallthroughComment": true } 选项的正确代码示例:

¥Examples of correct code for the { "reportUnusedFallthroughComment": true } option:

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

switch(foo){
    case 1:
        doSomething();
        break;
    // just a comment
    case 2: doSomething();
}

何时不使用

¥When Not To Use It

如果你不想强制每个 case 语句都应以 throwreturnbreak 或注释结尾,那么你可以安全地关闭此规则。

¥If you don’t want to enforce that each case statement should end with a throw, return, break, or comment, then you can safely turn this rule off.

版本

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

资源

ESLint 中文网
粤ICP备13048890号