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
Update: for yoda
, add a fixer
#7199
Conversation
@not-an-aardvark, thanks for your PR! By analyzing the annotation information on this pull request, we identified @vitorbal, @platinumazure and @puzrin to be potential reviewers |
LGTM |
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.
Besides the one inline change I've noted, could we also see a couple of tests where whitespace between tokens is (a) not present, and (b) inconsistent? Just so we know what to expect in those cases. Thanks!
!(!isEqualityOperator(node.operator) && onlyEquality) && | ||
isComparisonOperator(node.operator) && | ||
!(exceptRange && isRangeTest(context.getAncestors().pop())) | ||
) { | ||
context.report({ | ||
node, | ||
message: "Expected literal to be on the right side of {{operator}}.", | ||
message: `Expected literal to be on the ${always ? "left" : "right"} side of {{operator}}.`, |
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.
In order to facilitate possible i18n someday, could the "left"/"right" interpolation be performed using our double-brace substitution (i.e., same as how "{{operator}}" is handled)? Thanks!
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.
Actually, I think left
/right
is part of the message. E.g.: it's 左
/右
in Japanese.
11ca066
to
47b52aa
Compare
LGTM |
Updated to add inconsistent/missing spacing tests and use the double-brace substitution. edit: Updated again to remove the redundant ternary expressions here and replace them with |
47b52aa
to
e60be89
Compare
LGTM |
Still LGTM even after the last change (removing redundant ternary expressions). Thanks! |
I appreciate the commit message for this one 👍 |
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.
LGTM, thank you!
i'll champion this! |
Thanks for working on this, @not-an-aardvark! 🎉 |
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[x] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
Please check each item to ensure your pull request is ready:
What changes did you make? (Give an overview)
This adds a fixer for
yoda
. Whenyoda
reports that a binary expression should be flipped, the fixer can flip the sides of the expression, along with the operator, to fix the issue.Is there anything you'd like reviewers to focus on?
In general, flipping around the order of a comparison is unsafe, because it changes execution order (e.g. in
if (foo() === bar())
,foo()
gets called beforebar()
). However, I don't think this is a problem foryoda
because one side of the expression is always a literal, so it won't have side-effects.