diff --git a/packages/babel-helpers/src/helpers.js b/packages/babel-helpers/src/helpers.js index 0df0ef3fa1..87c1cbea20 100644 --- a/packages/babel-helpers/src/helpers.js +++ b/packages/babel-helpers/src/helpers.js @@ -445,10 +445,13 @@ helpers.objectWithoutProperties = template(` helpers.possibleConstructorReturn = template(` (function (self, call) { + if (call && (typeof call === "object" || typeof call === "function")) { + return call; + } if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } - return call && (typeof call === "object" || typeof call === "function") ? call : self; + return self; }); `); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/general/derived/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/general/derived/expected.js index 21e5e93b9b..82b3f3d791 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/general/derived/expected.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/general/derived/expected.js @@ -2,11 +2,11 @@ var Foo = function (_Bar) { babelHelpers.inherits(Foo, _Bar); function Foo(...args) { - var _temp, _this, _ret; + var _temp, _this; babelHelpers.classCallCheck(this, Foo); - return _ret = (_temp = (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, ...args)), _this), _this.bar = "foo", _temp), babelHelpers.possibleConstructorReturn(_this, _ret); + return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, ...args)), _this.bar = "foo", _temp)); } return Foo; -}(Bar); +}(Bar); \ No newline at end of file diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/general/foobar/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/general/foobar/expected.js index 00a223287d..96453beaa8 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/general/foobar/expected.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/general/foobar/expected.js @@ -4,9 +4,10 @@ var Child = function (_Parent) { babelHelpers.inherits(Child, _Parent); function Child() { - babelHelpers.classCallCheck(this, Child); + var _this; - var _this = babelHelpers.possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this)); + babelHelpers.classCallCheck(this, Child); + _this = babelHelpers.possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this)); _this.scopedFunctionWithThis = function () { _this.name = {}; diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/general/super-expression/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/general/super-expression/expected.js index 6aa9138dad..269e89a3c3 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/general/super-expression/expected.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/general/super-expression/expected.js @@ -5,7 +5,7 @@ var Foo = function (_Bar) { var _temp, _this; babelHelpers.classCallCheck(this, Foo); - foo((_temp = (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)), _this), _this.bar = "foo", _temp)); + foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)), _this.bar = "foo", _temp)); return _this; } diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/general/super-statement/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/general/super-statement/expected.js index ff1a04df2c..841b154064 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/general/super-statement/expected.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/general/super-statement/expected.js @@ -2,13 +2,13 @@ var Foo = function (_Bar) { babelHelpers.inherits(Foo, _Bar); function Foo() { + var _this; + babelHelpers.classCallCheck(this, Foo); - - var _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)); - + _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)); _this.bar = "foo"; return _this; } return Foo; -}(Bar); +}(Bar); \ No newline at end of file diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/expected.js index f2cd752350..96183ec943 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/expected.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/expected.js @@ -2,15 +2,15 @@ var Foo = function (_Bar) { babelHelpers.inherits(Foo, _Bar); function Foo(...args) { - var _temp, _this, _ret; + var _temp, _this; babelHelpers.classCallCheck(this, Foo); - return _ret = (_temp = (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, ...args)), _this), Object.defineProperty(_this, "bar", { + return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, ...args)), Object.defineProperty(_this, "bar", { enumerable: true, writable: true, value: "foo" - }), _temp), babelHelpers.possibleConstructorReturn(_this, _ret); + }), _temp)); } return Foo; -}(Bar); +}(Bar); \ No newline at end of file diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/expected.js index 72dd7ee1bc..865a66c27a 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/expected.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/expected.js @@ -4,10 +4,10 @@ var Child = function (_Parent) { babelHelpers.inherits(Child, _Parent); function Child() { + var _this; + babelHelpers.classCallCheck(this, Child); - - var _this = babelHelpers.possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this)); - + _this = babelHelpers.possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this)); Object.defineProperty(_this, "scopedFunctionWithThis", { enumerable: true, writable: true, diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-call/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-call/expected.js index 8ed2311f3d..7eb4cf2434 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-call/expected.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-call/expected.js @@ -16,14 +16,14 @@ var B = function (_A) { babelHelpers.inherits(B, _A); function B(...args) { - var _temp, _this, _ret; + var _temp, _this; babelHelpers.classCallCheck(this, B); - return _ret = (_temp = (_this = babelHelpers.possibleConstructorReturn(this, (B.__proto__ || Object.getPrototypeOf(B)).call(this, ...args)), _this), Object.defineProperty(_this, "foo", { + return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, (B.__proto__ || Object.getPrototypeOf(B)).call(this, ...args)), Object.defineProperty(_this, "foo", { enumerable: true, writable: true, value: babelHelpers.get(B.prototype.__proto__ || Object.getPrototypeOf(B.prototype), "foo", _this).call(_this) - }), _temp), babelHelpers.possibleConstructorReturn(_this, _ret); + }), _temp)); } return B; diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/expected.js index 7b18e3051a..fe11c6f6b4 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/expected.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/expected.js @@ -5,7 +5,7 @@ var Foo = function (_Bar) { var _temp, _this; babelHelpers.classCallCheck(this, Foo); - foo((_temp = (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)), _this), Object.defineProperty(_this, "bar", { + foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)), Object.defineProperty(_this, "bar", { enumerable: true, writable: true, value: "foo" diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/expected.js index 5050e95e6a..4b6c1cbff7 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/expected.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/expected.js @@ -2,10 +2,10 @@ var Foo = function (_Bar) { babelHelpers.inherits(Foo, _Bar); function Foo() { + var _this; + babelHelpers.classCallCheck(this, Foo); - - var _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)); - + _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)); Object.defineProperty(_this, "bar", { enumerable: true, writable: true, @@ -15,4 +15,4 @@ var Foo = function (_Bar) { } return Foo; -}(Bar); +}(Bar); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-classes/src/vanilla.js b/packages/babel-plugin-transform-es2015-classes/src/vanilla.js index 81870b9b2c..70e83a340a 100644 --- a/packages/babel-plugin-transform-es2015-classes/src/vanilla.js +++ b/packages/babel-plugin-transform-es2015-classes/src/vanilla.js @@ -93,11 +93,11 @@ export default class ClassTransformer { this.staticPropBody = []; this.body = []; - this.bareSuperAfter = []; this.bareSupers = []; this.pushedConstructor = false; this.pushedInherits = false; + this.pushedThis = false; this.isLoose = false; this.superThises = []; @@ -431,8 +431,6 @@ export default class ClassTransformer { ); } - const bareSuperAfter = this.bareSuperAfter.map(fn => fn(thisRef)); - if ( bareSuper.parentPath.isExpressionStatement() && bareSuper.parentPath.container === body.node.body && @@ -441,22 +439,18 @@ export default class ClassTransformer { // this super call is the last statement in the body so we can just straight up // turn it into a return - if (this.superThises.length || bareSuperAfter.length) { + if (this.superThises.length) { bareSuper.scope.push({ id: thisRef }); call = t.assignmentExpression("=", thisRef, call); } - if (bareSuperAfter.length) { - call = t.toSequenceExpression([call, ...bareSuperAfter, thisRef]); - } - bareSuper.parentPath.replaceWith(t.returnStatement(call)); } else { - bareSuper.replaceWithMultiple([ - t.variableDeclaration("var", [t.variableDeclarator(thisRef, call)]), - ...bareSuperAfter, - t.expressionStatement(thisRef), - ]); + if (!this.pushedThis) { + body.scope.push({ id: thisRef }); + this.pushedThis = true; + } + bareSuper.replaceWith(t.assignmentExpression("=", thisRef, call)); } } @@ -522,17 +516,9 @@ export default class ClassTransformer { } for (const returnPath of this.superReturns) { - if (returnPath.node.argument) { - const ref = returnPath.scope.generateDeclaredUidIdentifier("ret"); - returnPath - .get("argument") - .replaceWithMultiple([ - t.assignmentExpression("=", ref, returnPath.node.argument), - wrapReturn(ref), - ]); - } else { - returnPath.get("argument").replaceWith(wrapReturn()); - } + returnPath + .get("argument") + .replaceWith(wrapReturn(returnPath.node.argument)); } } diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose-classCallCheck/with-superClass/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose-classCallCheck/with-superClass/expected.js index 1bdf3da232..a23bd13150 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose-classCallCheck/with-superClass/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose-classCallCheck/with-superClass/expected.js @@ -6,14 +6,11 @@ let A = function (_B) { _inheritsLoose(A, _B); function A(track) { - if (track !== undefined) { - var _this = _B.call(this, track) || this; - } else { - var _this = _B.call(this) || this; - } + var _this; + if (track !== undefined) _this = _B.call(this, track) || this;else _this = _B.call(this) || this; return _this; } return A; -}(B); +}(B); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/accessing-super-class/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/accessing-super-class/expected.js index 939b58b9dd..1a59a0ff8a 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/accessing-super-class/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/accessing-super-class/expected.js @@ -4,15 +4,15 @@ var Test = function (_Foo) { function Test() { var _Foo$prototype$test, _Foo$prototype$test2; - woops.super.test(); + var _this; - var _this = _Foo.call(this) || this; + woops.super.test(); + _this = _Foo.call(this) || this; _Foo.prototype.test.call(_this); - var _this = _Foo.apply(this, arguments) || this; - - var _this = _Foo.call.apply(_Foo, [this, "test"].concat(Array.prototype.slice.call(arguments))) || this; + _this = _Foo.apply(this, arguments) || this; + _this = _Foo.call.apply(_Foo, [this, "test"].concat(Array.prototype.slice.call(arguments))) || this; (_Foo$prototype$test = _Foo.prototype.test).call.apply(_Foo$prototype$test, [_this].concat(Array.prototype.slice.call(arguments))); @@ -22,4 +22,4 @@ var Test = function (_Foo) { } return Test; -}(Foo); \ No newline at end of file +}(Foo); diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/accessing-super-properties/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/accessing-super-properties/expected.js index 03969d0f8d..7d592fe65a 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/accessing-super-properties/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/accessing-super-properties/expected.js @@ -2,12 +2,13 @@ var Test = function (_Foo) { babelHelpers.inheritsLoose(Test, _Foo); function Test() { - var _this = _Foo.call(this) || this; + var _this; + _this = _Foo.call(this) || this; _Foo.prototype.test; _Foo.prototype.test.whatever; return _this; } return Test; -}(Foo); +}(Foo); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/calling-super-properties/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/calling-super-properties/expected.js index ee927b67e1..702219fa19 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/calling-super-properties/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/calling-super-properties/expected.js @@ -2,7 +2,9 @@ var Test = function (_Foo) { babelHelpers.inheritsLoose(Test, _Foo); function Test() { - var _this = _Foo.call(this) || this; + var _this; + + _this = _Foo.call(this) || this; _Foo.prototype.test.whatever(); @@ -16,4 +18,4 @@ var Test = function (_Foo) { }; return Test; -}(Foo); \ No newline at end of file +}(Foo); diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/returning-from-derived-constructor/exec.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/returning-from-derived-constructor/exec.js new file mode 100644 index 0000000000..92f24d1586 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/loose/returning-from-derived-constructor/exec.js @@ -0,0 +1,44 @@ +"use strict"; +class Foo { + constructor() { + return { x: 1 }; + } +} + +assert.equal(new Foo().x, 1); + +class Bar extends Foo { + constructor() { + super(); + return; + } +} + +assert.equal(new Bar().x, 1); + +class Bar2 extends Foo { + constructor() { + super(); + assert.equal(this.x, 1); + return { x: 2 }; + } +} + +assert.equal(new Bar2().x, 2); + + +let singleton; +class Sub extends Foo { + constructor() { + if (singleton) { + return singleton; + } + singleton = super(); + } +} + +let instance = new Sub; +assert.equal(instance, singleton); + +instance = new Sub; +assert.equal(instance, singleton); diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/2663/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/2663/expected.js index dfe73947fe..88bcd594dd 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/2663/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/2663/expected.js @@ -20,10 +20,10 @@ var Connection = function (_EventEmitter) { babelHelpers.inherits(Connection, _EventEmitter); function Connection(endpoint, joinKey, joinData, roomId) { + var _this; + babelHelpers.classCallCheck(this, Connection); - - var _this = babelHelpers.possibleConstructorReturn(this, (Connection.__proto__ || Object.getPrototypeOf(Connection)).call(this)); - + _this = babelHelpers.possibleConstructorReturn(this, (Connection.__proto__ || Object.getPrototypeOf(Connection)).call(this)); _this.isConnected = false; _this.roomId = roomId; // ... diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/3028/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/3028/expected.js index 2c719619a2..4fbdf2f92c 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/3028/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/3028/expected.js @@ -12,9 +12,10 @@ var a1 = function (_b) { babelHelpers.inherits(a1, _b); function a1() { - babelHelpers.classCallCheck(this, a1); + var _this; - var _this = babelHelpers.possibleConstructorReturn(this, (a1.__proto__ || Object.getPrototypeOf(a1)).call(this)); + babelHelpers.classCallCheck(this, a1); + _this = babelHelpers.possibleConstructorReturn(this, (a1.__proto__ || Object.getPrototypeOf(a1)).call(this)); _this.x = function () { return _this; @@ -30,9 +31,10 @@ var a2 = function (_b2) { babelHelpers.inherits(a2, _b2); function a2() { - babelHelpers.classCallCheck(this, a2); + var _this2; - var _this2 = babelHelpers.possibleConstructorReturn(this, (a2.__proto__ || Object.getPrototypeOf(a2)).call(this)); + babelHelpers.classCallCheck(this, a2); + _this2 = babelHelpers.possibleConstructorReturn(this, (a2.__proto__ || Object.getPrototypeOf(a2)).call(this)); _this2.x = function () { return _this2; diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T2997/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T2997/expected.js index ec955e6b76..d11d62e2e3 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T2997/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T2997/expected.js @@ -8,10 +8,10 @@ var B = function (_A) { babelHelpers.inherits(B, _A); function B() { - var _this, _ret; + var _this; babelHelpers.classCallCheck(this, B); - return _ret = (_this = babelHelpers.possibleConstructorReturn(this, (B.__proto__ || Object.getPrototypeOf(B)).call(this)), _this), babelHelpers.possibleConstructorReturn(_this, _ret); + return babelHelpers.possibleConstructorReturn(_this, _this = babelHelpers.possibleConstructorReturn(this, (B.__proto__ || Object.getPrototypeOf(B)).call(this))); } return B; diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T7537/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T7537/expected.js index 52329c24f5..57019fadbb 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T7537/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T7537/expected.js @@ -1,6 +1,6 @@ "use strict"; -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } +function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } if (!self) { 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, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } @@ -14,14 +14,11 @@ var A = function (_B) { _inherits(A, _B); function A(track) { + var _this; + _classCallCheck(this, A); - if (track !== undefined) { - var _this = _possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this, track)); - } else { - var _this = _possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this)); - } - + if (track !== undefined) _this = _possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this, track));else _this = _possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this)); return _possibleConstructorReturn(_this); } diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/accessing-super-class/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/accessing-super-class/expected.js index f084e1a73c..5e15ebbee3 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/accessing-super-class/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/accessing-super-class/expected.js @@ -4,17 +4,14 @@ var Test = function (_Foo) { function Test() { var _ref, _babelHelpers$get; + var _this; + babelHelpers.classCallCheck(this, Test); woops.super.test(); - - var _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this)); - + _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this)); babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).call(_this); - - var _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).apply(this, arguments)); - - var _this = babelHelpers.possibleConstructorReturn(this, (_ref = Test.__proto__ || Object.getPrototypeOf(Test)).call.apply(_ref, [this, "test"].concat(Array.prototype.slice.call(arguments)))); - + _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).apply(this, arguments)); + _this = babelHelpers.possibleConstructorReturn(this, (_ref = Test.__proto__ || Object.getPrototypeOf(Test)).call.apply(_ref, [this, "test"].concat(Array.prototype.slice.call(arguments)))); babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).apply(_this, arguments); (_babelHelpers$get = babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this)).call.apply(_babelHelpers$get, [_this, "test"].concat(Array.prototype.slice.call(arguments))); diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/accessing-super-properties/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/accessing-super-properties/expected.js index 217d2e0802..ea654170b8 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/accessing-super-properties/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/accessing-super-properties/expected.js @@ -2,14 +2,14 @@ var Test = function (_Foo) { babelHelpers.inherits(Test, _Foo); function Test() { + var _this; + babelHelpers.classCallCheck(this, Test); - - var _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this)); - + _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this)); babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this); babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).whatever; return _this; } return Test; -}(Foo); +}(Foo); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/calling-super-properties/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/calling-super-properties/expected.js index d8933852db..53784d5cd7 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/calling-super-properties/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/calling-super-properties/expected.js @@ -2,10 +2,10 @@ var Test = function (_Foo) { babelHelpers.inherits(Test, _Foo); function Test() { + var _this; + babelHelpers.classCallCheck(this, Test); - - var _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this)); - + _this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this)); babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).whatever(); babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).call(_this); return _this; @@ -18,4 +18,4 @@ var Test = function (_Foo) { } }]); return Test; -}(Foo); +}(Foo); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/constructor/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/constructor/expected.js index 6ca087a9be..f1bffd8027 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/constructor/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/constructor/expected.js @@ -7,10 +7,10 @@ var Foo = function (_Bar) { babelHelpers.inherits(Foo, _Bar); function Foo() { + var _this; + babelHelpers.classCallCheck(this, Foo); - - var _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)); - + _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)); _this.state = "test"; return _this; } diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/returning-from-derived-constructor/exec.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/returning-from-derived-constructor/exec.js index a37d09054b..7d62221183 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/returning-from-derived-constructor/exec.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/returning-from-derived-constructor/exec.js @@ -1,3 +1,4 @@ +"use strict"; class Foo { constructor() { return { x: 1 }; @@ -24,3 +25,34 @@ class Bar2 extends Foo { } assert.equal(new Bar2().x, 2); + + +let singleton; +class Sub extends Foo { + constructor() { + if (singleton) { + return singleton; + } + singleton = super(); + } +} + +let instance = new Sub; +assert.equal(instance, singleton); + +instance = new Sub; +assert.equal(instance, singleton); + +class Null extends Foo { + constructor() { + if (false) { + super(); + } + return null; + super(); + } +} + +assert.throws(() => { + new Null(); +}, "this"); diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/super-reference-in-prop-exression/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/super-reference-in-prop-exression/expected.js index 17884366f0..665ce8813e 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/super-reference-in-prop-exression/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/spec/super-reference-in-prop-exression/expected.js @@ -5,9 +5,9 @@ var Foo = function (_Bar) { var _this; babelHelpers.classCallCheck(this, Foo); - babelHelpers.get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)), _this).method, _this).call(_this); + babelHelpers.get(Foo.prototype.__proto__ || Object.getPrototypeOf(Foo.prototype), (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this))).method, _this).call(_this); return _this; } return Foo; -}(Bar); +}(Bar); \ No newline at end of file