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
Closed

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

mdjermanovic opened this issue Jun 16, 2019 · 9 comments
Assignees

Comments

@mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Jun 16, 2019

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 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 rule and removed triage labels Jun 18, 2019
@mysticatea
Copy link
Member

@mysticatea mysticatea commented Jun 18, 2019

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

@mysticatea mysticatea commented Jun 18, 2019

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

@mdjermanovic
Copy link
Member Author

@mdjermanovic mdjermanovic commented Jun 25, 2019

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

@mdjermanovic mdjermanovic commented Jun 28, 2019

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 eslint bot closed this Sep 15, 2019
@eslint eslint bot added the auto closed label Sep 15, 2019
@eslint
Copy link
Contributor

@eslint eslint bot commented Sep 15, 2019

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

@mdjermanovic mdjermanovic commented Sep 15, 2019

I'll fix this.

@mdjermanovic mdjermanovic reopened this Sep 15, 2019
@eslint eslint bot closed this Oct 16, 2019
@eslint eslint bot added the auto closed label Oct 16, 2019
@eslint
Copy link
Contributor

@eslint eslint bot commented Oct 16, 2019

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.

@anikethsaha
Copy link
Member

@anikethsaha anikethsaha commented Apr 25, 2020

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

@mdjermanovic
Copy link
Member Author

@mdjermanovic mdjermanovic commented Apr 25, 2020

@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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants
You can’t perform that action at this time.