Skip to content
← Back to rules

eslint/no-nested-ternary 样式

作用

禁止嵌套的三元表达式,以提高代码可读性和可维护性。

为什么这是坏的?

嵌套的三元表达式会使代码更难阅读和理解。它们可能导致逻辑复杂且难以调试。

示例

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

js
const result = condition1 ? (condition2 ? "a" : "b") : "c";

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

js
let result;
if (condition1) {
  result = condition2 ? "a" : "b";
} else {
  result = "c";
}

如何使用

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

json
{
  "rules": {
    "no-nested-ternary": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则在 v0.15.4 中添加。

参考资料