no-negated-condition
禁止否定条件
否定条件更难理解。通过反转条件可以使代码更具可读性。
¥Negated conditions are more difficult to understand. Code can be made more readable by inverting the condition instead.
规则详情
¥Rule Details
此规则不允许以下任一情况的否定条件:
¥This rule disallows negated conditions in either of the following:
-
具有
else
分支的if
语句¥
if
statements which have anelse
branch -
三元表达式
¥ternary expressions
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
在线运行
/*eslint no-negated-condition: "error"*/
if (!a) {
doSomething();
} else {
doSomethingElse();
}
if (a != b) {
doSomething();
} else {
doSomethingElse();
}
if (a !== b) {
doSomething();
} else {
doSomethingElse();
}
!a ? c : b
此规则的正确代码示例:
¥Examples of correct code for this rule:
在线运行
/*eslint no-negated-condition: "error"*/
if (!a) {
doSomething();
}
if (!a) {
doSomething();
} else if (b) {
doSomething();
}
if (a != b) {
doSomething();
}
a ? b : c
版本
此规则是在 ESLint v1.6.0 中引入。