Skip to content
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

Merged

Conversation

VictorHom
Copy link
Member

@VictorHom VictorHom commented Jun 2, 2017

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

@eslintbot
Copy link

LGTM

@mention-bot
Copy link

@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.

Copy link
Member

@not-an-aardvark not-an-aardvark left a 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.


return rightParenToken && tokenAfterRightParen &&
!sourceCode.isSpaceBetweenTokens(rightParenToken, tokenAfterRightParen) &&
tokenAfterRightParen.type === "Keyword";
Copy link
Member

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.

@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels Jun 3, 2017
@VictorHom VictorHom changed the title Fix: (WIP) Add support for parens on left side for-loops (fixes: #8393) Update: (WIP) Add support for parens on left side for-loops (fixes: #8393) Jun 3, 2017
@VictorHom VictorHom force-pushed the pr/false_negative_no_exta_parens branch from 4731ca7 to 0e7a05d Compare June 3, 2017 12:57
@eslintbot
Copy link

LGTM

@VictorHom VictorHom force-pushed the pr/false_negative_no_exta_parens branch from 0e7a05d to e193bdf Compare June 3, 2017 12:59
@eslintbot
Copy link

LGTM

@VictorHom VictorHom force-pushed the pr/false_negative_no_exta_parens branch from e193bdf to 4e7aa1a Compare June 3, 2017 13:03
@eslintbot
Copy link

LGTM

@VictorHom
Copy link
Member Author

@not-an-aardvark thanks

I added the example you provided as a test if that is alright:
for ((foo['bar'])of baz);

I updated to check for adjacent nodes as you suggested. Let me know if there are other updates!

@VictorHom VictorHom changed the title Update: (WIP) Add support for parens on left side for-loops (fixes: #8393) Update: Add support for parens on left side for-loops (fixes: #8393) Jun 5, 2017
Copy link
Member

@kaicataldo kaicataldo left a 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.

@@ -1005,3 +1025,5 @@ ruleTester.run("no-extra-parens", rule, {
)
]
});


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Extra newline here

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed!

* @returns {boolean} `true` if a space should be inserted after the node
* @private
*/
function requiresAfterSpace(node) {
Copy link
Member

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

Copy link
Member

@not-an-aardvark not-an-aardvark left a 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!

@eslintbot
Copy link

LGTM

@VictorHom
Copy link
Member Author

VictorHom commented Jun 7, 2017

@kaicataldo @not-an-aardvark addressed the comments.

Copy link
Member

@kaicataldo kaicataldo left a 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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants