Skip to content
← Back to rules

unicorn/prefer-logical-operator-over-ternary Style

💡 A suggestion is available for this rule.

它的作用

此规则会查找可以简化为逻辑运算符的三元表达式。

为什么这不好?

使用逻辑运算符比三元表达式更短,也更简单。

示例

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

javascript
const foo = bar ? bar : baz;
console.log(foo ? foo : bar);

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

javascript
const foo = bar || baz;
console.log(foo ?? bar);

如何使用

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

json
{
  "rules": {
    "unicorn/prefer-logical-operator-over-ternary": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-logical-operator-over-ternary": "error",
  },
});
bash
oxlint --deny unicorn/prefer-logical-operator-over-ternary

版本

此规则是在 v0.0.15 中添加的。

参考资料