From f27049754adf0e226cbf7cab82411b70d545b645 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Mon, 19 Feb 2024 23:26:23 +0800 Subject: [PATCH] Reduce decorator static property size (#16287) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * improve * fix new test * fix test * add test * Apply suggestions from code review Co-authored-by: Nicolò Ribaudo * lint --------- Co-authored-by: Nicolò Ribaudo --- .../src/decorators.ts | 45 +++++++++------ .../babel-helpers/src/helpers-generated.ts | 4 +- .../src/helpers/applyDecs2311.ts | 56 ++++++++++++------- .../context-name/output.js | 24 ++++---- .../static-private/output.js | 14 ++--- .../static-public/output.js | 8 +-- .../2023-11-accessors/context-name/output.js | 24 ++++---- .../static-private/output.js | 14 ++--- .../2023-11-accessors/static-public/output.js | 8 +-- .../exec.js | 11 +++- .../input.js | 10 +++- .../output.js | 25 ++++++--- .../input.js | 10 +++- .../output.js | 21 +++++-- .../context-name/output.js | 20 +++---- .../static-private/output.js | 6 +- .../static-public/output.js | 8 +-- .../2023-11-fields/context-name/output.js | 20 +++---- .../2023-11-fields/static-private/output.js | 6 +- .../2023-11-fields/static-public/output.js | 8 +-- .../2023-11-misc/all-decorators/output.js | 14 ++--- 21 files changed, 209 insertions(+), 147 deletions(-) diff --git a/packages/babel-helper-create-class-features-plugin/src/decorators.ts b/packages/babel-helper-create-class-features-plugin/src/decorators.ts index 779695be2187..50c8cf7bac55 100644 --- a/packages/babel-helper-create-class-features-plugin/src/decorators.ts +++ b/packages/babel-helper-create-class-features-plugin/src/decorators.ts @@ -603,6 +603,7 @@ function extractElementLocalAssignments(decorationInfo: DecoratorInfo[]) { } function addCallAccessorsFor( + version: DecoratorVersionKind, element: NodePath, key: t.PrivateName, getId: t.Identifier, @@ -616,7 +617,12 @@ function addCallAccessorsFor( [], t.blockStatement([ t.returnStatement( - t.callExpression(t.cloneNode(getId), [t.thisExpression()]), + t.callExpression( + t.cloneNode(getId), + (process.env.BABEL_8_BREAKING || version === "2023-11") && isStatic + ? [] + : [t.thisExpression()], + ), ), ]), isStatic, @@ -630,10 +636,12 @@ function addCallAccessorsFor( [t.identifier("v")], t.blockStatement([ t.expressionStatement( - t.callExpression(t.cloneNode(setId), [ - t.thisExpression(), - t.identifier("v"), - ]), + t.callExpression( + t.cloneNode(setId), + (process.env.BABEL_8_BREAKING || version === "2023-11") && isStatic + ? [t.identifier("v")] + : [t.thisExpression(), t.identifier("v")], + ), ), ]), isStatic, @@ -1085,7 +1093,10 @@ function transformClass( if (kind === ACCESSOR) { const { value } = element.node as t.ClassAccessorProperty; - const params: t.Expression[] = [t.thisExpression()]; + const params: t.Expression[] = + (process.env.BABEL_8_BREAKING || version === "2023-11") && isStatic + ? [] + : [t.thisExpression()]; if (value) { params.push(t.cloneNode(value)); @@ -1112,7 +1123,7 @@ function transformClass( `set_${name}`, ); - addCallAccessorsFor(newPath, key, getId, setId, isStatic); + addCallAccessorsFor(version, newPath, key, getId, setId, isStatic); locals = [newFieldInitId, getId, setId]; } else { @@ -1135,12 +1146,13 @@ function transformClass( element as NodePath ).get("value"); - valuePath.replaceWith( - t.callExpression( - t.cloneNode(initId), - [t.thisExpression(), valuePath.node].filter(v => v), - ), - ); + const args: t.Expression[] = + (process.env.BABEL_8_BREAKING || version === "2023-11") && isStatic + ? [] + : [t.thisExpression()]; + if (valuePath.node) args.push(valuePath.node); + + valuePath.replaceWith(t.callExpression(t.cloneNode(initId), args)); locals = [initId]; @@ -1248,9 +1260,10 @@ function transformClass( `init_extra_${name}`, ); locals.push(initExtraId); - const initExtraCall = t.callExpression(t.cloneNode(initExtraId), [ - t.thisExpression(), - ]); + const initExtraCall = t.callExpression( + t.cloneNode(initExtraId), + isStatic ? [] : [t.thisExpression()], + ); if (!isStatic) { fieldInitializerExpressions.push(initExtraCall); } else { diff --git a/packages/babel-helpers/src/helpers-generated.ts b/packages/babel-helpers/src/helpers-generated.ts index 7205ea0bcb15..b56c7794b171 100644 --- a/packages/babel-helpers/src/helpers-generated.ts +++ b/packages/babel-helpers/src/helpers-generated.ts @@ -48,10 +48,10 @@ export default Object.freeze({ "7.21.0", 'import checkInRHS from"checkInRHS";import setFunctionName from"setFunctionName";import toPropertyKey from"toPropertyKey";export default function applyDecs2305(e,t,r,n,o,a){function i(e,t,r){return function(n,o){return r&&r(n),e[t].call(n,o)}}function c(e,t){for(var r=0;r=0;j-=r?2:1){var D=v[j],E=r?v[j-1]:void 0,I={},O={kind:["field","accessor","method","getter","setter","class"][o],name:n,metadata:a,addInitializer:function(e,t){if(e.v)throw new Error("attempted to call addInitializer after decoration was finished");s(t,"An initializer","be",!0),c.push(t)}.bind(null,I)};try{if(b)(y=s(D.call(E,P,O),"class decorators","return"))&&(P=y);else{var k,F;O.static=l,O.private=f,f?2===o?k=function(e){return m(e),w.value}:(o<4&&(k=i(w,"get",m)),3!==o&&(F=i(w,"set",m))):(k=function(e){return e[n]},(o<2||4===o)&&(F=function(e,t){e[n]=t}));var N=O.access={has:f?h.bind():function(e){return n in e}};if(k&&(N.get=k),F&&(N.set=F),P=D.call(E,d?{get:w.get,set:w.set}:w[A],O),d){if("object"==typeof P&&P)(y=s(P.get,"accessor.get"))&&(w.get=y),(y=s(P.set,"accessor.set"))&&(w.set=y),(y=s(P.init,"accessor.init"))&&S.push(y);else if(void 0!==P)throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0")}else s(P,(p?"field":"method")+" decorators","return")&&(p?S.push(P):w[A]=P)}}finally{I.v=!0}}return(p||d)&&u.push((function(e,t){for(var r=S.length-1;r>=0;r--)t=S[r].call(e,t);return t})),p||b||(f?d?u.push(i(w,"get"),i(w,"set")):u.push(2===o?w[A]:i.call.bind(w[A])):Object.defineProperty(e,n,w)),P}function u(e,t){return Object.defineProperty(e,Symbol.metadata||Symbol.for("Symbol.metadata"),{configurable:!0,enumerable:!0,value:t})}if(arguments.length>=6)var l=a[Symbol.metadata||Symbol.for("Symbol.metadata")];var f=Object.create(null==l?null:l),p=function(e,t,r,n){var o,a,i=[],s=function(t){return checkInRHS(t)===e},u=new Map;function l(e){e&&i.push(c.bind(null,e))}for(var f=0;f3,y=16&d,v=!!(8&d),g=0==(d&=7),b=h+"/"+v;if(!g&&!m){var w=u.get(b);if(!0===w||3===w&&4!==d||4===w&&3!==d)throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: "+h);u.set(b,!(d>2)||d)}applyDec(v?e:e.prototype,p,y,m?"#"+h:toPropertyKey(h),d,n,v?a=a||[]:o=o||[],i,v,m,g,1===d,v&&m?s:r)}}return l(o),l(a),i}(e,t,o,f);return r.length||u(e,f),{e:p,get c(){var t=[];return r.length&&[u(applyDec(e,[r],n,e.name,5,f,t),f),c.bind(null,t,e)]}}}', ), - // size: 2899, gzip size: 1475 + // size: 2917, gzip size: 1487 applyDecs2311: helper( "7.23.0", - 'import checkInRHS from"checkInRHS";import setFunctionName from"setFunctionName";import toPropertyKey from"toPropertyKey";export default function applyDecs2311(e,t,n,r,o,i){var a,c,u=Symbol.metadata||Symbol.for("Symbol.metadata"),s=Object.defineProperty,l=Object.create,f=[l(null),l(null)],p=n.length;function d(e,t,n,r){for(var o=0;o=0;O-=n?2:1){var z=b[O],H=n?b[O-1]:void 0,K={},R={kind:["field","accessor","method","getter","setter","class"][o],name:r,metadata:a,addInitializer:function(e,t){if(e.v)throw new Error("attempted to call addInitializer after decoration was finished");m(t,"An initializer","be",!0),i.push(t)}.bind(null,K)};if(D)c=z.call(H,N,R),K.v=1,m(c,"class decorators","return")&&(N=c);else if(R.static=l,R.private=p,c=R.access={has:p?v.bind():function(e){return r in e}},j||(c.get=p?E?function(e){return g(e),P.value}:I("get",g):function(e){return e[r]}),E||S||(c.set=p?I("set",g):function(e,t){e[r]=t}),N=z.call(H,y?{get:P.get,set:P.set}:P[F],R),K.v=1,y){if("object"==typeof N&&N)(c=m(N.get,"accessor.get"))&&(P.get=c),(c=m(N.set,"accessor.set"))&&(P.set=c),(c=m(N.init,"accessor.init"))&&k.unshift(c);else if(void 0!==N)throw new TypeError("accessor decorators must return an object with get, set, or init properties or undefined")}else m(N,(h?"field":"method")+" decorators","return")&&(h?k.unshift(N):P[F]=N)}return o<2&&u.push(d.bind(null,k,1),d.bind(null,i,0)),h||D||(p?y?u.splice(-1,0,I("get"),I("set")):u.push(E?P[F]:d.call.bind(P[F])):s(e,r,P)),N}function h(e){return s(e,u,{configurable:!0,enumerable:!0,value:a})}return void 0!==i&&(a=i[u]),a=l(null==a?null:a),c=function(){var n,r,i=[];function a(e){e&&i.push(d.bind(null,e,0))}for(var c=0;c=0;O-=n?2:1){var z=b[O],H=n?b[O-1]:void 0,K={},R={kind:["field","accessor","method","getter","setter","class"][o],name:r,metadata:a,addInitializer:function(e,t){if(e.v)throw new Error("attempted to call addInitializer after decoration was finished");m(t,"An initializer","be",!0),i.push(t)}.bind(null,K)};if(D)c=z.call(H,N,R),K.v=1,m(c,"class decorators","return")&&(N=c);else if(R.static=f,R.private=p,c=R.access={has:p?v.bind():function(e){return r in e}},j||(c.get=p?E?function(e){return g(e),P.value}:I("get",0,g):function(e){return e[r]}),E||S||(c.set=p?I("set",0,g):function(e,t){e[r]=t}),N=z.call(H,y?{get:P.get,set:P.set}:P[F],R),K.v=1,y){if("object"==typeof N&&N)(c=m(N.get,"accessor.get"))&&(P.get=c),(c=m(N.set,"accessor.set"))&&(P.set=c),(c=m(N.init,"accessor.init"))&&k.unshift(c);else if(void 0!==N)throw new TypeError("accessor decorators must return an object with get, set, or init properties or undefined")}else m(N,(h?"field":"method")+" decorators","return")&&(h?k.unshift(N):P[F]=N)}return o<2&&u.push(d(k,f,1),d(i,f,0)),h||D||(p?y?u.splice(-1,0,I("get",f),I("set",f)):u.push(E?P[F]:m.call.bind(P[F])):s(e,r,P)),N}function h(e){return s(e,u,{configurable:!0,enumerable:!0,value:a})}return void 0!==i&&(a=i[u]),a=f(null==a?null:a),c=function(){var n,r,i=[];function a(e){e&&i.push(d(e))}for(var c=0;c babelHelpers.assertClassBrand(o, _Foo, _B)._, (o, v) => _B._ = babelHelpers.assertClassBrand(o, _Foo, v)], [dec, 9, "b"], [dec, 9, "c"], [dec, 9, 0], [dec, 9, 1], [dec, 9, 2n], [dec, 9, 3n], [dec, 9, _computedKey]], []).e; var _A = { - _: _init_a(_Foo) + _: _init_a() }; var _B = { - _: (_init_extra_a(_Foo), _init_a2(_Foo)) + _: (_init_extra_a(), _init_a2()) }; var _C = { - _: (_init_extra_a2(_Foo), _init_computedKey(_Foo)) + _: (_init_extra_a2(), _init_computedKey()) }; var _D = { - _: (_init_extra_computedKey(_Foo), _init_computedKey2(_Foo)) + _: (_init_extra_computedKey(), _init_computedKey2()) }; var _E = { - _: (_init_extra_computedKey2(_Foo), _init_computedKey3(_Foo)) + _: (_init_extra_computedKey2(), _init_computedKey3()) }; var _F = { - _: (_init_extra_computedKey3(_Foo), _init_computedKey4(_Foo)) + _: (_init_extra_computedKey3(), _init_computedKey4()) }; var _G = { - _: (_init_extra_computedKey4(_Foo), _init_computedKey5(_Foo)) + _: (_init_extra_computedKey4(), _init_computedKey5()) }; var _H = { - _: (_init_extra_computedKey5(_Foo), _init_computedKey6(_Foo)) + _: (_init_extra_computedKey5(), _init_computedKey6()) }; var _I = { - _: (_init_extra_computedKey6(_Foo), _init_computedKey7(_Foo)) + _: (_init_extra_computedKey6(), _init_computedKey7()) }; -_init_extra_computedKey7(_Foo); +_init_extra_computedKey7(); expect(logs).toStrictEqual(["computing f", "calling toPrimitive", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors--to-es2015/static-private/output.js index d2831d660bda..8f448de033d4 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors--to-es2015/static-private/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors--to-es2015/static-private/output.js @@ -3,22 +3,22 @@ const dec = () => {}; class Foo {} _Foo = Foo; function _set_a2(v) { - _set_a(this, v); + _set_a(v); } function _get_a2() { - return _get_a(this); + return _get_a(); } function _set_b2(v) { - _set_b(this, v); + _set_b(v); } function _get_b2() { - return _get_b(this); + return _get_b(); } [_init_a, _get_a, _set_a, _init_extra_a, _init_b, _get_b, _set_b, _init_extra_b] = babelHelpers.applyDecs2311(_Foo, [[dec, 9, "a", o => babelHelpers.assertClassBrand(o, _Foo, _A)._, (o, v) => _A._ = babelHelpers.assertClassBrand(o, _Foo, v)], [dec, 9, "b", o => babelHelpers.assertClassBrand(o, _Foo, _B)._, (o, v) => _B._ = babelHelpers.assertClassBrand(o, _Foo, v)]], []).e; var _A = { - _: _init_a(_Foo) + _: _init_a() }; var _B = { - _: (_init_extra_a(_Foo), _init_b(_Foo, 123)) + _: (_init_extra_a(), _init_b(123)) }; -_init_extra_b(_Foo); +_init_extra_b(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors--to-es2015/static-public/output.js index d601aec19a46..4cca097154c1 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors--to-es2015/static-public/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors--to-es2015/static-public/output.js @@ -23,12 +23,12 @@ class Foo { _Foo = Foo; [_init_a, _init_extra_a, _init_b, _init_extra_b, _init_computedKey, _init_extra_computedKey] = babelHelpers.applyDecs2311(_Foo, [[dec, 9, "a"], [dec, 9, "b"], [dec, 9, 'c']], []).e; var _A = { - _: _init_a(_Foo) + _: _init_a() }; var _B = { - _: (_init_extra_a(_Foo), _init_b(_Foo, 123)) + _: (_init_extra_a(), _init_b(123)) }; var _C = { - _: (_init_extra_b(_Foo), _init_computedKey(_Foo, 456)) + _: (_init_extra_b(), _init_computedKey(456)) }; -_init_extra_computedKey(_Foo); +_init_extra_computedKey(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/context-name/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/context-name/output.js index b3dda8fda8e3..297ff988506e 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/context-name/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/context-name/output.js @@ -14,63 +14,63 @@ class Foo { static { [_init_a, _init_extra_a, _init_a2, _get_a, _set_a, _init_extra_a2, _init_computedKey, _init_extra_computedKey, _init_computedKey2, _init_extra_computedKey2, _init_computedKey3, _init_extra_computedKey3, _init_computedKey4, _init_extra_computedKey4, _init_computedKey5, _init_extra_computedKey5, _init_computedKey6, _init_extra_computedKey6, _init_computedKey7, _init_extra_computedKey7] = babelHelpers.applyDecs2311(this, [[dec, 9, "a"], [dec, 9, "a", o => o.#B, (o, v) => o.#B = v], [dec, 9, "b"], [dec, 9, "c"], [dec, 9, 0], [dec, 9, 1], [dec, 9, 2n], [dec, 9, 3n], [dec, 9, _computedKey]], []).e; } - static #A = _init_a(this); + static #A = _init_a(); static get a() { return Foo.#A; } static set a(v) { Foo.#A = v; } - static #B = (_init_extra_a(this), _init_a2(this)); + static #B = (_init_extra_a(), _init_a2()); static set #a(v) { - _set_a(this, v); + _set_a(v); } static get #a() { - return _get_a(this); + return _get_a(); } - static #C = (_init_extra_a2(this), _init_computedKey(this)); + static #C = (_init_extra_a2(), _init_computedKey()); static get "b"() { return Foo.#C; } static set "b"(v) { Foo.#C = v; } - static #D = (_init_extra_computedKey(this), _init_computedKey2(this)); + static #D = (_init_extra_computedKey(), _init_computedKey2()); static get ["c"]() { return Foo.#D; } static set ["c"](v) { Foo.#D = v; } - static #E = (_init_extra_computedKey2(this), _init_computedKey3(this)); + static #E = (_init_extra_computedKey2(), _init_computedKey3()); static get 0() { return Foo.#E; } static set 0(v) { Foo.#E = v; } - static #F = (_init_extra_computedKey3(this), _init_computedKey4(this)); + static #F = (_init_extra_computedKey3(), _init_computedKey4()); static get [1]() { return Foo.#F; } static set [1](v) { Foo.#F = v; } - static #G = (_init_extra_computedKey4(this), _init_computedKey5(this)); + static #G = (_init_extra_computedKey4(), _init_computedKey5()); static get 2n() { return Foo.#G; } static set 2n(v) { Foo.#G = v; } - static #H = (_init_extra_computedKey5(this), _init_computedKey6(this)); + static #H = (_init_extra_computedKey5(), _init_computedKey6()); static get [3n]() { return Foo.#H; } static set [3n](v) { Foo.#H = v; } - static #I = (_init_extra_computedKey6(this), _init_computedKey7(this)); + static #I = (_init_extra_computedKey6(), _init_computedKey7()); static get [_computedKey]() { return Foo.#I; } @@ -78,7 +78,7 @@ class Foo { Foo.#I = v; } static { - _init_extra_computedKey7(this); + _init_extra_computedKey7(); } } expect(logs).toStrictEqual(["computing f", "calling toPrimitive", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/static-private/output.js index ff0b7b609ee7..fd5caedaa32e 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/static-private/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/static-private/output.js @@ -4,21 +4,21 @@ class Foo { static { [_init_a, _get_a, _set_a, _init_extra_a, _init_b, _get_b, _set_b, _init_extra_b] = babelHelpers.applyDecs2311(this, [[dec, 9, "a", o => o.#A, (o, v) => o.#A = v], [dec, 9, "b", o => o.#B, (o, v) => o.#B = v]], []).e; } - static #A = _init_a(this); + static #A = _init_a(); static set #a(v) { - _set_a(this, v); + _set_a(v); } static get #a() { - return _get_a(this); + return _get_a(); } - static #B = (_init_extra_a(this), _init_b(this, 123)); + static #B = (_init_extra_a(), _init_b(123)); static set #b(v) { - _set_b(this, v); + _set_b(v); } static get #b() { - return _get_b(this); + return _get_b(); } static { - _init_extra_b(this); + _init_extra_b(); } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/static-public/output.js index 5f9287bb640e..a48eaeeb0f98 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/static-public/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-accessors/static-public/output.js @@ -4,21 +4,21 @@ class Foo { static { [_init_a, _init_extra_a, _init_b, _init_extra_b, _init_computedKey, _init_extra_computedKey] = babelHelpers.applyDecs2311(this, [[dec, 9, "a"], [dec, 9, "b"], [dec, 9, 'c']], []).e; } - static #A = _init_a(this); + static #A = _init_a(); static get a() { return Foo.#A; } static set a(v) { Foo.#A = v; } - static #B = (_init_extra_a(this), _init_b(this, 123)); + static #B = (_init_extra_a(), _init_b(123)); static get b() { return Foo.#B; } static set b(v) { Foo.#B = v; } - static #C = (_init_extra_b(this), _init_computedKey(this, 456)); + static #C = (_init_extra_b(), _init_computedKey(456)); static get ['c']() { return Foo.#C; } @@ -26,6 +26,6 @@ class Foo { Foo.#C = v; } static { - _init_extra_computedKey(this); + _init_extra_computedKey(); } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/exec.js index 166509622a3e..c9be25950ca3 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/exec.js @@ -1,10 +1,14 @@ -let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis; +let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis, classThis; -function dec(Klass) { +function dec(Klass, context) { original = Klass; replaced = class extends Klass {}; + context.addInitializer(function () { + classThis = this; + }) + return replaced; } @@ -19,7 +23,7 @@ function captureInitializerThis(callback) { @dec class Foo { @(captureInitializerThis(v => accessorThis = v)) - static accessor; + static accessor accessor; @(captureInitializerThis(v => getterThis = v)) static get getter() {}; @(captureInitializerThis(v => setterThis = v)) @@ -37,3 +41,4 @@ expect(methodThis).toBe(original); expect(accessorThis).toBe(replaced); expect(propertyThis).toBe(replaced); +expect(classThis).toBe(replaced); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/input.js index 15662500b473..d6335133be0e 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/input.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/input.js @@ -1,10 +1,14 @@ -let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis; +let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis, classThis; -function dec(Klass) { +function dec(Klass, context) { original = Klass; replaced = class extends Klass {}; + context.addInitializer(function () { + classThis = this; + }) + return replaced; } @@ -19,7 +23,7 @@ function captureInitializerThis(callback) { @dec class Foo { @(captureInitializerThis(v => accessorThis = v)) - static accessor; + static accessor accessor; @(captureInitializerThis(v => getterThis = v)) static get getter() {}; @(captureInitializerThis(v => setterThis = v)) diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/output.js index 7887215436fd..fae56354b21e 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes--to-es2015/replacement-static-decorator-initializer-this/output.js @@ -1,8 +1,11 @@ -var _initStatic, _initClass, _accessorDecs, _init_accessor, _init_extra_accessor, _getterDecs, _setterDecs, _methodDecs, _propertyDecs, _init_property, _init_extra_property, _temp; -let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis; -function dec(Klass) { +var _initStatic, _initClass, _accessorDecs, _init_accessor, _init_extra_accessor, _getterDecs, _setterDecs, _methodDecs, _propertyDecs, _init_property, _init_extra_property, _A, _temp; +let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis, classThis; +function dec(Klass, context) { original = Klass; replaced = class extends Klass {}; + context.addInitializer(function () { + classThis = this; + }); return replaced; } function captureInitializerThis(callback) { @@ -18,14 +21,20 @@ _setterDecs = captureInitializerThis(v => setterThis = v); _methodDecs = captureInitializerThis(v => methodThis = v); _propertyDecs = captureInitializerThis(v => propertyThis = v); let _Foo; -new (_temp = class extends babelHelpers.identity { +new (_A = /*#__PURE__*/new WeakMap(), (_temp = class extends babelHelpers.identity { constructor() { - (super(_Foo), babelHelpers.defineProperty(this, "accessor", _init_accessor(this)), babelHelpers.defineProperty(this, "property", (_init_extra_accessor(this), _init_property(this)))), (() => { - _init_extra_property(this); + (super(_Foo), babelHelpers.classPrivateFieldInitSpec(this, _A, _init_accessor()), babelHelpers.defineProperty(this, "property", (_init_extra_accessor(), _init_property()))), (() => { + _init_extra_property(); })(), _initClass(); } }, (_Foo2 => { class Foo { + static get accessor() { + return babelHelpers.classPrivateFieldGet2(Foo, _A); + } + static set accessor(v) { + babelHelpers.classPrivateFieldSet2(Foo, _A, v); + } static get getter() {} static set setter(_) {} static method() {} @@ -35,7 +44,7 @@ new (_temp = class extends babelHelpers.identity { ({ e: [_init_accessor, _init_extra_accessor, _init_property, _init_extra_property, _initStatic], c: [_Foo, _initClass] - } = babelHelpers.applyDecs2311(_Foo2, [[_getterDecs, 11, "getter"], [_setterDecs, 12, "setter"], [_methodDecs, 10, "method"], [_accessorDecs, 8, "accessor"], [_propertyDecs, 8, "property"]], [dec])); + } = babelHelpers.applyDecs2311(_Foo2, [[_accessorDecs, 9, "accessor"], [_getterDecs, 11, "getter"], [_setterDecs, 12, "setter"], [_methodDecs, 10, "method"], [_propertyDecs, 8, "property"]], [dec])); _initStatic(_Foo2); })(); -})(), _temp)(); +})(), _temp))(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes/replacement-static-decorator-initializer-this/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes/replacement-static-decorator-initializer-this/input.js index 15662500b473..d6335133be0e 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes/replacement-static-decorator-initializer-this/input.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes/replacement-static-decorator-initializer-this/input.js @@ -1,10 +1,14 @@ -let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis; +let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis, classThis; -function dec(Klass) { +function dec(Klass, context) { original = Klass; replaced = class extends Klass {}; + context.addInitializer(function () { + classThis = this; + }) + return replaced; } @@ -19,7 +23,7 @@ function captureInitializerThis(callback) { @dec class Foo { @(captureInitializerThis(v => accessorThis = v)) - static accessor; + static accessor accessor; @(captureInitializerThis(v => getterThis = v)) static get getter() {}; @(captureInitializerThis(v => setterThis = v)) diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes/replacement-static-decorator-initializer-this/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes/replacement-static-decorator-initializer-this/output.js index 7cc04568b288..b8d1119bc02b 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes/replacement-static-decorator-initializer-this/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-classes/replacement-static-decorator-initializer-this/output.js @@ -1,8 +1,11 @@ var _initStatic, _initClass, _accessorDecs, _init_accessor, _init_extra_accessor, _getterDecs, _setterDecs, _methodDecs, _propertyDecs, _init_property, _init_extra_property; -let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis; -function dec(Klass) { +let original, replaced, accessorThis, getterThis, setterThis, methodThis, propertyThis, classThis; +function dec(Klass, context) { original = Klass; replaced = class extends Klass {}; + context.addInitializer(function () { + classThis = this; + }); return replaced; } function captureInitializerThis(callback) { @@ -25,19 +28,25 @@ new class extends babelHelpers.identity { ({ e: [_init_accessor, _init_extra_accessor, _init_property, _init_extra_property, _initStatic], c: [_Foo, _initClass] - } = babelHelpers.applyDecs2311(this, [[_getterDecs, 11, "getter"], [_setterDecs, 12, "setter"], [_methodDecs, 10, "method"], [_accessorDecs, 8, "accessor"], [_propertyDecs, 8, "property"]], [dec])); + } = babelHelpers.applyDecs2311(this, [[_accessorDecs, 9, "accessor"], [_getterDecs, 11, "getter"], [_setterDecs, 12, "setter"], [_methodDecs, 10, "method"], [_propertyDecs, 8, "property"]], [dec])); _initStatic(this); } + static get accessor() { + return Foo.#A; + } + static set accessor(v) { + Foo.#A = v; + } static get getter() {} static set setter(_) {} static method() {} } } - accessor = _init_accessor(this); - property = (_init_extra_accessor(this), _init_property(this)); + #A = _init_accessor(); + property = (_init_extra_accessor(), _init_property()); constructor() { super(_Foo), (() => { - _init_extra_property(this); + _init_extra_property(); })(), _initClass(); } }(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/context-name/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/context-name/output.js index f997bb9e4ab4..843b8d45e5bf 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/context-name/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/context-name/output.js @@ -13,16 +13,16 @@ _computedKey = babelHelpers.toPropertyKey(f()); class Foo {} _Foo = Foo; [_init_a, _init_extra_a, _init_a2, _init_extra_a2, _init_computedKey, _init_extra_computedKey, _init_computedKey2, _init_extra_computedKey2, _init_computedKey3, _init_extra_computedKey3, _init_computedKey4, _init_extra_computedKey4, _init_computedKey5, _init_extra_computedKey5, _init_computedKey6, _init_extra_computedKey6, _init_computedKey7, _init_extra_computedKey7] = babelHelpers.applyDecs2311(_Foo, [[dec, 8, "a"], [dec, 8, "a", o => babelHelpers.assertClassBrand(o, _Foo, _a)._, (o, v) => _a._ = babelHelpers.assertClassBrand(o, _Foo, v)], [dec, 8, "b"], [dec, 8, "c"], [dec, 8, 0], [dec, 8, 1], [dec, 8, 2n], [dec, 8, 3n], [dec, 8, _computedKey]], []).e; -babelHelpers.defineProperty(Foo, "a", _init_a(_Foo)); +babelHelpers.defineProperty(Foo, "a", _init_a()); var _a = { - _: (_init_extra_a(_Foo), _init_a2(_Foo)) + _: (_init_extra_a(), _init_a2()) }; -babelHelpers.defineProperty(Foo, "b", (_init_extra_a2(_Foo), _init_computedKey(_Foo))); -babelHelpers.defineProperty(Foo, "c", (_init_extra_computedKey(_Foo), _init_computedKey2(_Foo))); -babelHelpers.defineProperty(Foo, 0, (_init_extra_computedKey2(_Foo), _init_computedKey3(_Foo))); -babelHelpers.defineProperty(Foo, 1, (_init_extra_computedKey3(_Foo), _init_computedKey4(_Foo))); -babelHelpers.defineProperty(Foo, 2n, (_init_extra_computedKey4(_Foo), _init_computedKey5(_Foo))); -babelHelpers.defineProperty(Foo, 3n, (_init_extra_computedKey5(_Foo), _init_computedKey6(_Foo))); -babelHelpers.defineProperty(Foo, _computedKey, (_init_extra_computedKey6(_Foo), _init_computedKey7(_Foo))); -_init_extra_computedKey7(_Foo); +babelHelpers.defineProperty(Foo, "b", (_init_extra_a2(), _init_computedKey())); +babelHelpers.defineProperty(Foo, "c", (_init_extra_computedKey(), _init_computedKey2())); +babelHelpers.defineProperty(Foo, 0, (_init_extra_computedKey2(), _init_computedKey3())); +babelHelpers.defineProperty(Foo, 1, (_init_extra_computedKey3(), _init_computedKey4())); +babelHelpers.defineProperty(Foo, 2n, (_init_extra_computedKey4(), _init_computedKey5())); +babelHelpers.defineProperty(Foo, 3n, (_init_extra_computedKey5(), _init_computedKey6())); +babelHelpers.defineProperty(Foo, _computedKey, (_init_extra_computedKey6(), _init_computedKey7())); +_init_extra_computedKey7(); expect(logs).toStrictEqual(["computing f", "calling toPrimitive", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/static-private/output.js index 69f7c7fc62f3..e7cf09e5b939 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/static-private/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/static-private/output.js @@ -4,9 +4,9 @@ class Foo {} _Foo = Foo; [_init_a, _init_extra_a, _init_b, _init_extra_b] = babelHelpers.applyDecs2311(_Foo, [[dec, 8, "a", o => babelHelpers.assertClassBrand(o, _Foo, _a)._, (o, v) => _a._ = babelHelpers.assertClassBrand(o, _Foo, v)], [dec, 8, "b", o => babelHelpers.assertClassBrand(o, _Foo, _b)._, (o, v) => _b._ = babelHelpers.assertClassBrand(o, _Foo, v)]], []).e; var _a = { - _: _init_a(_Foo) + _: _init_a() }; var _b = { - _: (_init_extra_a(_Foo), _init_b(_Foo, 123)) + _: (_init_extra_a(), _init_b(123)) }; -_init_extra_b(_Foo); +_init_extra_b(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/static-public/output.js index 344c107f885d..617834e616c3 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/static-public/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields--to-es2015/static-public/output.js @@ -3,7 +3,7 @@ const dec = () => {}; class Foo {} _Foo = Foo; [_init_a, _init_extra_a, _init_b, _init_extra_b, _init_computedKey, _init_extra_computedKey] = babelHelpers.applyDecs2311(_Foo, [[dec, 8, "a"], [dec, 8, "b"], [dec, 8, 'c']], []).e; -babelHelpers.defineProperty(Foo, "a", _init_a(_Foo)); -babelHelpers.defineProperty(Foo, "b", (_init_extra_a(_Foo), _init_b(_Foo, 123))); -babelHelpers.defineProperty(Foo, 'c', (_init_extra_b(_Foo), _init_computedKey(_Foo, 456))); -_init_extra_computedKey(_Foo); +babelHelpers.defineProperty(Foo, "a", _init_a()); +babelHelpers.defineProperty(Foo, "b", (_init_extra_a(), _init_b(123))); +babelHelpers.defineProperty(Foo, 'c', (_init_extra_b(), _init_computedKey(456))); +_init_extra_computedKey(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/context-name/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/context-name/output.js index 9e44dabc0118..2f25c8330c87 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/context-name/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/context-name/output.js @@ -14,17 +14,17 @@ class Foo { static { [_init_a, _init_extra_a, _init_a2, _init_extra_a2, _init_computedKey, _init_extra_computedKey, _init_computedKey2, _init_extra_computedKey2, _init_computedKey3, _init_extra_computedKey3, _init_computedKey4, _init_extra_computedKey4, _init_computedKey5, _init_extra_computedKey5, _init_computedKey6, _init_extra_computedKey6, _init_computedKey7, _init_extra_computedKey7] = babelHelpers.applyDecs2311(this, [[dec, 8, "a"], [dec, 8, "a", o => o.#a, (o, v) => o.#a = v], [dec, 8, "b"], [dec, 8, "c"], [dec, 8, 0], [dec, 8, 1], [dec, 8, 2n], [dec, 8, 3n], [dec, 8, _computedKey]], []).e; } - static a = _init_a(this); - static #a = (_init_extra_a(this), _init_a2(this)); - static "b" = (_init_extra_a2(this), _init_computedKey(this)); - static ["c"] = (_init_extra_computedKey(this), _init_computedKey2(this)); - static 0 = (_init_extra_computedKey2(this), _init_computedKey3(this)); - static [1] = (_init_extra_computedKey3(this), _init_computedKey4(this)); - static 2n = (_init_extra_computedKey4(this), _init_computedKey5(this)); - static [3n] = (_init_extra_computedKey5(this), _init_computedKey6(this)); - static [_computedKey] = (_init_extra_computedKey6(this), _init_computedKey7(this)); + static a = _init_a(); + static #a = (_init_extra_a(), _init_a2()); + static "b" = (_init_extra_a2(), _init_computedKey()); + static ["c"] = (_init_extra_computedKey(), _init_computedKey2()); + static 0 = (_init_extra_computedKey2(), _init_computedKey3()); + static [1] = (_init_extra_computedKey3(), _init_computedKey4()); + static 2n = (_init_extra_computedKey4(), _init_computedKey5()); + static [3n] = (_init_extra_computedKey5(), _init_computedKey6()); + static [_computedKey] = (_init_extra_computedKey6(), _init_computedKey7()); static { - _init_extra_computedKey7(this); + _init_extra_computedKey7(); } } expect(logs).toStrictEqual(["computing f", "calling toPrimitive", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/static-private/output.js index b47e34a501fb..9caf732da34c 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/static-private/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/static-private/output.js @@ -4,9 +4,9 @@ class Foo { static { [_init_a, _init_extra_a, _init_b, _init_extra_b] = babelHelpers.applyDecs2311(this, [[dec, 8, "a", o => o.#a, (o, v) => o.#a = v], [dec, 8, "b", o => o.#b, (o, v) => o.#b = v]], []).e; } - static #a = _init_a(this); - static #b = (_init_extra_a(this), _init_b(this, 123)); + static #a = _init_a(); + static #b = (_init_extra_a(), _init_b(123)); static { - _init_extra_b(this); + _init_extra_b(); } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/static-public/output.js index a8974e8cc4ee..93725938f3eb 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/static-public/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-fields/static-public/output.js @@ -4,10 +4,10 @@ class Foo { static { [_init_a, _init_extra_a, _init_b, _init_extra_b, _init_computedKey, _init_extra_computedKey] = babelHelpers.applyDecs2311(this, [[dec, 8, "a"], [dec, 8, "b"], [dec, 8, 'c']], []).e; } - static a = _init_a(this); - static b = (_init_extra_a(this), _init_b(this, 123)); - static ['c'] = (_init_extra_b(this), _init_computedKey(this, 456)); + static a = _init_a(); + static b = (_init_extra_a(), _init_b(123)); + static ['c'] = (_init_extra_b(), _init_computedKey(456)); static { - _init_extra_computedKey(this); + _init_extra_computedKey(); } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/all-decorators/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/all-decorators/output.js index 11979c8bd966..3648ed19cc17 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/all-decorators/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/all-decorators/output.js @@ -52,25 +52,25 @@ new class extends babelHelpers.identity { } } #o = _call_o; - i = _init_i(this); - #C = (_init_extra_i(this), _init_m(this)); - #n = (_init_extra_m(this), _init_n(this)); + i = _init_i(); + #C = (_init_extra_i(), _init_m()); + #n = (_init_extra_m(), _init_n()); get #p() { return _call_p(this); } set #q(v) { _call_q(this, v); } - #D = (_init_extra_n(this), _init_r(this)); + #D = (_init_extra_n(), _init_r()); set #r(v) { - _set_r(this, v); + _set_r(v); } get #r() { - return _get_r(this); + return _get_r(); } constructor() { super(_Class), (() => { - _init_extra_r(this); + _init_extra_r(); })(), _initClass(); } }();