版本

no-fallthrough

禁止 case 语句的穿透

推荐

使用 recommended 配置从 @eslint/js配置文件 中启用此规则

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

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

    case 2:
        doSomethingElse();
}

在这个例子中,如果 foo1,那么执行流程会穿透两个 case,因为第一个 case 会穿透到第二个 case。你可以使用 break 来防止这种情况,如下例所示

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

    case 2:
        doSomethingElse();
}

当你不希望穿透时,这很好用,但是如果穿透是故意的呢?在语言中没有办法表明这一点。最佳实践是始终使用与 /falls?\s?through/i 正则表达式匹配但不是指令的注释来指示何时穿透是故意的

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();
    }
}

在这个例子中,对预期的行为没有混淆。很明显,第一个 case 打算穿透到第二个 case。

规则详情

此规则旨在消除从一个 case 到另一个 case 的非意图穿透。因此,它会标记任何未用注释标记的穿透场景。

此规则的 错误 代码示例

在 Playground 中打开
/*eslint no-fallthrough: "error"*/

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

    case 2:
        doSomething();
}

此规则的 正确 代码示例

在 Playground 中打开
/*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 语句不会产生警告,因为没有东西可以穿透到其中。

选项

此规则有一个对象选项

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

  • allowEmptyCase 选项设置为 true,以允许无论布局如何的空 case。默认情况下,仅当空 case 和下一个 case 在同一行或连续行上时,此规则才不需要穿透注释。

  • reportUnusedFallthroughComment 选项设置为 true,以禁止在 case 由于不可达而无法穿透时出现穿透注释。这主要是为了帮助避免由于重构而产生的误导性注释。

commentPattern

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

在 Playground 中打开
/*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 } 选项的 正确 代码示例

在 Playground 中打开
/* 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 } 选项的 错误 代码示例

在 Playground 中打开
/* 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 } 选项的 正确 代码示例

在 Playground 中打开
/* eslint no-fallthrough: ["error", { "reportUnusedFallthroughComment": true }] */

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

何时不使用它

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

版本

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

资源

更改语言