no-fallthrough

不允许 case 语句的失败

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

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

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

    case 2:
        doSomethingElse();
}

在此示例中,如果 foo1,则执行将流经这两种情况,因为第一种情况会一直到第二种情况。您可以通过使用 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();
    }
}

AJvtDY8mDSLp/uYx64a3jdPbaNYo2J98JSLyWb5HeqUFqIcXZpZPt8Z0irq8bncJAnE45gn1xONtlmb3Ts58OJx2FHOFZDkIP1IPmbxl7JOf6aKnCLgA9vTMa0fkpGG0pfeNle/nX1LyIBAqt1GSxw==

规则详情

/MoB2P2oRwATHN283h8/7RKmBkVz5ZUHsbMhvGRpgGph1468Rofh1rhXjGgl5e3FYegzyYn+xrudZX2IjVzbuB7gH7q9msog18yTeU2twi5J3rmTA5ZQ02uPT3mMa9QPzHHGxP2csXTqN0lVdJlpWYDrrj8ySeiIKfoKvje1S+c=

Wk+Mu7oFIccqpNSe5orR05ZG/BDV40hHIKzF0SoJnVcuS8r6evQjx3oyoFn9+fZT

/*eslint no-fallthrough: "error"*/

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

    case 2:
        doSomething();
}

UubGaThNGVSAZCEeqgdAAx/QE1uwW/De5ytjPBnKCKqeOsDZpJS0MvQSJQtY0WPI

/*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:
        doSomething();
        // falls through

    case 2:
        doSomething();
}

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

    case 2: {
        doSomethingElse();
    }
}

wWXdTqrueI2r0+x/NP+QkUwcXs19b14rzZIW3XBJP6XljG5aVtXLqQIbnNd40ihyXmzuajvZxqTJOYhwwDTrJLaAV10lSAv/CjHd0OEf4xeqrQBV7XupINZlXCT9Iz5hpXEa+H8CpcEULspfmbpDcelQOSTMQ49P30vu3CSjk10=

选项

WhtdTe+SyJ0WUNUgKbqftH14r+kYEYAMLB+y3h0fvE5TyeVcrDm7eioz/kqA+xeS

    czvXcauP1YPdU1nLs6g6D0xwg+eMQ3TmTlM6aMoXZDCMYHgDrM0VqcdXaSQkpCIUD0680+6/5ErKM2pmKRtgBhEzpSJRZuC2ZnolAa/rtYu/S2jsCH7qOPQSuHmAvtryky/1cMzOZ2QX+HlOzzEdgJN42Y1k6Bc3AFpq7TFq5/UAenapdiaZHvlZpGMMyyIhB2al3Y/P2zOaBuWLyWQvmYB/0Bcmwqs3ijeR65vmRKVwxRn1EBiYUXBe/u5nLyofHYcPuY2fWpiMJ/hU+gg7d8rXA7nP5oAkR59pFQhnWh39iSfFFz3Xb6O+Wahv+qW1Ke93n0BwnRHk7MbR2CBqJuuCqtekZWUp99eQuK7KJdRCJRzCy1W1ZLhJZ2t0Ye2FIiMAVeKw5qyPgaI+65P+ottFi9NcaZ4uKcD32KETQgUc/22N07Ww28wrtg8E1Nqyz/xnoWa6SI61YuyrbNGRSoyCNqYPoOeo0FY0Ug7Rw5Bx0VmVrbff7MZPKvxIhJW3oviE+n+b3FaeNd0IondzqnnIuMGPSriRNMDhpjqkvQyK+zXMCfpzYh9vvoiBB7ZIpKwa74UNSqzVmd3TAFeTM+mo+s3HQtykxperpHx+MrOtaNF95+OKgq02Ecdco841jA72gHcAsVjS75EHdyA3Z3GrDw7za6ba/QUIm4IE1jaX1x98E+rMmeBiNndXxHN+DbRulsjkmiFhB0I5/qNjrQ==

commentPattern

3GmfGRczLSLkMXYaCDRUAvieq5ptqXfTRVE3MCyhqZryIPzUTtaef1IvvcJHebhQUAGLeRb9H1QdaPSIAo6Zqev5K7u60grY/mqvOGxJAyrYcCu9l28r37xtva1Oid9T

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

FizMTrKbYkp/GKbV/kTNoOKMZGp93kPKIAqAZbojZpHdlv00VVbvAcGsYDg6C2Dqcfs3igOpIctvFQwSJWl92D6i4IN0YeZ7EVaHhazb4Ek=

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

switch(foo){
    case 1:

    case 2: doSomething();
}

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

何时不使用

HFsG5NZ1uG5z1hRVOnKfLE9Cu21Q3zvEXF8K5DHdHSwTdqOsLDIqF++mzlWxil5s9izp49BsglCPEXl5dvkbqP/lW51gLP2ml95RKYUqlxWYk06Bt43jUsvi60B4u6tYwcDEBP6QN+2of+evSZ98c6q9q8j+76FU6GNTeIDaF7cvpaVio0E5GMkyUhZGYvLxqIHRwCc5wvGu9IQzjdj3IB5daGVJxX9F/Jk6exzAyfN+do8sgSZkNxSbT92IiiXq