Skip to content

Commit

Permalink
fix: new.target with shadowed class name (#14611)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Jun 10, 2022
1 parent a5dacd9 commit facafc3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
20 changes: 17 additions & 3 deletions packages/babel-plugin-transform-new-target/src/index.ts
Expand Up @@ -47,9 +47,6 @@ export default declare(api => {
path.replaceWith(scope.buildUndefinedNode());
return;
}
if (!node.id) {
node.id = scope.generateUidIdentifier("target");
}

const constructor = t.memberExpression(
t.thisExpression(),
Expand All @@ -61,6 +58,23 @@ export default declare(api => {
return;
}

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;
}
}

path.replaceWith(
t.conditionalExpression(
t.binaryExpression(
Expand Down
Expand Up @@ -11,23 +11,23 @@ class Foo {
_newtarget;
};

this.Bar = (_class = class _target2 {
this.Bar = (_class = class {
constructor() {
this.q = this.constructor;
} // should not replace


}, _class.p = void 0, _class.p1 = class _target3 {
}, _class.p = void 0, _class.p1 = class {
constructor() {
this.constructor;
}

}, _class.p2 = new function _target4() {
this instanceof _target4 ? this.constructor : void 0;
}, _class.p2 = new function _target2() {
this instanceof _target2 ? this.constructor : void 0;
}(), _class.p3 = function () {
void 0;
}, _class.p4 = function _target5() {
this instanceof _target5 ? this.constructor : void 0;
}, _class.p4 = function _target3() {
this instanceof _target3 ? this.constructor : void 0;
}, _class);
}

Expand Down
Expand Up @@ -9,23 +9,23 @@ class Foo {
babelHelpers.defineProperty(this, "test2", function () {
_newtarget;
});
this.Bar = (_class = class _target2 {
this.Bar = (_class = class {
constructor() {
babelHelpers.defineProperty(this, "q", this.constructor);
} // should not replace


}, babelHelpers.defineProperty(_class, "p", void 0), babelHelpers.defineProperty(_class, "p1", class _target3 {
}, babelHelpers.defineProperty(_class, "p", void 0), babelHelpers.defineProperty(_class, "p1", class {
constructor() {
this.constructor;
}

}), babelHelpers.defineProperty(_class, "p2", new function _target4() {
this instanceof _target4 ? this.constructor : void 0;
}), babelHelpers.defineProperty(_class, "p2", new function _target2() {
this instanceof _target2 ? this.constructor : void 0;
}()), babelHelpers.defineProperty(_class, "p3", function () {
void 0;
}), babelHelpers.defineProperty(_class, "p4", function _target5() {
this instanceof _target5 ? this.constructor : void 0;
}), babelHelpers.defineProperty(_class, "p4", function _target3() {
this instanceof _target3 ? this.constructor : void 0;
}), _class);
}

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;
};

0 comments on commit facafc3

Please sign in to comment.