no-dupe-else-if
禁止 if-else-if 链中的重复条件
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
if-else-if
链通常用于根据特定条件只执行几个可能分支中的一个分支(或最多一个分支)的情况。
¥if-else-if
chains are commonly used when there is a need to execute only one branch (or at most one branch) out of several possible branches, based on certain conditions.
if (a) {
foo();
} else if (b) {
bar();
} else if (c) {
baz();
}
同一链中的两个相同的测试条件几乎总是代码中的错误。除非表达式中有副作用,否则重复项的 true
或 false
值将与链中较早的相同表达式相同,这意味着它的分支永远不会执行。
¥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();
}
}
何时不使用
¥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 中引入。