-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Create additional scope when redeclaring array variable in for..of loop #8920
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...ges/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-redeclare/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
for (let o of arr) { | ||
const arr = o; | ||
} |
6 changes: 6 additions & 0 deletions
6
...es/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-redeclare/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
for (let _i = 0, _arr = arr; _i < _arr.length; _i++) { | ||
let o = _arr[_i]; | ||
{ | ||
const arr = o; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ages/babel-plugin-transform-for-of/test/fixtures/regression/redeclare-array-8913/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function f(...t) { | ||
for (let o of t) { | ||
const t = o; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ges/babel-plugin-transform-for-of/test/fixtures/regression/redeclare-array-8913/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function f(...t) { | ||
for (var _i = 0; _i < t.length; _i++) { | ||
let o = t[_i]; | ||
{ | ||
const t = o; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great effort, thanks so much, the main issues are
for (const {a, b} of arr) { const b = 4; }
block.body
it would be better to replace thepath.node.body
node, I thinkSomething along the lines of
I forget the exact return value of
path.getBindingIdentifiers()
so you might need to tweak that.Also, we should make sure this also adds the block wrapper for code like
since that would also collide. I think this might just work automatically with
getBindingIdentifiers
but please check if you can.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback!
As for your first suggestion, I suppose that comes down the scope of the PR. The original issue was caused by a reuse of the array identifier, not the iteration variable. This was causing the array lookup to use the wrong identifier when the inner variable was renamed.
There are a couple of other issues open which specifically deal with the iteration variables being redeclared, namely #8498 and #8839.
I'm rather new to this whole game, so I'm not sure whether it would be preferred to fix those issues in this PR as well, since it is similar, or just leave them to someone else. This PR has already been referenced in #8498 as a related solution. But I'll defer back to you on that.
As for your second point, the reason I used
block.body
was becauseblock
is guaranteed to be a block statement, whereaspath.node.body
is not. If there is some other reason this is preferred, then I suppose the code could be changed to the following:But this seems a bit underhanded to me.