Skip to content

noNestedTernary

Diagnostic Category: lint/nursery/noNestedTernary

Since: v1.9.3

Sources:

Disallow nested ternary expressions.

Nesting ternary expressions can make code more difficult to understand.

const thing = foo ? bar : baz === qux ? quxx : foobar;
code-block.js:1:27 lint/nursery/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/nursery/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.

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