Index

no-negated-condition

禁止否定条件

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

否定条件更难理解。可以通过反转条件来使代码更易读。

🌐 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:

  • if 语句,其具有 else 分支
  • 三元表达式

此规则的错误代码示例:

🌐 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

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

版本

此规则是在 ESLint v1.6.0 中引入。

资源