Skip to content
← Back to rules

unicorn/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": {
    "unicorn/no-negated-condition": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则已在 v0.0.18 中添加。

参考资料