From 045d019149e40fd9424efad445b92a04f5f1f7e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 7 Feb 2019 22:01:46 +0000 Subject: [PATCH] Fix duplicated assertThisInitialized calls in constructors (#9458) --- .../derived-multiple-supers/output.js | 4 +- .../fixtures/private-loose/derived/output.js | 2 +- .../fixtures/private-loose/foobar/output.js | 2 +- .../private-loose/super-expression/output.js | 2 +- .../private-loose/super-statement/output.js | 2 +- .../private/derived-multiple-supers/output.js | 4 +- .../test/fixtures/private/derived/output.js | 2 +- .../test/fixtures/private/foobar/output.js | 2 +- .../fixtures/private/super-call/output.js | 2 +- .../private/super-expression/output.js | 2 +- .../private/super-statement/output.js | 2 +- .../public/derived-multiple-supers/output.js | 4 +- .../test/fixtures/public/derived/output.js | 2 +- .../test/fixtures/public/foobar/output.js | 2 +- .../test/fixtures/public/super-call/output.js | 2 +- .../public/super-expression/output.js | 2 +- .../fixtures/public/super-statement/output.js | 2 +- .../test/fixtures/regression/6154/output.js | 6 +- .../src/transformClass.js | 92 ++++++++----------- .../test/fixtures/regression/3028/output.js | 4 +- .../output.js | 2 +- .../output.js | 2 +- .../output.js | 2 +- .../output.js | 2 +- .../regression/6057-expanded/output.js | 6 +- 25 files changed, 69 insertions(+), 87 deletions(-) diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/derived-multiple-supers/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/derived-multiple-supers/output.js index c6f6c9ecc3..c9e24a3826 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/derived-multiple-supers/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/derived-multiple-supers/output.js @@ -12,13 +12,13 @@ function (_Bar) { if (condition) { _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - Object.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this))), _bar, { + Object.defineProperty(babelHelpers.assertThisInitialized(_this), _bar, { writable: true, value: "foo" }); } else { _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - Object.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this))), _bar, { + Object.defineProperty(babelHelpers.assertThisInitialized(_this), _bar, { writable: true, value: "foo" }); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/derived/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/derived/output.js index 01ba0c6cdf..b008861733 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/derived/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/derived/output.js @@ -22,7 +22,7 @@ function (_Foo) { babelHelpers.classCallCheck(this, Bar); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Bar).call(this, ...args)); - Object.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), _prop2, { + Object.defineProperty(babelHelpers.assertThisInitialized(_this), _prop2, { writable: true, value: "bar" }); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/foobar/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/foobar/output.js index d4a49a383c..c28687635b 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/foobar/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/foobar/output.js @@ -10,7 +10,7 @@ function (_Parent) { babelHelpers.classCallCheck(this, Child); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Child).call(this)); - Object.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), _scopedFunctionWithThis, { + Object.defineProperty(babelHelpers.assertThisInitialized(_this), _scopedFunctionWithThis, { writable: true, value: function value() { _this.name = {}; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/super-expression/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/super-expression/output.js index a21817fed2..70cd36e525 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/super-expression/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/super-expression/output.js @@ -9,7 +9,7 @@ function (_Bar) { var _temp, _this; babelHelpers.classCallCheck(this, Foo); - foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)), Object.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), _bar, { + foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)), Object.defineProperty(babelHelpers.assertThisInitialized(_this), _bar, { writable: true, value: "foo" }), _temp)); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/super-statement/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/super-statement/output.js index bf1561a9da..c0f57c4a33 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/super-statement/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/super-statement/output.js @@ -10,7 +10,7 @@ function (_Bar) { babelHelpers.classCallCheck(this, Foo); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - Object.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), _bar, { + Object.defineProperty(babelHelpers.assertThisInitialized(_this), _bar, { writable: true, value: "foo" }); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/derived-multiple-supers/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/derived-multiple-supers/output.js index 9b46f9b9e4..e69db438db 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/derived-multiple-supers/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/derived-multiple-supers/output.js @@ -13,14 +13,14 @@ function (_Bar) { if (condition) { _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - _bar.set(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this))), { + _bar.set(babelHelpers.assertThisInitialized(_this), { writable: true, value: "foo" }); } else { _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - _bar.set(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this))), { + _bar.set(babelHelpers.assertThisInitialized(_this), { writable: true, value: "foo" }); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/derived/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/derived/output.js index c21d6c3186..e963676ac3 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/derived/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/derived/output.js @@ -24,7 +24,7 @@ function (_Foo) { babelHelpers.classCallCheck(this, Bar); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Bar).call(this, ...args)); - _prop2.set(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), { + _prop2.set(babelHelpers.assertThisInitialized(_this), { writable: true, value: "bar" }); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/foobar/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/foobar/output.js index f518512133..7519122281 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/foobar/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/foobar/output.js @@ -11,7 +11,7 @@ function (_Parent) { babelHelpers.classCallCheck(this, Child); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Child).call(this)); - _scopedFunctionWithThis.set(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), { + _scopedFunctionWithThis.set(babelHelpers.assertThisInitialized(_this), { writable: true, value: () => { _this.name = {}; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-call/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-call/output.js index 09e9c4b9e1..460fc4c6be 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-call/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-call/output.js @@ -29,7 +29,7 @@ function (_A) { babelHelpers.classCallCheck(this, B); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(B).call(this, ...args)); - _foo.set(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), { + _foo.set(babelHelpers.assertThisInitialized(_this), { writable: true, value: babelHelpers.get(babelHelpers.getPrototypeOf(B.prototype), "foo", babelHelpers.assertThisInitialized(_this)).call(babelHelpers.assertThisInitialized(_this)) }); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-expression/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-expression/output.js index c887562842..f4bce1d758 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-expression/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-expression/output.js @@ -9,7 +9,7 @@ function (_Bar) { var _temp, _this; babelHelpers.classCallCheck(this, Foo); - foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)), _bar.set(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), { + foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)), _bar.set(babelHelpers.assertThisInitialized(_this), { writable: true, value: "foo" }), _temp)); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-statement/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-statement/output.js index ecb55ffde5..df484a2a1c 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-statement/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/super-statement/output.js @@ -11,7 +11,7 @@ function (_Bar) { babelHelpers.classCallCheck(this, Foo); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - _bar.set(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), { + _bar.set(babelHelpers.assertThisInitialized(_this), { writable: true, value: "foo" }); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-multiple-supers/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-multiple-supers/output.js index 7b4d565978..cb9e03767b 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-multiple-supers/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-multiple-supers/output.js @@ -12,10 +12,10 @@ function (_Bar) { if (condition) { _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - babelHelpers.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this))), "bar", "foo"); + babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", "foo"); } else { _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - babelHelpers.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this))), "bar", "foo"); + babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", "foo"); } return babelHelpers.possibleConstructorReturn(_this); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived/output.js index 24da3aeebd..ebfcf1faa2 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived/output.js @@ -10,7 +10,7 @@ function (_Bar) { babelHelpers.classCallCheck(this, Foo); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this, ...args)); - babelHelpers.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), "bar", "foo"); + babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", "foo"); return _this; } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/output.js index beb7b9f196..3e89c9938b 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/output.js @@ -10,7 +10,7 @@ function (_Parent) { babelHelpers.classCallCheck(this, Child); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Child).call(this)); - babelHelpers.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), "scopedFunctionWithThis", function () { + babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "scopedFunctionWithThis", function () { _this.name = {}; }); return _this; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-call/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-call/output.js index 800414215c..08bb0b4d9b 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-call/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-call/output.js @@ -28,7 +28,7 @@ function (_A) { babelHelpers.classCallCheck(this, B); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(B).call(this, ...args)); - babelHelpers.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), "foo", babelHelpers.get(babelHelpers.getPrototypeOf(B.prototype), "foo", babelHelpers.assertThisInitialized(_this)).call(babelHelpers.assertThisInitialized(_this))); + babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "foo", babelHelpers.get(babelHelpers.getPrototypeOf(B.prototype), "foo", babelHelpers.assertThisInitialized(_this)).call(babelHelpers.assertThisInitialized(_this))); return _this; } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-expression/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-expression/output.js index a2eab2f89b..be7f0ef682 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-expression/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-expression/output.js @@ -9,7 +9,7 @@ function (_Bar) { var _temp, _this; babelHelpers.classCallCheck(this, Foo); - foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)), babelHelpers.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), "bar", "foo"), _temp)); + foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)), babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", "foo"), _temp)); return _this; } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-statement/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-statement/output.js index be84c76145..ef101c0bee 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-statement/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-statement/output.js @@ -10,7 +10,7 @@ function (_Bar) { babelHelpers.classCallCheck(this, Foo); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - babelHelpers.defineProperty(babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)), "bar", "foo"); + babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", "foo"); return _this; } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js index 54b3b68615..b72cd0ee5d 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js @@ -2,12 +2,12 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterat function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } +function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } + function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } -function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } - function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } @@ -43,7 +43,7 @@ var Test = function Test() { _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Other)).call.apply(_getPrototypeOf2, [this].concat(args))); - _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "a", function () { + _defineProperty(_assertThisInitialized(_this), "a", function () { return _get(_getPrototypeOf(Other.prototype), "test", _assertThisInitialized(_this)); }); diff --git a/packages/babel-plugin-transform-classes/src/transformClass.js b/packages/babel-plugin-transform-classes/src/transformClass.js index 60e586c277..b537c00b3a 100644 --- a/packages/babel-plugin-transform-classes/src/transformClass.js +++ b/packages/babel-plugin-transform-classes/src/transformClass.js @@ -32,25 +32,6 @@ const verifyConstructorVisitor = traverse.visitors.merge([ ); } }, - - ThisExpression(path, state) { - if (!state.isDerived) return; - - const { node, parentPath } = path; - if (parentPath.isMemberExpression({ object: node })) { - // In cases like this.foo or this[foo], there is no need to add - // assertThisInitialized, since they already throw if this is - // undefined. - return; - } - - const assertion = t.callExpression( - state.file.addHelper("assertThisInitialized"), - [node], - ); - path.replaceWith(assertion); - path.skip(); - }, }, ]); @@ -84,7 +65,6 @@ export default function transformClass( instancePropRefs: {}, staticPropBody: [], body: [], - bareSupers: new Set(), superThises: [], pushedConstructor: false, pushedInherits: false, @@ -217,34 +197,22 @@ export default function transformClass( replaceSupers.replace(); - // TODO this needs to be cleaned up. But, one step at a time. - const state = { - returns: [], - bareSupers: new Set(), - }; + const superReturns = []; path.traverse( traverse.visitors.merge([ environmentVisitor, { - ReturnStatement(path, state) { + ReturnStatement(path) { if (!path.getFunctionParent().isArrowFunctionExpression()) { - state.returns.push(path); - } - }, - - Super(path, state) { - const { node, parentPath } = path; - if (parentPath.isCallExpression({ callee: node })) { - state.bareSupers.add(parentPath); + superReturns.push(path); } }, }, ]), - state, ); if (isConstructor) { - pushConstructor(state, node, path); + pushConstructor(superReturns, node, path); } else { pushMethod(node, path); } @@ -378,15 +346,43 @@ export default function transformClass( path.traverse(findThisesVisitor); - let guaranteedSuperBeforeFinish = !!classState.bareSupers.size; - let thisRef = function() { const ref = path.scope.generateDeclaredUidIdentifier("this"); thisRef = () => t.cloneNode(ref); return ref; }; - for (const bareSuper of classState.bareSupers) { + for (const thisPath of classState.superThises) { + const { node, parentPath } = thisPath; + if (parentPath.isMemberExpression({ object: node })) { + thisPath.replaceWith(thisRef()); + continue; + } + thisPath.replaceWith( + t.callExpression(classState.file.addHelper("assertThisInitialized"), [ + thisRef(), + ]), + ); + } + + const bareSupers = new Set(); + path.traverse( + traverse.visitors.merge([ + environmentVisitor, + { + Super(path) { + const { node, parentPath } = path; + if (parentPath.isCallExpression({ callee: node })) { + bareSupers.add(parentPath); + } + }, + }, + ]), + ); + + let guaranteedSuperBeforeFinish = !!bareSupers.size; + + for (const bareSuper of bareSupers) { wrapSuperCall(bareSuper, classState.superName, thisRef, body); if (guaranteedSuperBeforeFinish) { @@ -408,19 +404,6 @@ export default function transformClass( } } - for (const thisPath of classState.superThises) { - const { node, parentPath } = thisPath; - if (parentPath.isMemberExpression({ object: node })) { - thisPath.replaceWith(thisRef()); - continue; - } - thisPath.replaceWith( - t.callExpression(classState.file.addHelper("assertThisInitialized"), [ - thisRef(), - ]), - ); - } - let wrapReturn; if (classState.isLoose) { @@ -535,7 +518,7 @@ export default function transformClass( * Replace the constructor body of our class. */ function pushConstructor( - replaceSupers, + superReturns, method: { type: "ClassMethod" }, path: NodePath, ) { @@ -548,8 +531,7 @@ export default function transformClass( userConstructorPath: path, userConstructor: method, hasConstructor: true, - bareSupers: replaceSupers.bareSupers, - superReturns: replaceSupers.returns, + superReturns, }); const { construct } = classState; diff --git a/packages/babel-plugin-transform-classes/test/fixtures/regression/3028/output.js b/packages/babel-plugin-transform-classes/test/fixtures/regression/3028/output.js index ec55d2a483..2de289261b 100644 --- a/packages/babel-plugin-transform-classes/test/fixtures/regression/3028/output.js +++ b/packages/babel-plugin-transform-classes/test/fixtures/regression/3028/output.js @@ -21,7 +21,7 @@ function (_b) { _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(a1).call(this)); _this.x = function () { - return babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)); + return babelHelpers.assertThisInitialized(_this); }; return _this; @@ -42,7 +42,7 @@ function (_b2) { _this2 = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(a2).call(this)); _this2.x = function () { - return babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this2)); + return babelHelpers.assertThisInitialized(_this2); }; return _this2; diff --git a/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-2/output.js b/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-2/output.js index 37de9b654a..ec1d652c8d 100644 --- a/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-2/output.js +++ b/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-2/output.js @@ -9,7 +9,7 @@ function (_Bar) { var _this; babelHelpers.classCallCheck(this, Foo); - return _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this, babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)))); + return _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this, babelHelpers.assertThisInitialized(_this))); } return Foo; diff --git a/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-3/output.js b/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-3/output.js index d678fc5b30..06e41b47d3 100644 --- a/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-3/output.js +++ b/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-3/output.js @@ -10,7 +10,7 @@ function (_Bar) { babelHelpers.classCallCheck(this, Foo); - var fn = () => babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)); + var fn = () => babelHelpers.assertThisInitialized(_this); fn(); return _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); diff --git a/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-4/output.js b/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-4/output.js index bc92c2f54d..2bce474839 100644 --- a/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-4/output.js +++ b/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-4/output.js @@ -10,7 +10,7 @@ function (_Bar) { babelHelpers.classCallCheck(this, Foo); - var fn = () => babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this)); + var fn = () => babelHelpers.assertThisInitialized(_this); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); fn(); diff --git a/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-5/output.js b/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-5/output.js index 57ca566c99..a49d2be809 100644 --- a/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-5/output.js +++ b/packages/babel-plugin-transform-classes/test/fixtures/spec/this-not-allowed-before-super-in-derived-classes-5/output.js @@ -9,7 +9,7 @@ function (_Bar) { var _this; babelHelpers.classCallCheck(this, Foo); - Foo[babelHelpers.assertThisInitialized(babelHelpers.assertThisInitialized(_this))]; + Foo[babelHelpers.assertThisInitialized(_this)]; return babelHelpers.possibleConstructorReturn(_this); } diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js index f7722d117e..ed713e114c 100644 --- a/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js +++ b/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js @@ -21,12 +21,12 @@ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } +function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } + function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } -function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } - function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var App = @@ -47,7 +47,7 @@ function (_Component) { _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(App)).call.apply(_getPrototypeOf2, [this].concat(args))); - _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "exportType", ''); + _defineProperty(_assertThisInitialized(_this), "exportType", ''); return _this; }