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 5738f6d8112f..9cefbd714935 100644 --- a/packages/babel-plugin-proposal-decorators/src/transformer-2023-03.ts +++ b/packages/babel-plugin-proposal-decorators/src/transformer-2023-03.ts @@ -585,10 +585,19 @@ function transformClass( const maybeExtractDecorator = (decorator: t.Decorator) => { const { expression } = decorator; if (version === "2023-03" && t.isMemberExpression(expression)) { - if (!scopeParent.isStatic(expression)) { - expression.object = memoiseExpression(expression.object, "obj"); + let object; + if ( + t.isSuper(expression.object) || + t.isThisExpression(expression.object) + ) { + object = memoiseExpression(t.thisExpression(), "obj"); + } else if (!scopeParent.isStatic(expression)) { + object = memoiseExpression(expression.object, "obj"); + expression.object = object; + } else { + object = expression.object; } - decoratorsThis.set(decorator, t.cloneNode(expression.object)); + decoratorsThis.set(decorator, t.cloneNode(object)); } if (!scopeParent.isStatic(expression)) { decorator.expression = memoiseExpression(expression, "dec"); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc--to-es2015/super-in-decorator/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc--to-es2015/super-in-decorator/input.js new file mode 100644 index 000000000000..7e75b7766176 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc--to-es2015/super-in-decorator/input.js @@ -0,0 +1,9 @@ +class A extends B { + m() { + @(super.dec1) + class C { + @(super.dec2) + m2() {} + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc--to-es2015/super-in-decorator/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc--to-es2015/super-in-decorator/output.js new file mode 100644 index 000000000000..362f72e318f6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc--to-es2015/super-in-decorator/output.js @@ -0,0 +1,21 @@ +class A extends B { + m() { + var _initClass, _obj, _dec, _obj2, _dec2, _initProto; + let _C; + _obj = this; + _dec = super.dec1; + _obj2 = this; + _dec2 = super.dec2; + class C { + constructor(...args) { + _initProto(this); + } + m2() {} + } + ({ + e: [_initProto], + c: [_C, _initClass] + } = babelHelpers.applyDecs2303(C, [[[1, _obj2, _dec2], 2, "m2"]], [1, _obj, _dec])); + _initClass(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/super-in-decorator/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/super-in-decorator/input.js new file mode 100644 index 000000000000..7e75b7766176 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/super-in-decorator/input.js @@ -0,0 +1,9 @@ +class A extends B { + m() { + @(super.dec1) + class C { + @(super.dec2) + m2() {} + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/super-in-decorator/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/super-in-decorator/output.js new file mode 100644 index 000000000000..9e0efc55f199 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/super-in-decorator/output.js @@ -0,0 +1,25 @@ +class A extends B { + m() { + var _initClass, _obj, _dec, _obj2, _dec2, _initProto; + let _C; + _obj = this; + _dec = super.dec1; + _obj2 = this; + _dec2 = super.dec2; + class C { + static { + ({ + e: [_initProto], + c: [_C, _initClass] + } = babelHelpers.applyDecs2303(this, [[[1, _obj2, _dec2], 2, "m2"]], [1, _obj, _dec])); + } + constructor(...args) { + _initProto(this); + } + m2() {} + static { + _initClass(); + } + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/valid-expression-formats/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/valid-expression-formats/output.js index e43ff08dfbf1..3ed7702e9847 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/valid-expression-formats/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-03-misc/valid-expression-formats/output.js @@ -25,7 +25,7 @@ class Foo { method() {} makeClass() { var _obj3, _dec9, _init_bar; - return _obj3 = this, _dec9 = _obj3.#a, class Nested { + return _obj3 = this, _dec9 = this.#a, class Nested { static { [_init_bar] = babelHelpers.applyDecs2303(this, [[[1, _obj3, _dec9], 0, "bar"]], []).e; }