Navigation Menu

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

fix: new.target with shadowed class name #14611

Merged
merged 4 commits into from Jun 10, 2022

Conversation

liuxingbaoyu
Copy link
Member

@liuxingbaoyu liuxingbaoyu commented May 28, 2022

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

@babel-bot
Copy link
Collaborator

babel-bot commented May 28, 2022

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

@liuxingbaoyu liuxingbaoyu marked this pull request as draft May 28, 2022 06:18
@liuxingbaoyu liuxingbaoyu marked this pull request as ready for review May 28, 2022 14:56
@liuxingbaoyu liuxingbaoyu added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label May 29, 2022
Comment on lines 50 to 64
if (!node.id) {
node.id = scope.generateUidIdentifier("target");
} else {
// packages/babel-helper-create-class-features-plugin/src/fields.ts#L192 unshadow
let scope = path.scope;
const name = node.id.name;
while (scope !== func.parentPath.scope) {
if (
scope.hasOwnBinding(name) &&
!scope.bindingIdentifierEquals(name, node.id)
) {
scope.rename(name);
}
scope = scope.parent;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: they can be moved after the if (func.isClass()) branch.

if (!node.id) {
node.id = scope.generateUidIdentifier("target");
} else {
// packages/babel-helper-create-class-features-plugin/src/fields.ts#L192 unshadow
Copy link
Member

Choose a reason for hiding this comment

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

We could actually move this to a Scope#unshadow(name, targetAncestorScope) method, and use it here like

path.scope.unshadow?.(name, func.parentPath.scope);

It will only work when people update their @babel/traverse (usually by updating their @babel/core), but it's not a breaking change because when using older versions new.target will just continue to be broken.

Then, we can use scope.unshadow() in babel-helper-create-class-features-plugin starting from Babel 8 (or better, now with a fallback to the inline unshadow() function when !process.env.BABEL_8_BREAKING).

Copy link
Member Author

Choose a reason for hiding this comment

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

I feel a little unnecessary.
Because unshadow is only a few lines of code and may be different in different scenarios.
Another major reason is that our current Scope is a bit messy, eg binding is not always reliable.
Maybe we can do it in the future.

Copy link
Member

Choose a reason for hiding this comment

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

Oh ok fair enough, we can refactor if needed.

@nicolo-ribaudo nicolo-ribaudo changed the title fix: new.target with the duplicate name fix: new.target with shadowed class name Jun 10, 2022
@nicolo-ribaudo nicolo-ribaudo merged commit facafc3 into babel:main Jun 10, 2022
@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 Sep 10, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 10, 2022
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.

[Bug]: Right-Hand Side of 'instanceof' is not an Object
4 participants