no-nested-ternary

Disallow a ternary inside the branch of another ternary

A single a ? b : c is easy to read at a glance. With a second one inside a branch, you have to work out which ? goes with which : before you know what the value is.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-nested-ternary" = "error" # or "warn"

Examples#

example.js1 problem
const size = width > 1200 ? "large" : width > 600 ? "medium" : "small";
example.js1 problem
const price = user ? (user.isMember ? 8 : 10) : 12;
example.jsNo problems
function sizeOf(width) {
  if (width > 1200) return "large";
  if (width > 600) return "medium";
  return "small";
}

const size = sizeOf(width);

When not to use it#

A chain of a ? x : b ? y : z with one condition per line reads like if / else if to many people. If your team likes that style, leave the rule off.