From f15cf3f289330b74242b8f51d90be5b137474adc Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Sat, 28 May 2022 13:14:22 +0800 Subject: [PATCH 1/4] fix --- .../babel-plugin-transform-new-target/src/index.ts | 10 ++++++++++ .../fixtures/general/function-duplicate-name/input.js | 11 +++++++++++ .../general/function-duplicate-name/output.js | 11 +++++++++++ 3 files changed, 32 insertions(+) create mode 100644 packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/input.js create mode 100644 packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/output.js diff --git a/packages/babel-plugin-transform-new-target/src/index.ts b/packages/babel-plugin-transform-new-target/src/index.ts index c3dce6a2c634..ca96a61c1475 100644 --- a/packages/babel-plugin-transform-new-target/src/index.ts +++ b/packages/babel-plugin-transform-new-target/src/index.ts @@ -49,6 +49,16 @@ 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 + let scope = path.scope; + while ( + scope?.hasBinding(node.id.name) && + !scope.bindingIdentifierEquals(node.id.name, node.id) + ) { + scope.rename(node.id.name); + scope = scope.parent; + } } const constructor = t.memberExpression( diff --git a/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/input.js b/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/input.js new file mode 100644 index 000000000000..023cc33d683c --- /dev/null +++ b/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/input.js @@ -0,0 +1,11 @@ +function Foo() { + var Foo = new.target; +} + +Foo.prototype.test = function() { + var Foo = new.target; +}; + +var Bar = function() { + var Bar = new.target; +}; diff --git a/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/output.js b/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/output.js new file mode 100644 index 000000000000..a7ae0bf16022 --- /dev/null +++ b/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/output.js @@ -0,0 +1,11 @@ +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; +}; From 1b280bdc9015d8ad423a9b2607a6f8e4d10e97ee Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Sat, 28 May 2022 13:35:21 +0800 Subject: [PATCH 2/4] fix --- .../babel-plugin-transform-new-target/src/index.ts | 1 + .../general/function-duplicate-name/input.js | 12 +++++++----- .../general/function-duplicate-name/output.js | 12 +++++++----- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/babel-plugin-transform-new-target/src/index.ts b/packages/babel-plugin-transform-new-target/src/index.ts index ca96a61c1475..aba243360ea4 100644 --- a/packages/babel-plugin-transform-new-target/src/index.ts +++ b/packages/babel-plugin-transform-new-target/src/index.ts @@ -53,6 +53,7 @@ export default declare(api => { // packages/babel-helper-create-class-features-plugin/src/fields.ts#L192 unshadow let scope = path.scope; while ( + scope !== func.parentPath.scope && scope?.hasBinding(node.id.name) && !scope.bindingIdentifierEquals(node.id.name, node.id) ) { diff --git a/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/input.js b/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/input.js index 023cc33d683c..795d867a237b 100644 --- a/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/input.js +++ b/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/input.js @@ -1,10 +1,12 @@ function Foo() { - var Foo = new.target; -} + function Foo() { + var Foo = new.target; + } -Foo.prototype.test = function() { - var Foo = new.target; -}; + Foo.prototype.test = function() { + var Foo = new.target; + }; +} var Bar = function() { var Bar = new.target; diff --git a/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/output.js b/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/output.js index a7ae0bf16022..2cc238626347 100644 --- a/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/output.js +++ b/packages/babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/output.js @@ -1,10 +1,12 @@ function Foo() { - var _Foo = this instanceof Foo ? this.constructor : void 0; -} + 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; -}; + 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; From 897bf07c0ea30d551724a69b77d3cf4737dbbc7a Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Sat, 28 May 2022 22:56:09 +0800 Subject: [PATCH 3/4] fix --- .../babel-plugin-transform-new-target/src/index.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/babel-plugin-transform-new-target/src/index.ts b/packages/babel-plugin-transform-new-target/src/index.ts index aba243360ea4..bce3e4be376a 100644 --- a/packages/babel-plugin-transform-new-target/src/index.ts +++ b/packages/babel-plugin-transform-new-target/src/index.ts @@ -52,12 +52,14 @@ export default declare(api => { } else { // packages/babel-helper-create-class-features-plugin/src/fields.ts#L192 unshadow let scope = path.scope; - while ( - scope !== func.parentPath.scope && - scope?.hasBinding(node.id.name) && - !scope.bindingIdentifierEquals(node.id.name, node.id) - ) { - scope.rename(node.id.name); + 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; } } From 917f05fe1343056a1703099dc15ad7812504765b Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 8 Jun 2022 08:14:10 +0800 Subject: [PATCH 4/4] review --- .../src/index.ts | 21 ++++++++++--------- .../general/class-properties-loose/output.js | 12 +++++------ .../general/class-properties/output.js | 12 +++++------ 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/babel-plugin-transform-new-target/src/index.ts b/packages/babel-plugin-transform-new-target/src/index.ts index bce3e4be376a..b3b573cb052c 100644 --- a/packages/babel-plugin-transform-new-target/src/index.ts +++ b/packages/babel-plugin-transform-new-target/src/index.ts @@ -47,6 +47,17 @@ export default declare(api => { path.replaceWith(scope.buildUndefinedNode()); return; } + + const constructor = t.memberExpression( + t.thisExpression(), + t.identifier("constructor"), + ); + + if (func.isClass()) { + path.replaceWith(constructor); + return; + } + if (!node.id) { node.id = scope.generateUidIdentifier("target"); } else { @@ -64,16 +75,6 @@ export default declare(api => { } } - const constructor = t.memberExpression( - t.thisExpression(), - t.identifier("constructor"), - ); - - if (func.isClass()) { - path.replaceWith(constructor); - return; - } - path.replaceWith( t.conditionalExpression( t.binaryExpression( diff --git a/packages/babel-plugin-transform-new-target/test/fixtures/general/class-properties-loose/output.js b/packages/babel-plugin-transform-new-target/test/fixtures/general/class-properties-loose/output.js index 19010c573c8f..18fa79130db7 100644 --- a/packages/babel-plugin-transform-new-target/test/fixtures/general/class-properties-loose/output.js +++ b/packages/babel-plugin-transform-new-target/test/fixtures/general/class-properties-loose/output.js @@ -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); } diff --git a/packages/babel-plugin-transform-new-target/test/fixtures/general/class-properties/output.js b/packages/babel-plugin-transform-new-target/test/fixtures/general/class-properties/output.js index b37a4560aef0..80074d51b61a 100644 --- a/packages/babel-plugin-transform-new-target/test/fixtures/general/class-properties/output.js +++ b/packages/babel-plugin-transform-new-target/test/fixtures/general/class-properties/output.js @@ -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); }