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

arrow-body-style autofix with 'in' in a for-loop init #11849

Closed
mdjermanovic opened this issue Jun 16, 2019 · 9 comments · Fixed by #13228
Closed

arrow-body-style autofix with 'in' in a for-loop init #11849

mdjermanovic opened this issue Jun 16, 2019 · 9 comments · Fixed by #13228
Assignees
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 help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules

Comments

@mdjermanovic
Copy link
Member

Tell us about your environment

  • ESLint Version: 5.16.0 (same behaviour with 6.0.0-rc.0)
  • Node Version: 10.16.0
  • npm Version: 6.9.0

What parser (default, Babel-ESLint, etc.) are you using?

default

Please show your full configuration:

Configuration
module.exports = {
  parserOptions: {
    ecmaVersion: 6,
  },
  rules: {
    "arrow-body-style": "error",
  }
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

for (let a = b => { return b in c; }; ;);
eslint index.js --fix

What did you expect to happen?

Not a SyntaxError in the fixed code.

What actually happened? Please include the actual, raw output from ESLint.

for (let a = b => b in c; ;);

The fix changed semantics and produced an error:

SyntaxError: for-in loop variable declaration may not have an initializer.

This is similar to #11706

Are you willing to submit a pull request to fix this bug?

Yes, when PR #11848 is finished, since it might be a similar fix.

Note

The fixed code is SyntaxError in Node/Chrome and Firefox (with a bit different message).

However, it's a valid ForStatement for acorn, babel-eslint and other parsers (e.g. not esprima).

Check: AST Explorer

Reported acorn #838

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Jun 16, 2019
@mdjermanovic mdjermanovic changed the title arrow-body-style autofix with in in a for-loop init arrow-body-style autofix with 'in' in a for-loop init Jun 17, 2019
@mysticatea mysticatea added accepted There is consensus among the team that this change meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Jun 18, 2019
@mysticatea
Copy link
Member

Thank you for your report.

I confirmed it. I prefer that the rule fixes that code along with wrapping by parentheses.


In the spec,

  • RelationalExpression production requires [In] parameter with positive to allow the in operator.
  • In IterationStatement production, the initialization part of for statement passes negative to [In] argument.
  • In ArrowFunction production, the bare function body passes the received value to [In] argument as is. On the other hand, the block function body doesn't pass [In] argument. I guess that it means the[In] argument becomes the default value (positive).
  • ParenthesizedExpression production passes positive to [In] argument always.

Therefore, for (let a = b => b in c; ;); should be a syntax error.

@mysticatea
Copy link
Member

By the way, for (let a = b => [b in c]; ;); is valid.

@mdjermanovic
Copy link
Member Author

I prefer that the rule fixes that code along with wrapping by parentheses.

Where should the fix put parentheses, there are several options.

For example, if the code that should be fixed is:

for (let a = (b, c, d) => { return b && c in d; }; ;);

parens could be put around the whole function:

for (let a = ((b, c, d) => b && c in d); ;);

or around the body:

for (let a = (b, c, d) => (b && c in d); ;);

or around the 'in' node':

for (let a = (b, c, d) => b && (c in d); ;);

Depending on the precedence, there could be more expressions inside the parens around the 'in' node.

@mdjermanovic
Copy link
Member Author

I checked how the fix works for cases like () => {return {}}, () => {return {}.a} etc.

The added parentheses are always around the whole body, so that option would be the consistent one:

for (let a = (b, c, d) => (b && c in d); ;);

@eslint-deprecated eslint-deprecated bot added the auto closed The bot closed this issue label Sep 15, 2019
@eslint-deprecated
Copy link

Unfortunately, it looks like there wasn't enough interest from the team
or community to implement this change. While we wish we'd be able to
accommodate everyone's requests, we do need to prioritize. We've found
that accepted issues failing to be implemented after 90 days tend to
never be implemented, and as such, we close those issues.
This doesn't mean the idea isn't interesting or useful, just that it's
not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

@mdjermanovic
Copy link
Member Author

I'll fix this.

@mdjermanovic mdjermanovic reopened this Sep 15, 2019
@mdjermanovic mdjermanovic removed the auto closed The bot closed this issue label Sep 15, 2019
@eslint-deprecated eslint-deprecated bot added the auto closed The bot closed this issue label Oct 16, 2019
@eslint-deprecated
Copy link

Unfortunately, it looks like there wasn't enough interest from the team
or community to implement this change. While we wish we'd be able to
accommodate everyone's requests, we do need to prioritize. We've found
that accepted issues failing to be implemented after 90 days tend to
never be implemented, and as such, we close those issues.
This doesn't mean the idea isn't interesting or useful, just that it's
not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

@mdjermanovic mdjermanovic removed the auto closed The bot closed this issue label Oct 16, 2019
@mdjermanovic mdjermanovic reopened this Oct 16, 2019
@kaicataldo kaicataldo added good first issue Good for people who haven't worked on ESLint before help wanted The team would welcome a contribution from the community for this issue labels Feb 7, 2020
@kaicataldo kaicataldo removed the good first issue Good for people who haven't worked on ESLint before label Feb 17, 2020
@anikethsaha
Copy link
Member

@mdjermanovic are you working on this? if not I would like to take this

@mdjermanovic
Copy link
Member Author

@anikethsaha I'm not working on it at the moment, feel free to take this!

mdjermanovic pushed a commit that referenced this issue Jul 11, 2020
* Fix: add parens when in operator (fixes #11849)

* Chore: some more tests

* Chore: update jsdoc comments

Co-authored-by: YeonJuan <yeonjuan93@naver.com>

* Update: add paren only for ForStatement

* Update: fixed adding extra parens

* Update: minified traversing using visitor query

* Chore: isInOp to false while existing

* Update: fixed the logic

* Update: logic

* Update: checking inside of for loop

* Chore: updated is for in check

Co-authored-by: YeonJuan <yeonjuan93@naver.com>
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jan 8, 2021
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jan 8, 2021
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 help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants