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 no-dupe-ternary-expressions

See original GitHub issue

Please describe what the rule should do: The rule will catch and mark as errors any ternary operator that has identical left and right hand expressions. It wall also catch and mark as errors identical ternary conditions which necessarily results in unreachable code.

What new ECMAScript feature does this rule relate to? It does not relate to a new ECMAScript feature, but please indulge me a little and read on. I read the updated contribution guidelines but thought the value of this proposed rule might be high enough despite the fact that it’s not related to new JS features.

What category of rule is this? (place an “X” next to just one item)

  • Warns about a potential error (problem)

Provide 2-3 code examples that this rule will warn about:

const getFee = (isMember) => isMember ? 2.00 : 2.00
// "Error: identical ternary expressions"

thing === otherThing ? getFee(isMember) && myBoolean : getFee(isMember) && myBoolean
// "Error: identical ternary expressions"

Edit: removed examples pertaining to no-unreachable-ternary.

Why should this rule be included in ESLint (instead of a plugin)? I understand that the nature of ESLint is that it’s a pluggable linter and after reading the contribution guidelines, new rules are only generally accepted if they relate to new JavaScript standards/functionality. Considering there are already 200+ rules this principle makes a lot of sense.

Even so, I would argue that duplicate ternary expressions (in the form of statements or conditions) are always and necessarily an error, even if the developer doesn’t at first realise what the ternary (or chained ternary) operator is doing.

Avoiding unreachable code is already a great thing for a linter to detect and if we assume that developers want to be informed when a ternary operator does the exact same thing whether or not the condition it evaluates is true or false, then this rule covers both those cases and would be very valuable in any project.

The rule is stand alone (although relates to no-unreachable and no-unneeded-ternary) and it’s hard to imagine a scenario where developers would not want this rule enabled by default.

For these reasons, I would ask that the ESLint core contributors (hello there!) consider accepting this proposal, which (if accepted and taken as a recommended rule) would be a breaking change and need to be bundled in a major release.

Are you willing to submit a pull request to implement this rule? Absolutely 😸

Do you have an idea how this rule could be implemented? Yes - the implementation would tokenize all expressions as well as all ternary symbols (so [‘:’, ‘?’]) and create an ordered array. Every element inside the array that is exceeded by a ‘?’ token is a condition and every element that is exceeded by a ‘:’ (or if it is the last element in the array) is an expression.

Some exception/logic for handling complex expression (i.e. function calls or boolean expressions) may be needed.

The resulting array could then easily be searched for identical conditions and identical left and right hand expressions.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
GrayedFoxcommented, Oct 14, 2020

That said, I did say that I understood that without consensus there was no guarantee of a merge or implementation, and I did also read the docs about new rules relating to new language features, so I will of course accept that outcome if that is the case 🙇🏾‍♂️

1reaction
nzakascommented, Oct 14, 2020

Our official (and documented) policy is that new rules must be related to new language features. Unofficially we might consider new rules not related to new language features if the errors they unveil are serious enough.

To me, this proposal doesn’t meet the bar of “serious enough.” The downside of not flagging this pattern is that everything works. This is flagging an efficiency issue, not really an error.

This seems exactly the case where a plugin makes sense.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional (ternary) operator - JavaScript - MDN Web Docs
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?)
Read more >
Make Your Code Cleaner with JavaScript Ternary Operator
This tutorial shows you how to use the JavaScript ternary operator as the shortcut of the if-else statement to make your code cleaner....
Read more >
JavaScript Ternary Operator (with Examples) - Programiz
A ternary operator evaluates a condition and executes a block of code based on the condition. Its syntax is: condition ? expression1 :...
Read more >
JavaScript Ternary Operator - GeeksforGeeks
Ternary Operator : The “Question mark” or “conditional” operator in JavaScript is a ternary operator that has three operands.
Read more >
How to use a ternary operator to add a class to an HTML ...
How to use a ternary operator to add a class to an HTML element in ejs · javascript · node.js · template-engine ·...
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