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.

Option to ignore no-nested-ternary for daisy-chained ternaries only

See original GitHub issue

What rule do you want to change? no-nested-ternary

Does this change cause the rule to produce more or fewer warnings? Fewer warnings.

How will the change be implemented? (New option, new default behavior, etc.)? New option, e.g.: allowDaisyChain: true

Please provide some example code that this change will affect:

var foo = false;
var bar = true;

var baz =
	foo ? 'foo' :
	bar ? 'bar' :
	null;

var qux =
	!foo ?
		!bar ? null : 'bar'
	: 'foo';

(ESLint Demo) Both baz and qux will be 'bar', but the code for the assignment to baz is far more easy to reason about because it’s daisy-chained and not just nested.

It’d be great to allow nested ternaries of that type whilst still disallowing the more convoluted qux form.

What does the rule currently do for this code? Raises 2 errors:

5:2 - Do not nest ternary expressions. (no-nested-ternary) 10:2 - Do not nest ternary expressions. (no-nested-ternary)

What will the rule do after it’s changed? Raise only one error:

10:2 - Do not nest ternary expressions. (no-nested-ternary)

For a more in-depth analysis of nested ternaries and how the daisy-chained version is useful because it’s like a switch statement turned into an expression, capable of more than just strict equality checks of one value, see here: https://medium.com/javascript-scene/nested-ternaries-are-great-361bddd0f340#c69a

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
whitelizardcommented, Dec 17, 2018

I would love this option.

I have missed to be able to do this for a long time (with still good linting active + integration with prettier formatting, etc - i.e. an exception option to the no-nested-ternary rule).

I’m thinking about this article on the subject that argues for the use of chained ternaries, and that I agree on: https://medium.com/javascript-scene/nested-ternaries-are-great-361bddd0f340

Also as @zeorin said:

with functional programming becoming more popular, it’s likely that there will be an uptake in conditional expressions vs. statements

Yes.

1reaction
zeorincommented, Jun 8, 2018

The more I think about it, the more I like allowAlternate. Although it’s likely to be unfamiliar to most devs at first, it is the closest thing we have to a formal term for it (that I’m aware).

With an explanation of the terms “consequent” and “alternate” in the docs for this option I think it’ll be clear, and we’ll also be expanding the available technical vocabulary for devs that (like me) are unfamiliar with these terms.

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 >
Are chain ternary operators an antipattern? : r/reactjs - Reddit
Daisy chained ternary is just guard clauses in an expression. Yes, that example is worse, but your initial chained ternary is also more ......
Read more >
Alternative to nested ternary operator in JS - Stack Overflow
I tried to come up with a reasonable lookup map option, ... Then you could chain them with the bitwise or ( ||...
Read more >
Nested Ternaries are Great - Medium
Daisy Chaining vs Nesting. First, we've flattened out the nesting. “Nested” ternaries is a bit of a misnomer, because ternaries are easy to ......
Read more >
demo-outil-edition - node_modules - eslint - CHANGELOG.md
c5c7086 Fix: ignore aligning single line in key-spacing (fixes #11414) ... Docs: clarify defaultAssignment option, fix no-unneeded-ternary ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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