Add prefer-conditional-expression rule #2363
Conversation
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 better name would be prefer-ternary-declaration
|
||
if (true) | ||
~~ [X] | ||
x = "TRUE"; |
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.
what happens if there is complex logic in these branches? I would prefer to use this more verbose pattern over a ternary if switching to a ternary meant a very long multiline statement.
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.
The rule needs x = <<code>>
exactly, it doesn't just search to see if it appears somewhere. So normally that would filter out complex logic.
Maybe you were thinking of something like this?
let x: number[];
if (so) {
x = [1,2,3].map(n => {
switch (n) {
... lots of code ...
}
});
} else {
x = [];
}
Maybe the rule could only activate if the expression takes up a single line.
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.
Yeah, that's a good example. In that case I personally would prefer to keep the if / else blocks; I generally only want to use ternarys when each RHS expression is a single line. If this is going to be a core rule, I think it should be more conservative than it is now -- can you make that change?
src/configs/all.ts
Outdated
@@ -117,6 +117,7 @@ export const rules = { | |||
"no-use-before-declare": true, | |||
"no-var-keyword": true, | |||
"no-void-expression": true, | |||
"prefer-conditional": true, |
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.
can you please change the rule name? I think simply "conditional" is too vague because if-else statements are often described as conditionals as well. For example, no-conditional-assignment applies to the conditional expression as part of an if
or else
statement.
I would be happy with prefer-conditional-operator
or prefer-ternary-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.
How about prefer-conditional-expression
? if
and ? ... :
are both conditionals, but ? ... :
is an expression and if
is a statement.
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.
Ok, works. The ESTree spec calls it a conditional expression 👍
Is this rule documented anywhere? I don't see it in the rules list. |
Thanks for new awesome rules with the 5.3.0 release! As @lfairy said it would be great to have the new rules listed in the docs with the release.
I also recognized that most of the mentioned
Shouldn't it be mandatory to update the docs with rule changes? |
The docs are located in a different branch that needs to be updated when releasing a new version. Seems like this was forgotten? @nchen63 @adidahiya |
yep, thanks for the heads up, looks like we missed updating the docs for 5.3. we'll update them today! |
PR checklist
Overview of change:
Added the
prefer-conditional
rule, which warns onlet x; if (so) { x = 1; } else { x = 2; }
.Not sure if this should do anything about
if (so) { return 1; } else { return 2; }
. Thoughts?CHANGELOG.md entry:
[new-rule]
prefer-conditional