Add option to the `no-nested-ternary` rule that allows parens wrapped nested ternaries
See original GitHub issueWe’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}]
Issue Analytics
- State:
- Created 8 years ago
- Reactions:4
- Comments:23 (13 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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…
and multiline
The problem with that is that it would allow parens around all ternaries, not just nested ones.