Skip to content

fix: Identifiers in the loop are not renamed#15361

Merged
nicolo-ribaudo merged 4 commits intobabel:mainfrom
liuxingbaoyu:fix-15308
Jan 26, 2023
Merged

fix: Identifiers in the loop are not renamed#15361
nicolo-ribaudo merged 4 commits intobabel:mainfrom
liuxingbaoyu:fix-15308

Conversation

@liuxingbaoyu
Copy link
Copy Markdown
Member

@liuxingbaoyu liuxingbaoyu commented Jan 24, 2023

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

After I tried some wrong methods, I found out that it is so simple. 😅

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix 🐛 A type of pull request used for our changelog categories i: regression labels Jan 24, 2023
@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Jan 24, 2023

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/53792/

Comment on lines +1 to +4
// `index` will be registered into `scope.globals`
type SomeLookup = {
[index: number]: any[];
};
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think this test is very good, but I don't know how to make it better.
Because after we change traverse it will not be registered in globals.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add a custom plugin to the test that registers a non-existing global? You can create a plugin.js file and add it as "plugins": ["./plugin.js"] in options.json

} else if (
headScope.parent.hasBinding(name) ||
blockScope.parent.hasGlobal(name) ||
(isProgramScope && varScope.hasGlobal(name))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since hasGlobal checks across all the parent scope, when can it happen that blockScope.parent.hasGlobal(name) is false but isProgramScope && varScope.hasGlobal(name) is true?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure, I actually copy-pasted it from here. 🤦‍♂️
Because I found the easiest way is to rename this identifier before wrapLoopBody.

if (varScope !== blockScope) {
for (const name of bindingNames) {
let newName = name;
if (
// We pass `noUids` true because, if `name` was a generated
// UID, it has been used to declare the current variable in
// a nested scope and thus we don't need to assume that it
// may be declared (but not registered yet) in an upper one.
blockScope.parent.hasBinding(name, { noUids: true }) ||
blockScope.parent.hasGlobal(name) ||
(isProgramScope && varScope.hasGlobal(name))
) {
newName = blockScope.generateUid(name);
blockScope.rename(name, newName);
}
blockScope.moveBindingTo(newName, varScope);
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Whops 😅
It looks like deleting the isProgramScope && varScope.hasGlobal(name) check also from that code still makes all the tests pass.

Comment on lines 209 to 211
let varScope = blockScope.getFunctionParent();
let isProgramScope = false;
if (!varScope) {
varScope = blockScope.getProgramParent();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

const varScope = blockScope.getFunctionParent() || blockScope.getProgramParent();

@nicolo-ribaudo nicolo-ribaudo merged commit abff244 into babel:main Jan 26, 2023
@JLHwung JLHwung mentioned this pull request Jan 27, 2023
1 task
@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 Apr 28, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

i: regression 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.

[Bug]: transform error when use two loops in one function with @babel/plugin-transform-block-scoping@7.20.9

4 participants