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: fix let logic in for-in and for-of loops in no-extra-parens #14011

Merged
merged 1 commit into from Jan 30, 2021

Conversation

mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Jan 18, 2021

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an 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:

Tell us about your environment

  • ESLint Version: v7.18.0
  • Node Version: v12.18.4
  • npm Version: 6.14.6

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, etc.) are you using?

default

Please show your full configuration:

Configuration
module.exports = {
  parserOptions: {
    ecmaVersion: 2015
  }
};

What did you do? Please include the actual source code causing the issue.

Online Demo

/* eslint no-extra-parens: error */

for ((let) in foo);

for ((let.a) of foo);

What did you expect to happen?

Per the ForInOfStatement specification, ForInStatement#left expression cannot start with let[, and ForOfStatement#left expression cannot start with let:

for ( [lookahead ≠ let [] LeftHandSideExpression[?Yield, ?Await] in Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]

for ( [lookahead ≠ let] LeftHandSideExpression[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]

So, I would expect:

  • 1 error for for ((let) in foo);
  • no errors for for ((let.a) of foo);

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

  • no errors for for ((let) in foo);
  • 1 error for for ((let.a) of foo);
  5:6  error  Unnecessary parentheses around expression  no-extra-parens

✖ 1 problem (1 error, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.

After the autofix:

/* eslint no-extra-parens: error */

for ((let) in foo);

for (let.a of foo);

for (let.a of foo); is indeed a syntax error in engines:

Uncaught SyntaxError: The left-hand side of a for-of loop may not start with 'let'.

Espree currently allows that, but it shouldn't. Reported acornjs/acorn#1009

What changes did you make? (Give an overview)

Split "ForInStatement, ForOfStatement"(node) into two functions, as they're already different enough, and fixed the logic about expressions starting with let identifier.

Is there anything you'd like reviewers to focus on?

This fix produces more warnings for for-in loops and fewer warnings for for-of loops.

This is similar to the previous fix for for loops #13981

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Jan 18, 2021
@btmills btmills merged commit c60e23f into master Jan 30, 2021
@btmills btmills deleted the noextraparens-forinforof-let branch January 30, 2021 04:04
@medianick
Copy link

This looks to have broken typescript-eslint; see typescript-eslint/typescript-eslint#2986

@mdjermanovic
Copy link
Member Author

It seems that @typescript-eslint/no-extra-parens rule should be updated, and that will fix the problem.

@mdjermanovic
Copy link
Member Author

@medianick

This looks to have broken typescript-eslint; see typescript-eslint/typescript-eslint#2986

Fixed in @typescript-eslint/eslint-plugin v4.15.0

This was referenced Mar 17, 2021
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jul 30, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jul 30, 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 rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants