Disable the no-nested-ternary
rule
#224
Replies: 3 comments 1 reply
-
Nesting ternary expressions can make code more difficult to understand. Incorrect code: const foo = bar ? baz : qux === quxx ? bing : bam; Correct code: let thing;
if (foo) {
thing = bar;
} else if (baz === qux) {
thing = quxx;
} else {
thing = foobar;
} It's fine to use a |
Beta Was this translation helpful? Give feedback.
-
When going multiline, the indenting helps to make it a bit more clear:
If there was an option to force multiline for nested ternary, then I might be okay with it. But even Leroy's example is not super readable on multiline (I would have preferred the last two lines to be indented as well): const foo = bar
? baz
: qux === quxx
? bing
: bam.veryLongWordThatShouldBeSplitIntoMultipleLinesToAvoidHorizontalScrolling;
In this case, that verbosity comes with clarity, which in my opinion is worth it. Personally i've ran into this rule as well, but I'm happy it protects me against my own laziness :) Or in the rare occasion that it's okay to have it, I'll disable it. |
Beta Was this translation helpful? Give feedback.
-
Some times we do conditional rendering inside components like:
We could try to fix it by:
But the linter will complain again, now saying we SHOULD use a ternary:
Personally, I don't like having multiple return functions in my components. |
Beta Was this translation helpful? Give feedback.
-
There is a proposal to disable the
no-nested-ternary
rule.Please see the documentation
Beta Was this translation helpful? Give feedback.
All reactions