question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add option to the `no-nested-ternary` rule that allows parens wrapped nested ternaries

See original GitHub issue

We’ve been discussing it over at sindresorhus/xo#12 and concluded it might be nice to have a rule where no-nested-ternary wouldn’t trigger if the nested ternary is wrapped in parenthesis.

We agreed the following ‘unreadable’ and ambiguous code

var foo = i > 5 ? true : i < 100 ? true : false;

could be improved by adding parenthesis.

var foo = i > 5 ? true : (i < 100 ? true : false);

As it stands, ESLint catches both as nested ternaries.


There also comes the issue of logic. The following is correct logic; platform === 'linux' isn’t evaluated unless opts.dash is falsey.

var file = opts.dash ? '-' : (platform === 'linux' ? '/dev/stdin' : '/proc/self/fd/0');

Alternatively, the logic would have to be

var file;
if (opts.dash) {
  file = '-';
} else {
  file = platform === 'linux' ? '/dev/stdin' : '/proc/self/fd/0';
}

which is much less than ideal.


It’d be nice if there was an option for allowing parenthesis wrapped nested ternary expressions, i.e.

{'no-nested-ternary': [2, {allowParensWrapped: true}]

// @sindresorhus

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:23 (13 by maintainers)

github_iconTop GitHub Comments

5reactions
wmertenscommented, Feb 28, 2016

It is hard to avoid nested ternaries in React JSX expressions since it is the only available flow control inside them short of creating an immediately-evaluated function.

The only other alternative is preparing the result beforehand, which can be confusing and hard to maintain.

When doing multi-line ternaries, it also helps with figuring out nesting, somewhat.

I think that allowing parens around ternaries is an acceptable trade-off here…

<div>{isFoo ? (isBar ? "barred foo" : <Foo />) : <Whee />}</div>

and multiline

<div>{isFoo ? (
  isBar ? (
    "barred foo"
  ) : (
    <Foo />
  )
) : (
  <Whee />
)}</div>
1reaction
nzakascommented, Sep 9, 2015

The problem with that is that it would allow parens around all ternaries, not just nested ones.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-nested-ternary - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
How can I avoid nested ternary expressions in my code?
This is just tricking ESLint and adding indirection. I agree that this rule should disabled if no alternatives are suitable. Can you please...
Read more >
Conditional branching: if, '?' - The Modern JavaScript Tutorial
The if (…) statement evaluates the expression in its parentheses and converts the result to a boolean. Let's recall the conversion rules ......
Read more >
tslint-eslint-rules - npm Package Health Analysis | Snyk
Learn more about tslint-eslint-rules: package health score, popularity, security, ... in your projects for vulnerabilities and provides automated fix advice.
Read more >
Low level issues - Embold Help Center
In all other cases the parameter(s) must be wrapped in parentheses. This rule enforces the ... This rule allows setting a cyclomatic complexity...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found