diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 31b3ca8843..e405ab761d 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -21,7 +21,7 @@ exports.ClassDeclaration = function (node, parent, file, scope) { }); return t.assignmentExpression("=", node.id, newNode); } else { - // likely has a PrivateDeclaration etc + // has a super class or PrivateDeclaration etc return t.variableDeclaration("let", [ t.variableDeclarator(node.id, newNode) ]); @@ -85,7 +85,8 @@ Class.prototype.run = function () { // - if (superName && t.isDynamic(superName)) { + if (superName) { + this.closure = true; // so we're only evaluating it once var superRefName = "super"; if (className) superRefName = className.name + "Super"; @@ -95,13 +96,8 @@ Class.prototype.run = function () { t.variableDeclarator(superRef, superName) ])); superName = superRef; - } - this.superName = superName; - - // - - if (superName) { + this.superName = superName; body.push(t.expressionStatement(t.callExpression(file.addDeclaration("inherits"), [className, superName]))); } diff --git a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js index fd7527b70a..66577c1cc1 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js @@ -13,29 +13,34 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var Test = function Test() { - woops["super"].test(); - Foo.call(this); - Foo.prototype.test.call(this); - foob(Foo); +var Test = (function () { + var _TestSuper = Foo; + var Test = function Test() { + woops["super"].test(); + _TestSuper.call(this); + _TestSuper.prototype.test.call(this); + foob(_TestSuper); - Foo.call.apply(Foo, [this].concat(_slice.call(arguments))); - Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments))); + _TestSuper.call.apply(_TestSuper, [this].concat(_slice.call(arguments))); + _TestSuper.call.apply(_TestSuper, [this, "test"].concat(_slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments))); -}; + _TestSuper.prototype.test.call.apply(_TestSuper.prototype, [this].concat(_slice.call(arguments))); + _TestSuper.prototype.test.call.apply(_TestSuper.prototype, [this, "test"].concat(_slice.call(arguments))); + }; -_inherits(Test, Foo); + _inherits(Test, _TestSuper); -Test.prototype.test = function () { - Foo.prototype.test.call(this); - Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_slice.call(arguments))); -}; + Test.prototype.test = function () { + _TestSuper.prototype.test.call(this); + _TestSuper.prototype.test.call.apply(_TestSuper.prototype.test, [this].concat(_slice.call(arguments))); + _TestSuper.prototype.test.call.apply(_TestSuper.prototype.test, [this, "test"].concat(_slice.call(arguments))); + }; -Test.foo = function () { - Foo.foo.call(this); - Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments))); - Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments))); -}; + Test.foo = function () { + _TestSuper.foo.call(this); + _TestSuper.foo.call.apply(_TestSuper.foo, [this].concat(_slice.call(arguments))); + _TestSuper.foo.call.apply(_TestSuper.foo, [this, "test"].concat(_slice.call(arguments))); + }; + + return Test; +})(); diff --git a/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js index 64df221c08..46d9bf1c41 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js @@ -12,9 +12,14 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var Test = function Test() { - Foo.prototype.test; - Foo.prototype.test.whatever; -}; +var Test = (function () { + var _TestSuper = Foo; + var Test = function Test() { + _TestSuper.prototype.test; + _TestSuper.prototype.test.whatever; + }; -_inherits(Test, Foo); + _inherits(Test, _TestSuper); + + return Test; +})(); diff --git a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js index d13ee876cd..44f086c4b9 100644 --- a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js @@ -12,13 +12,18 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var Test = function Test() { - Foo.prototype.test.whatever(); - Foo.prototype.test.call(this); -}; +var Test = (function () { + var _TestSuper = Foo; + var Test = function Test() { + _TestSuper.prototype.test.whatever(); + _TestSuper.prototype.test.call(this); + }; -_inherits(Test, Foo); + _inherits(Test, _TestSuper); -Test.test = function () { - return Foo.wow.call(this); -}; + Test.test = function () { + return _TestSuper.wow.call(this); + }; + + return Test; +})(); diff --git a/test/fixtures/transformation/es6-classes/constructor/expected.js b/test/fixtures/transformation/es6-classes/constructor/expected.js index 00e97931b5..c6b5282782 100644 --- a/test/fixtures/transformation/es6-classes/constructor/expected.js +++ b/test/fixtures/transformation/es6-classes/constructor/expected.js @@ -16,8 +16,13 @@ var Test = function Test() { this.state = "test"; }; -var Foo = function Foo() { - this.state = "test"; -}; +var Foo = (function () { + var _FooSuper = Bar; + var Foo = function Foo() { + this.state = "test"; + }; -_inherits(Foo, Bar); + _inherits(Foo, _FooSuper); + + return Foo; +})(); 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 70de0712ea..cc5ebdee6f 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 @@ -12,18 +12,28 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var BaseController = function BaseController() { - if (Chaplin.Controller) { - Chaplin.Controller.apply(this, arguments); - } -}; +var BaseController = (function () { + var _BaseControllerSuper = Chaplin.Controller; + var BaseController = function BaseController() { + if (_BaseControllerSuper) { + _BaseControllerSuper.apply(this, arguments); + } + }; -_inherits(BaseController, Chaplin.Controller); + _inherits(BaseController, _BaseControllerSuper); -var BaseController2 = function BaseController2() { - if (Chaplin.Controller.Another) { - Chaplin.Controller.Another.apply(this, arguments); - } -}; + return BaseController; +})(); -_inherits(BaseController2, Chaplin.Controller.Another); +var BaseController2 = (function () { + var _BaseController2Super = Chaplin.Controller.Another; + var BaseController2 = function BaseController2() { + if (_BaseController2Super) { + _BaseController2Super.apply(this, arguments); + } + }; + + _inherits(BaseController2, _BaseController2Super); + + return BaseController2; +})(); diff --git a/test/fixtures/transformation/es6-classes/super-class/expected.js b/test/fixtures/transformation/es6-classes/super-class/expected.js index d7a906d848..8e7db71d24 100644 --- a/test/fixtures/transformation/es6-classes/super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class/expected.js @@ -12,10 +12,15 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var Test = function Test() { - if (Foo) { - Foo.apply(this, arguments); - } -}; +var Test = (function () { + var _TestSuper = Foo; + var Test = function Test() { + if (_TestSuper) { + _TestSuper.apply(this, arguments); + } + }; -_inherits(Test, Foo); + _inherits(Test, _TestSuper); + + return Test; +})();