no-negated-condition
Disallow negated conditions
否定条件更难理解。可以通过倒置条件来使代码更易读。
规则细节
这条规则不允许以下任何一种情况下的否定条件:
if
语句有一个else
分支- 三元表达式
使用此规则的错误示例:
Open in Playground
/*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
使用此规则的正确示例:
Open in Playground
/*eslint no-negated-condition: "error"*/
if (!a) {
doSomething();
}
if (!a) {
doSomething();
} else if (b) {
doSomething();
}
if (a != b) {
doSomething();
}
a ? b : c
Version
This rule was introduced in ESLint v1.6.0.