From cc101de4deb4938a7c666e09a857fe9bcbb034bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 16 Apr 2023 14:36:09 +0200 Subject: [PATCH] Fix static accessors with subclasses --- .../src/transformer-2023-03.ts | 27 ++++++++++++++++--- .../static-public-this/exec.js | 12 +++++++++ .../static-public/output.js | 12 ++++----- .../undecorated-static-private/output.js | 8 +++--- .../undecorated-static-public/output.js | 12 ++++----- .../static-public-inherited/exec.js | 12 +++++++++ .../static-public-inherited/options.json | 3 +++ .../2023-03-accessors/static-public/output.js | 12 ++++----- .../undecorated-static-private/output.js | 8 +++--- .../undecorated-static-public/output.js | 12 ++++----- .../2023-03-misc/all-decorators/output.js | 4 +-- .../fixtures/2023-03-misc/this/options.json | 3 +++ 12 files changed, 87 insertions(+), 38 deletions(-) create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/static-public-this/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public-inherited/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public-inherited/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/this/options.json diff --git a/packages/babel-plugin-proposal-decorators/src/transformer-2023-03.ts b/packages/babel-plugin-proposal-decorators/src/transformer-2023-03.ts index 6f152f362632..5738f6d8112f 100644 --- a/packages/babel-plugin-proposal-decorators/src/transformer-2023-03.ts +++ b/packages/babel-plugin-proposal-decorators/src/transformer-2023-03.ts @@ -165,16 +165,21 @@ function generateClassProperty( } function addProxyAccessorsFor( + className: t.Identifier, element: NodePath, originalKey: t.PrivateName | t.Expression, targetKey: t.PrivateName, + version: DecoratorVersionKind, isComputed = false, ): void { const { static: isStatic } = element.node; + const thisArg = + version === "2023-03" && isStatic ? className : t.thisExpression(); + const getterBody = t.blockStatement([ t.returnStatement( - t.memberExpression(t.thisExpression(), t.cloneNode(targetKey)), + t.memberExpression(t.cloneNode(thisArg), t.cloneNode(targetKey)), ), ]); @@ -182,7 +187,7 @@ function addProxyAccessorsFor( t.expressionStatement( t.assignmentExpression( "=", - t.memberExpression(t.thisExpression(), t.cloneNode(targetKey)), + t.memberExpression(t.cloneNode(thisArg), t.cloneNode(targetKey)), t.identifier("v"), ), ), @@ -538,7 +543,14 @@ function transformClass( const newField = generateClassProperty(newId, valueNode, isStatic); const [newPath] = element.replaceWith(newField); - addProxyAccessorsFor(newPath, key, newId, computed); + addProxyAccessorsFor( + path.node.id, + newPath, + key, + newId, + version, + computed, + ); } } @@ -698,7 +710,14 @@ function transformClass( locals = [newFieldInitId, getId, setId]; } else { - addProxyAccessorsFor(newPath, key, newId, isComputed); + addProxyAccessorsFor( + path.node.id, + newPath, + key, + newId, + version, + isComputed, + ); locals = newFieldInitId; } } else if (kind === FIELD) { diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/static-public-this/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/static-public-this/exec.js new file mode 100644 index 000000000000..e981fac69393 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/static-public-this/exec.js @@ -0,0 +1,12 @@ +// https://github.com/tc39/proposal-decorators/issues/468 + +class A { + static accessor x; + + @(() => {}) static accessor y; +} + +class B extends A {} + +expect(() => B.x).not.toThrow(); +expect(() => B.y).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/static-public/output.js index b822ec929a9d..2a86080e3545 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/static-public/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/static-public/output.js @@ -3,22 +3,22 @@ const dec = () => {}; _computedKey = 'c'; class Foo { static get a() { - return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _A); + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _A); } static set a(v) { - babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _A, v); + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _A, v); } static get b() { - return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _B); + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _B); } static set b(v) { - babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _B, v); + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _B, v); } static get [_computedKey]() { - return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _C); + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _C); } static set [_computedKey](v) { - babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _C, v); + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _C, v); } } (() => { diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/undecorated-static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/undecorated-static-private/output.js index ab3a0e1b2678..a4d71f4cc5a1 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/undecorated-static-private/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/undecorated-static-private/output.js @@ -1,16 +1,16 @@ const dec = () => {}; class Foo {} function _get_a() { - return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _A); + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _A); } function _set_a(v) { - babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _A, v); + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _A, v); } function _get_b() { - return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _B); + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _B); } function _set_b(v) { - babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _B, v); + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _B, v); } var _b = { get: _get_b, diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/undecorated-static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/undecorated-static-public/output.js index 5041e9a95d6e..ea1636ad8321 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/undecorated-static-public/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors--to-es2015/undecorated-static-public/output.js @@ -1,22 +1,22 @@ const dec = () => {}; class Foo { static get a() { - return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _A); + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _A); } static set a(v) { - babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _A, v); + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _A, v); } static get b() { - return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _B); + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _B); } static set b(v) { - babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _B, v); + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _B, v); } static get ['c']() { - return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _C); + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _C); } static set ['c'](v) { - babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _C, v); + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _C, v); } } var _A = { diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public-inherited/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public-inherited/exec.js new file mode 100644 index 000000000000..e981fac69393 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public-inherited/exec.js @@ -0,0 +1,12 @@ +// https://github.com/tc39/proposal-decorators/issues/468 + +class A { + static accessor x; + + @(() => {}) static accessor y; +} + +class B extends A {} + +expect(() => B.x).not.toThrow(); +expect(() => B.y).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public-inherited/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public-inherited/options.json new file mode 100644 index 000000000000..f97653393918 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public-inherited/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public/output.js index fe6f5da9582b..9db071fbeee2 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/static-public/output.js @@ -8,23 +8,23 @@ class Foo { } static #A = _init_a(this); static get a() { - return this.#A; + return Foo.#A; } static set a(v) { - this.#A = v; + Foo.#A = v; } static #B = _init_b(this, 123); static get b() { - return this.#B; + return Foo.#B; } static set b(v) { - this.#B = v; + Foo.#B = v; } static #C = _init_computedKey(this, 456); static get [_computedKey]() { - return this.#C; + return Foo.#C; } static set [_computedKey](v) { - this.#C = v; + Foo.#C = v; } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/undecorated-static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/undecorated-static-private/output.js index 9268d8a99d07..7c864b31413f 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/undecorated-static-private/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/undecorated-static-private/output.js @@ -2,16 +2,16 @@ const dec = () => {}; class Foo { static #A; static get #a() { - return this.#A; + return Foo.#A; } static set #a(v) { - this.#A = v; + Foo.#A = v; } static #B = 123; static get #b() { - return this.#B; + return Foo.#B; } static set #b(v) { - this.#B = v; + Foo.#B = v; } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/undecorated-static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/undecorated-static-public/output.js index 32b5ee6911b8..dc2012f8012f 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/undecorated-static-public/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-accessors/undecorated-static-public/output.js @@ -2,23 +2,23 @@ const dec = () => {}; class Foo { static #A; static get a() { - return this.#A; + return Foo.#A; } static set a(v) { - this.#A = v; + Foo.#A = v; } static #B = 123; static get b() { - return this.#B; + return Foo.#B; } static set b(v) { - this.#B = v; + Foo.#B = v; } static #C = 456; static get ['c']() { - return this.#C; + return Foo.#C; } static set ['c'](v) { - this.#C = v; + Foo.#C = v; } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/all-decorators/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/all-decorators/output.js index 91cced17103e..91b5c479fb54 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/all-decorators/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/all-decorators/output.js @@ -41,10 +41,10 @@ new class extends babelHelpers.identity { static get k() {} static set l(v) {} static get m() { - return this.#C; + return Class.#C; } static set m(v) { - this.#C = v; + Class.#C = v; } set #r(v) { _set_r(this, v); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/this/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/this/options.json new file mode 100644 index 000000000000..f97653393918 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/this/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "16.11.0" +}