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

Allow redeclaration of loop variable in body #11088

Merged
merged 1 commit into from Feb 11, 2020

Conversation

openorclose
Copy link
Contributor

Q                       A
Fixed Issues? Fixes #8913
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

The following code

for (let i of 'foo') {
  let i = 'a';
  console.log(i);
}

now gives the error message

/repl.js: Duplicate declaration "i"
  1 | for (let i of 'foo') {
> 2 |   let i = 'a';
    |       ^
  3 |   console.log(i);
  4 | }

on 7.8.4 repl link

when this is valid js code.

Fix

We check to see if the immediate body of the for-of loop contains any bindings to variables declared in the left side of the for-of loop.

If so, we wrap the original for-of body in a block, and then prepend the necessary declarations/assignments during transformation:

So the above code would transform into:

for (let _i = 0, _foo = 'foo'; _i < _foo.length; _i++) {
  let i = _foo[_i]; /// Line A
  {
    let i = 'a';
    console.log(i);
  }
}

Of note is that this extra block is necessary, and we cannot simply omit Line A in the above code to avoid creating the extra block, because we need to account for cases like:

for ( const [first, ...rest] of array) {
  const first = 1;
  // no redeclaration of rest
}

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thanks!

@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Feb 3, 2020
@openorclose
Copy link
Contributor Author

Thanks!

Thanks for the review!

Copy link
Member

@existentialism existentialism left a comment

Choose a reason for hiding this comment

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

Nice work @openorclose!

@nicolo-ribaudo nicolo-ribaudo merged commit 3907396 into babel:master Feb 11, 2020
rajasekarm pushed a commit to rajasekarm/babel that referenced this pull request Feb 17, 2020
@openorclose openorclose deleted the allow-loop-var-redcl branch March 1, 2020 16:17
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 1, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

for...of array optimization can introduce conflicting variables into the scope
4 participants