diff --git a/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts b/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts index b85890e0af..b963d8fe4f 100644 --- a/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts +++ b/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts @@ -790,25 +790,21 @@ function transformClass( value.replaceWith(t.sequenceExpression(body)); } else if (constructorPath) { if (path.node.superClass) { - let found = false; - path.traverse({ - Super(path) { - const { parentPath } = path; + CallExpression: { + exit(path) { + if (!path.get("callee").isSuper()) return; - if (found || parentPath.node.type !== "CallExpression") return; + path.replaceWith( + t.sequenceExpression([ + path.node, + t.cloneNode(protoInitCall), + t.thisExpression(), + ]), + ); - found = true; - - const prop = - path.scope.parent.generateDeclaredUidIdentifier("super"); - parentPath.replaceWith( - t.sequenceExpression([ - t.assignmentExpression("=", t.cloneNode(prop), parentPath.node), - protoInitCall, - t.cloneNode(prop), - ]), - ); + path.skip(); + }, }, }); } else { diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/input.js index 4956801b7a..6b0bc3b5a2 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/input.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/input.js @@ -10,3 +10,14 @@ class A extends B { @deco method() {} } + +class C extends B { + constructor() { + try { + super(super(), null.x); + } catch {} + } + + @deco + method() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/output.js index 6ab0449f86..f29863a7dd 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/output.js @@ -1,4 +1,4 @@ -var _initProto; +var _initProto, _initProto2; class A extends B { static { @@ -6,15 +6,28 @@ class A extends B { } constructor() { - var _super; - if (Math.random() > 0.5) { - _super = super(true), _initProto(this), _super; + super(true), _initProto(this), this; } else { - super(false); + super(false), _initProto(this), this; } } method() {} } + +class C extends B { + static { + [_initProto2] = babelHelpers.applyDecs(this, [[deco, 2, "method"]], []); + } + + constructor() { + try { + super((super(), _initProto2(this), this), null.x), _initProto2(this), this; + } catch {} + } + + method() {} + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/output.js index 223f54963b..8cbf561af4 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/output.js @@ -1,4 +1,4 @@ -var _initProto, _super; +var _initProto; class A extends B { static { @@ -7,7 +7,7 @@ class A extends B { constructor() { let a = 2; - _super = super(a), _initProto(this), _super; + super(a), _initProto(this), this; foo(); }