Skip to content

noNestedTernary

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"style": {
"noNestedTernary": "error"
}
}
}
}

¥Description

禁止使用嵌套的三元表达式。

¥Disallow nested ternary expressions.

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

¥Nesting ternary expressions can make code more difficult to understand.

¥Examples

¥Invalid

const thing = foo ? bar : baz === qux ? quxx : foobar;
code-block.js:1:27 lint/style/noNestedTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not nest ternary expressions.

> 1 │ const thing = foo ? bar : baz === qux ? quxx : foobar;
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Nesting ternary expressions can make code more difficult to understand.

Convert nested ternary expression into if-else statements or separate the conditions to make the logic easier to understand.

foo ? baz === qux ? quxx() : foobar() : bar();
code-block.js:1:7 lint/style/noNestedTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not nest ternary expressions.

> 1 │ foo ? baz === qux ? quxx() : foobar() : bar();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Nesting ternary expressions can make code more difficult to understand.

Convert nested ternary expression into if-else statements or separate the conditions to make the logic easier to understand.

¥Valid

const thing = foo ? bar : foobar;
let thing;
if (foo) {
thing = bar;
} else if (baz === qux) {
thing = quxx;
} else {
thing = foobar;
}

¥Related links