diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index ffa8f0e47f..0d39bdc565 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -58,15 +58,23 @@ Class.prototype.run = function () { t.variableDeclarator(className, constructor) ])); + var closureArgs = []; + var closureParams = []; + // if (superName) { - // so we're only evaluating it once - var superRef = this.scope.generateUidBasedOnNode(superName, this.file); - body.unshift(t.variableDeclaration("var", [ - t.variableDeclarator(superRef, superName) - ])); - superName = superRef; + closureArgs.push(superName); + + if (!t.isIdentifier(superName)) { + var superRef = this.scope.generateUidBasedOnNode(superName, this.file); + body.unshift(t.variableDeclaration("var", [ + t.variableDeclarator(superRef, superName) + ])); + superName = superRef; + } + + closureParams.push(superName); this.superName = superName; body.push(t.expressionStatement(t.callExpression(file.addHelper("inherits"), [className, superName]))); @@ -84,8 +92,8 @@ Class.prototype.run = function () { } else { body.push(t.returnStatement(className)); init = t.callExpression( - t.functionExpression(null, [], t.blockStatement(body)), - [] + t.functionExpression(null, closureParams, t.blockStatement(body)), + closureArgs ); } @@ -212,25 +220,12 @@ Class.prototype.superProperty = function (property, isStatic, isComputed) { this.file.addHelper("get"), [ t.callExpression( - t.memberExpression( - t.identifier("Object"), - t.identifier("getPrototypeOf"), - false - ), + t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")), [ - isStatic ? - this.className : - t.memberExpression( - this.className, - t.identifier("prototype"), - false - ) + isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype")) ] ), - isComputed ? - property : - t.literal(property.name), - t.thisExpression() + isComputed ? property : t.literal(property.name), t.thisExpression() ] ); }; 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 67abcf3cb4..c556d7c015 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js @@ -38,8 +38,7 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var Test = (function () { - var _Foo = Foo; +var Test = (function (Foo) { var Test = function Test() { var _get2, _get3, _get4, _get5; woops["super"].test(); @@ -53,7 +52,7 @@ var Test = (function () { (_get5 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get5, [this, "test"].concat(_slice.call(arguments))); }; - _inherits(Test, _Foo); + _inherits(Test, Foo); Test.prototype.test = function () { var _get6, _get7; @@ -70,5 +69,4 @@ var Test = (function () { }; return Test; -})(); - +})(Foo); 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 5c06e689e1..fe231fa303 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js @@ -37,15 +37,13 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var Test = (function () { - var _Foo = Foo; +var Test = (function (Foo) { var Test = function Test() { _get(Object.getPrototypeOf(Test.prototype), "test", this); _get(Object.getPrototypeOf(Test.prototype), "test", this).whatever; }; - _inherits(Test, _Foo); + _inherits(Test, Foo); return Test; -})(); - +})(Foo); 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 24595cd8ba..ce7a061321 100644 --- a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js @@ -37,19 +37,17 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var Test = (function () { - var _Foo = Foo; +var Test = (function (Foo) { var Test = function Test() { _get(Object.getPrototypeOf(Test.prototype), "test", this).whatever(); _get(Object.getPrototypeOf(Test.prototype), "test", this).call(this); }; - _inherits(Test, _Foo); + _inherits(Test, Foo); Test.test = function () { return _get(Object.getPrototypeOf(Test), "wow", this).call(this); }; return Test; -})(); - +})(Foo); diff --git a/test/fixtures/transformation/es6-classes/constructor/expected.js b/test/fixtures/transformation/es6-classes/constructor/expected.js index cc5264d5b6..ce1a7be09c 100644 --- a/test/fixtures/transformation/es6-classes/constructor/expected.js +++ b/test/fixtures/transformation/es6-classes/constructor/expected.js @@ -19,13 +19,12 @@ var Test = function Test() { this.state = "test"; }; -var Foo = (function () { - var _Bar = Bar; +var Foo = (function (Bar) { var Foo = function Foo() { this.state = "test"; }; - _inherits(Foo, _Bar); + _inherits(Foo, Bar); return Foo; -})(); +})(Bar); 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 a8ecac411b..929f727aed 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 @@ -15,7 +15,7 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var BaseController = (function () { +var BaseController = (function (_Chaplin$Controller) { var _Chaplin$Controller = Chaplin.Controller; var BaseController = function BaseController() { if (Object.getPrototypeOf(BaseController) !== null) { @@ -26,9 +26,9 @@ var BaseController = (function () { _inherits(BaseController, _Chaplin$Controller); return BaseController; -})(); +})(Chaplin.Controller); -var BaseController2 = (function () { +var BaseController2 = (function (_Chaplin$Controller$Another) { var _Chaplin$Controller$Another = Chaplin.Controller.Another; var BaseController2 = function BaseController2() { if (Object.getPrototypeOf(BaseController2) !== null) { @@ -39,4 +39,4 @@ var BaseController2 = (function () { _inherits(BaseController2, _Chaplin$Controller$Another); return 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 fdd356cf96..bd9234406b 100644 --- a/test/fixtures/transformation/es6-classes/super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class/expected.js @@ -15,15 +15,14 @@ var _inherits = function (child, parent) { if (parent) child.__proto__ = parent; }; -var Test = (function () { - var _Foo = Foo; +var Test = (function (Foo) { var Test = function Test() { if (Object.getPrototypeOf(Test) !== null) { Object.getPrototypeOf(Test).apply(this, arguments); } }; - _inherits(Test, _Foo); + _inherits(Test, Foo); return Test; -})(); +})(Foo); diff --git a/test/fixtures/transformation/optional-proto-to-assign/class/expected.js b/test/fixtures/transformation/optional-proto-to-assign/class/expected.js index b6517c4263..59892c84f0 100644 --- a/test/fixtures/transformation/optional-proto-to-assign/class/expected.js +++ b/test/fixtures/transformation/optional-proto-to-assign/class/expected.js @@ -25,15 +25,14 @@ var _inherits = function (child, parent) { if (parent) _defaults(child, parent); }; -var Foo = (function () { - var _Bar = Bar; +var Foo = (function (Bar) { var Foo = function Foo() { if (Object.getPrototypeOf(Foo) !== null) { Object.getPrototypeOf(Foo).apply(this, arguments); } }; - _inherits(Foo, _Bar); + _inherits(Foo, Bar); return Foo; -})(); +})(Bar);