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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/babel-plugin-transform-new-target/src/index.ts
Expand Up @@ -49,6 +49,19 @@ export default declare(api => {
}
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.

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.

}

const constructor = t.memberExpression(
Expand Down
@@ -0,0 +1,13 @@
function Foo() {
function Foo() {
var Foo = new.target;
}

Foo.prototype.test = function() {
var Foo = new.target;
};
}

var Bar = function() {
var Bar = new.target;
};
@@ -0,0 +1,13 @@
function Foo() {
function Foo() {
var _Foo = this instanceof Foo ? this.constructor : void 0;
}

Foo.prototype.test = function _target() {
var Foo = this instanceof _target ? this.constructor : void 0;
};
}

var Bar = function _target2() {
var Bar = this instanceof _target2 ? this.constructor : void 0;
};