-
-
Notifications
You must be signed in to change notification settings - Fork 367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add no-identical-alternatives-in-ternary
rule
#2430
Conversation
I think the rule name should include |
docs/rules/no-identical-alternate.md
Outdated
@@ -0,0 +1,22 @@ | |||
# Disallows nested ternary expressions with repeated alternatives |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Disallows nested ternary expressions with repeated alternatives | |
# Disallow nested ternary expressions with repeated alternatives |
rules/no-identical-alternate.js
Outdated
|
||
const MESSAGE_ID = 'no-identical-alternate'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Replace nested ternary expressions with a logical expression.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be more specific about what kind of ternary expression it targets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about Replace repeated alternatives of ternary expressions with a logical expression.
or Simplify repeated alternatives of ternary expressions.
?
I'm not very good with rule naming, but I think this is a better name |
no-identical-alternate
no-identical-alternatives-in-ternary
no-identical-alternatives-in-ternary
no-identical-alternatives-in-ternary
rule
/** @param {import('estree').ConditionalExpression} node */ | ||
ConditionalExpression(node) { | ||
// Check if the alternate is a ConditionalExpression | ||
if (node.alternate && node.consequent.type === 'ConditionalExpression') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should go deeper,
a ? (b ? 1 : 2) : (c ? 1 : 3);
a ? (b ? 1 : 2) : (c ? 3 : 1)
should also be reported. I'm not sure about the equivalent... but there must be a better way to write?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is too complicated, I don't think it can be done easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should ignore this situation, it is almost impossible to happen in the real world.
Or create another rule that names no-complex-ternary
to disallow it.
6912aa8
to
8246f20
Compare
This comment was marked as resolved.
This comment was marked as resolved.
no-identical-alternatives-in-ternary
ruleno-identical-alternatives-in-ternary
rule
d98c86c
to
46c0ba4
Compare
46c0ba4
to
95b18ee
Compare
no-identical-alternatives-in-ternary
ruleno-identical-alternatives-in-ternary
rule
I have reimplemented it. It is supports multiple lines and complex comments retention. Now it's ready for review @fisker |
@param {import('eslint').SourceCode} sourceCode | ||
@returns {number} | ||
*/ | ||
function findIndexOfQuestionMarkInConditionalExpression(node, sourceCode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sourceCode.getTokenAfter(node.test, () => ...isQuestionMarkToken)
should enough
'a && b ? c : 1', | ||
], | ||
invalid: [ | ||
'a?b?c:1:1', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a? b?1:c :1
a? 1: b?c:1
a? 1: b?1:c
Should be invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a ? b ? (1: c :1)
fix to (a && b) ? 1 : c
a ? 1: (b ? c : 1)
fix to (a || !b) ? 1 : c
a ? 1 : (b ? 1 : c)
fix to (a || b) ? 1 : c
This implementation is too complicated
I don't think this is easy to do. It cannot retain original comments.
Rather than implementing such a complex rule, I prefer closing this PR
Close because it is too complex to implement edge cases perfectly. |
Close #2153