Skip to content
← Back to rules

unicorn/no-nested-ternary 风格

🛠️ An auto-fix is available for this rule for some violations.

它的作用

此规则禁止深度嵌套的三元表达式。 仅嵌套一层且被括号包裹的三元表达式是允许的。

为什么这是问题?

嵌套三元表达式会使代码更难理解。

示例

此规则的错误代码示例:

javascript
const foo = i > 5 ? (i < 100 ? true : false) : true;
const foo = i > 5 ? true : i < 100 ? true : i < 1000 ? true : false;

此规则的正确代码示例:

javascript
const foo = i > 5 ? (i < 100 ? true : false) : true;
const foo = i > 5 ? (i < 100 ? true : false) : i < 100 ? true : false;

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "unicorn/no-nested-ternary": "error"
  }
}
bash
oxlint --deny unicorn/no-nested-ternary

参考资料