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+HlOzzEdgN6n1zCfLdl4b188xDhIux8e9I7YyP0snBZFpwkFs5Frhob1Jzp7iSoKkANguJVmikTY6fFJOixK2+sSaDn+OyyHQIoou4fVQgSk6D9cF8ZGWTt2PHoBwG+JaN2rb9O0RQGpmulEu0x/cgom9D/7061WfGex86iBXhED2SSqYgI0KNhCE67Ia6aJiZ979TVCDpTpik0ySo8QfM+VmuB+Si2PVp9LDPIJm5Nf1CPSqvqTOjeSaaOUzL3zf7ymOoS0cwSeVC5uSgrNZ6ZNpncmLaykjSTSo8EZhs9tBLc3qpp4tBN0sQQ7pQBFU0hNo7oowhaRURPu8Pvx6tTyAS2EgIqvXki0tZXFpnkRL57FC2EfS43fup/L6yx7p++g9tvi7q6utRM58FLV9wBNEtkklzgJ8aMEO0tY3hq2kVE/+LXYChfjX9Jrqs2NezhSEEuvRg==

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