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: Add support for parens on left side for-loops (fixes: #8393) #8679
Update: Add support for parens on left side for-loops (fixes: #8393) #8679
Conversation
LGTM |
@VictorHom, thanks for your PR! By analyzing the history of the files in this pull request, we identified @michaelficarra, @not-an-aardvark and @vitorbal to be potential reviewers. |
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.
Thanks! This looks good, I just have a small suggestion.
Also, can you change the prefix of the commit message to Update:
rather than Fix:
? Our tooling take Update:
as an indication that a change is semver-minor according to our semantic versioning policy, and this commit is semver-minor because it's a bugfix that causes a rule to report more errors.
lib/rules/no-extra-parens.js
Outdated
|
||
return rightParenToken && tokenAfterRightParen && | ||
!sourceCode.isSpaceBetweenTokens(rightParenToken, tokenAfterRightParen) && | ||
tokenAfterRightParen.type === "Keyword"; |
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.
Instead of checking whether the rightmost token is a keyword, can you use something like this instead?
const tokenBeforeRightParen = sourceCode.getLastToken(node);
!astUtils.canTokensBeAdjacent(tokenBeforeRightParen, tokenAfterRightParen)
The reason a space is needed isn't because the following token is a keyword, it's because the two tokens will combine if the parens are removed. This makes a difference in cases like this:
for ((foo['bar'])of baz);
The ]
and of
would not combine into a single token if the parens were removed, but the autofixer unnecessarily inserts a space at the moment.
4731ca7
to
0e7a05d
Compare
LGTM |
0e7a05d
to
e193bdf
Compare
LGTM |
e193bdf
to
4e7aa1a
Compare
LGTM |
@not-an-aardvark thanks I added the example you provided as a test if that is alright: I updated to check for adjacent nodes as you suggested. Let me know if there are other updates! |
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.
Thanks for contributing to ESLint! Just some minor nitpicks, but otherwise LGTM.
tests/lib/rules/no-extra-parens.js
Outdated
@@ -1005,3 +1025,5 @@ ruleTester.run("no-extra-parens", rule, { | |||
) | |||
] | |||
}); | |||
|
|||
|
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.
Nit: Extra newline here
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.
As an aside, maybe we should enforce this with no-multiple-empty-lines
on the codebase.
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.
Yes, agreed!
lib/rules/no-extra-parens.js
Outdated
* @returns {boolean} `true` if a space should be inserted after the node | ||
* @private | ||
*/ | ||
function requiresAfterSpace(node) { |
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.
Nitpick: requiresTrailingSpace
might be a little clearer for this function name
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.
Looks good to me once @kaicataldo's comments are addressed. Thanks!
LGTM |
@kaicataldo @not-an-aardvark addressed the comments. |
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. Thanks for contributing to ESLint!
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[ X] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
Added a fix around supporting parens support for for-in and for-of loops which weren't giving off warnings. I updated no-extra-parens.js with supporting tests.
Is there anything you'd like reviewers to focus on?
I wrote a function requiresAfterSpace to determine if a space is required after a node. I didn't know a good way to test this and wasn't sure if what I have is optimal (not sure what I don't know). If you could provide some tips on what a good approach would be to fixing these issues, that would be great. I followed the docs from http://eslint.org/docs/developer-guide/working-with-rules and