diff --git a/lib/6to5/transformation/templates/class-super-constructor-call.js b/lib/6to5/transformation/templates/class-super-constructor-call.js index 35dc0b8f58..62c7079a0f 100644 --- a/lib/6to5/transformation/templates/class-super-constructor-call.js +++ b/lib/6to5/transformation/templates/class-super-constructor-call.js @@ -1,5 +1,3 @@ -(function (instance, Constructor) { - if (Object.getPrototypeOf(Constructor) !== null) { - Object.getPrototypeOf(Constructor).apply(instance, arguments); - } -}); +if (Object.getPrototypeOf(CLASS_NAME) !== null) { + Object.getPrototypeOf(CLASS_NAME).apply(instance, arguments); +} diff --git a/lib/6to5/transformation/transformers/es6/classes.js b/lib/6to5/transformation/transformers/es6/classes.js index 43785d92f0..04347b09c3 100644 --- a/lib/6to5/transformation/transformers/es6/classes.js +++ b/lib/6to5/transformation/transformers/es6/classes.js @@ -169,19 +169,11 @@ Class.prototype.buildBody = function () { // we have no constructor, we have a super, and the super doesn't appear to be falsy if (!this.hasConstructor && superName && !t.isFalsyExpression(superName)) { var helperName = "class-super-constructor-call"; - - if (this.isLoose) { - constructor.body.body.push(util.template(helperName + "-loose", { - CLASS_NAME: className, - SUPER_NAME: this.superName - }, true)); - } else { - constructor.body.body.push( - t.expressionStatement( - t.callExpression(this.file.addHelper(helperName), [t.thisExpression(), className]) - ) - ); - } + if (this.isLoose) helperName += "-loose" + constructor.body.body.push(util.template(helperName, { + CLASS_NAME: className, + SUPER_NAME: this.superName + }, true)); } var instanceProps; diff --git a/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js b/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js index 63ffd096b1..9230f0395d 100644 --- a/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js @@ -1,7 +1,5 @@ "use strict"; -var _classSuperConstructorCall = function (instance, Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(instance, arguments); } }; - var _inherits = function (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) subClass.__proto__ = superClass; }; var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; @@ -10,7 +8,9 @@ var BaseController = (function (_Chaplin$Controller) { function BaseController() { _classCallCheck(this, BaseController); - _classSuperConstructorCall(this, BaseController); + if (Object.getPrototypeOf(BaseController) !== null) { + Object.getPrototypeOf(BaseController).apply(instance, arguments); + } } _inherits(BaseController, _Chaplin$Controller); @@ -22,7 +22,9 @@ var BaseController2 = (function (_Chaplin$Controller$Another) { function BaseController2() { _classCallCheck(this, BaseController2); - _classSuperConstructorCall(this, BaseController2); + if (Object.getPrototypeOf(BaseController2) !== null) { + Object.getPrototypeOf(BaseController2).apply(instance, arguments); + } } _inherits(BaseController2, _Chaplin$Controller$Another); diff --git a/test/fixtures/transformation/es6-classes/super-class/expected.js b/test/fixtures/transformation/es6-classes/super-class/expected.js index 6c7d2d245f..2505a6b3c3 100644 --- a/test/fixtures/transformation/es6-classes/super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class/expected.js @@ -1,7 +1,5 @@ "use strict"; -var _classSuperConstructorCall = function (instance, Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(instance, arguments); } }; - var _inherits = function (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) subClass.__proto__ = superClass; }; var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; @@ -10,7 +8,9 @@ var Test = (function (Foo) { function Test() { _classCallCheck(this, Test); - _classSuperConstructorCall(this, Test); + if (Object.getPrototypeOf(Test) !== null) { + Object.getPrototypeOf(Test).apply(instance, arguments); + } } _inherits(Test, Foo); diff --git a/test/fixtures/transformation/spec-proto-to-assign/class/expected.js b/test/fixtures/transformation/spec-proto-to-assign/class/expected.js index 3d5b539004..4221646b33 100644 --- a/test/fixtures/transformation/spec-proto-to-assign/class/expected.js +++ b/test/fixtures/transformation/spec-proto-to-assign/class/expected.js @@ -2,8 +2,6 @@ var _defaults = function (obj, defaults) { for (var key in defaults) { if (obj[key] === undefined) { obj[key] = defaults[key]; } } return obj; }; -var _classSuperConstructorCall = function (instance, Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(instance, arguments); } }; - var _inherits = function (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) _defaults(subClass, superClass); }; var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; @@ -12,7 +10,9 @@ var Foo = (function (Bar) { function Foo() { _classCallCheck(this, Foo); - _classSuperConstructorCall(this, Foo); + if (Object.getPrototypeOf(Foo) !== null) { + Object.getPrototypeOf(Foo).apply(instance, arguments); + } } _inherits(Foo, Bar);