Skip to content
← Back to rules

eslint/no-negated-condition 严格检查

An auto-fix is available for this rule.

它的作用

禁止使用否定条件。

为什么这是不好的?

否定条件更难理解。通过反转条件,可以使代码更具可读性。

示例

此规则的错误代码示例:

javascript
if (!a) {
  doSomethingC();
} else {
  doSomethingB();
}

!a ? doSomethingC() : doSomethingB();

此规则的正确代码示例:

javascript
if (a) {
  doSomethingB();
} else {
  doSomethingC();
}

a ? doSomethingB() : doSomethingC();

如何使用

要通过配置文件或命令行界面启用此规则,可以使用:

json
{
  "rules": {
    "no-negated-condition": "error"
  }
}
bash
oxlint --deny no-negated-condition

参考资料