Nested ternary
Nested ternary. A ternary inside a ternary. Readers have to parse it twice.
This is how the comment reads inline, under the flagged line.
Why it matters
Nested conditionals in one expression hide the branches and are easy to get wrong when edited.
What to do
Use if/return, a lookup table, or a small function.
Examples
const c = a ? 'x' : b ? 'y' : 'z';
const c = LABELS[kind];
What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript, Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
const c = a ? 1 : b ? 2 : 3;
Left alone
const c = a ? 1 : 2;
const c = a?.b ?? 2;
function f(o?: { readonly timeoutMs?: number }) {}const t = `${a ? 'x' : ''} and ${b ? 'y' : ''}`;Research
A ternary inside a ternary. It hides which branch wins.
- skill: code-review
- owner's notes
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"nested-ternary"
],
"severity": {
"nested-ternary": "medium"
}
}
}