Index

no-dupe-else-if

禁止 if-else-if 链中的重复条件

Recommended

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

if-else-if 链通常在需要根据某些条件从多个可能的分支中只执行一个分支(或最多一个分支)时使用。

if (a) {
    foo();
} else if (b) {
    bar();
} else if (c) {
    baz();
}

在同一链中的两个相同测试条件几乎总是在代码中是错误的。除非表达式中存在副作用,否则重复的表达式将评估为与链中先前相同表达式相同的 truefalse 值,这意味着它的分支永远无法执行。

🌐 Two identical test conditions in the same chain are almost always a mistake in the code. Unless there are side effects in the expressions, a duplicate will evaluate to the same true or false value as the identical expression earlier in the chain, meaning that its branch can never execute.

if (a) {
    foo();
} else if (b) {
    bar();
} else if (b) {
    baz();
}

在上述示例中,baz() 永远不会执行。显然,baz() 只有在 b 求值为 true 时才可能执行,但在那种情况下会执行 bar(),因为它在链中位置更早。

🌐 In the above example, baz() can never execute. Obviously, baz() could be executed only when b evaluates to true, but in that case bar() would be executed instead, since it’s earlier in the chain.

规则详情

🌐 Rule Details

此规则不允许在相同的 if-else-if 链中出现重复条件。

🌐 This rule disallows duplicate conditions in the same if-else-if chain.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-dupe-else-if: "error"*/

if (isSomething(x)) {
    foo();
} else if (isSomething(x)) {
    bar();
}

if (a) {
    foo();
} else if (b) {
    bar();
} else if (c && d) {
    baz();
} else if (c && d) {
    quux();
} else {
    quuux();
}

if (n === 1) {
    foo();
} else if (n === 2) {
    bar();
} else if (n === 3) {
    baz();
} else if (n === 2) {
    quux();
} else if (n === 5) {
    quuux();
}

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint no-dupe-else-if: "error"*/

if (isSomething(x)) {
    foo();
} else if (isSomethingElse(x)) {
    bar();
}

if (a) {
    foo();
} else if (b) {
    bar();
} else if (c && d) {
    baz();
} else if (c && e) {
    quux();
} else {
    quuux();
}

if (n === 1) {
    foo();
} else if (n === 2) {
    bar();
} else if (n === 3) {
    baz();
} else if (n === 4) {
    quux();
} else if (n === 5) {
    quuux();
}

该规则还可以检测某些条件不完全相同的情况,但由于 ||&& 操作符的逻辑,分支永远不会被执行。

🌐 This rule can also detect some cases where the conditions are not identical, but the branch can never execute due to the logic of || and && operators.

此规则的额外错误代码示例:

🌐 Examples of additional incorrect code for this rule:

在线运行
/*eslint no-dupe-else-if: "error"*/

if (a || b) {
    foo();
} else if (a) {
    bar();
}

if (a) {
    foo();
} else if (b) {
    bar();
} else if (a || b) {
    baz();
}

if (a) {
    foo();
} else if (a && b) {
    bar();
}

if (a && b) {
    foo();
} else if (a && b && c) {
    bar();
}

if (a || b) {
    foo();
} else if (b && c) {
    bar();
}

if (a) {
    foo();
} else if (b && c) {
    bar();
} else if (d && (c && e && b || a)) {
    baz();
}

请注意,此规则不会将链中的条件与语句内部的条件进行比较,并且在以下情况下不会触发警告:

🌐 Please note that this rule does not compare conditions from the chain with conditions inside statements, and will not warn in the cases such as follows:

if (a) {
    if (a) {
        foo();
    }
}

if (a) {
    foo();
} else {
    if (a) {
        bar();
    }
}

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

何时不使用

🌐 When Not To Use It

在极少数情况下,你在同一链中确实需要相同的测试条件,这必然意味着链中的表达式会导致并依赖副作用,你将不得不关闭此规则。

🌐 In rare cases where you really need identical test conditions in the same chain, which necessarily means that the expressions in the chain are causing and relying on side effects, you will have to turn this rule off.

版本

此规则是在 ESLint v6.7.0 中引入。

资源