版本

no-fallthrough

不允許 case 陳述式向下執行

建議

設定檔中使用 @eslint/jsrecommended 設定會啟用此規則

JavaScript 中的 switch 陳述式是該語言中較容易出錯的結構之一,部分原因是它可以從一個 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 的情況。因此,它會標記任何未以註解標記的向下執行情境。

此規則的不正確程式碼範例

在線上測試中開啟
/*eslint no-fallthrough: "error"*/

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

    case 2:
        doSomething();
}

此規則的正確程式碼範例

在線上測試中開啟
/*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 在同一行或連續行時,此規則才不需要在空的 case 後面加上向下執行註解。

  • reportUnusedFallthroughComment 選項設定為 true,以禁止存在無法向下執行(因為無法到達)的 case 的向下執行註解。這主要是為了避免因重構而出現誤導性註解。

commentPattern

{ "commentPattern": "break[\\s\\w]*omitted" } 選項的正確程式碼範例

在線上測試中開啟
/*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 } 選項的正確程式碼範例

在線上測試中開啟
/* 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 } 選項的不正確程式碼範例

在線上測試中開啟
/* 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 } 選項的正確程式碼範例

在線上測試中開啟
/* eslint no-fallthrough: ["error", { "reportUnusedFallthroughComment": true }] */

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

何時不該使用

如果您不想強制每個 case 陳述式都以 throwreturnbreak 或註解結尾,則可以安全地關閉此規則。

版本

此規則在 ESLint v0.0.7 中引入。

資源

變更語言