Skip to content
← Back to rules

eslint/no-negated-condition Pedantic

🚧 An auto-fix is planned for this rule, but not implemented at this time.

作用

不允许使用取反条件。

为什么这不好?

取反条件更难理解。通过反转条件,可以让代码更易读。

示例

以下是此规则的错误代码示例:

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

!a ? doSomethingC() : doSomethingB();

以下是此规则的正确代码示例:

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

a ? doSomethingB() : doSomethingC();

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-negated-condition": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-negated-condition": "error",
  },
});
bash
oxlint --deny no-negated-condition

版本

此规则添加于 v0.0.18。

参考资料