diff --git a/src/babel/transformation/transformers/es6/classes.js b/src/babel/transformation/transformers/es6/classes.js index 9b01d45642..294fb79394 100644 --- a/src/babel/transformation/transformers/es6/classes.js +++ b/src/babel/transformation/transformers/es6/classes.js @@ -168,7 +168,6 @@ class ClassTransformer { var closureArgs = []; // - if (this.hasSuper) { closureArgs.push(superName); @@ -180,30 +179,30 @@ class ClassTransformer { } // - - this.buildBody(); - var decorators = this.node.decorators; - var classCheckRef = classRef; - if (decorators) { - classCheckRef = this.scope.generateUidIdentifier(classRef); + // create a class reference to use later on + this.classRef = this.scope.generateUidIdentifier(classRef); + + // this is so super calls and the decorators have access to the raw function + body.unshift(t.variableDeclaration("var", [ + t.variableDeclarator(this.classRef, classRef) + ])); } + // + this.buildBody(); + + // make sure this class isn't directly called constructorBody.body.unshift(t.expressionStatement(t.callExpression(file.addHelper("class-call-check"), [ t.thisExpression(), - classCheckRef + this.classRef ]))); // if (decorators) { - if (this.className) { - body.push(t.variableDeclaration("var", [ - t.variableDeclarator(classCheckRef, classRef) - ])); - } - + // reverse the decorators so we execute them in the right order decorators = decorators.reverse(); for (var i = 0; i < decorators.length; i++) { @@ -219,6 +218,8 @@ class ClassTransformer { } if (this.isNativeSuper) { + // we've determined this is inheriting from a native class so return the constructed + // instance constructorBody.body.push(t.returnStatement(this.nativeSuperRef)); } diff --git a/test/core/fixtures/transformation/es7.decorators/class-modules/expected.js b/test/core/fixtures/transformation/es7.decorators/class-modules/expected.js index a061a0b3de..67dac7e982 100644 --- a/test/core/fixtures/transformation/es7.decorators/class-modules/expected.js +++ b/test/core/fixtures/transformation/es7.decorators/class-modules/expected.js @@ -9,11 +9,12 @@ var _foo = require("foo"); var _foo2 = babelHelpers.interopRequireWildcard(_foo); var Foo = (function () { + var _Foo = Foo; + function Foo() { babelHelpers.classCallCheck(this, _Foo); } - var _Foo = Foo; Foo = _foo2["default"](Foo) || Foo; return Foo; })(); diff --git a/test/core/fixtures/transformation/es7.decorators/class-super/actual.js b/test/core/fixtures/transformation/es7.decorators/class-super/actual.js new file mode 100644 index 0000000000..14770c1129 --- /dev/null +++ b/test/core/fixtures/transformation/es7.decorators/class-super/actual.js @@ -0,0 +1,12 @@ +@bar +class Foo extends Bar { + constructor() { + super(); + } +} + +var Foo2 = @bar class extends Bar { + constructor() { + super(); + } +}; diff --git a/test/core/fixtures/transformation/es7.decorators/class-super/expected.js b/test/core/fixtures/transformation/es7.decorators/class-super/expected.js new file mode 100644 index 0000000000..b5c10fd6d1 --- /dev/null +++ b/test/core/fixtures/transformation/es7.decorators/class-super/expected.js @@ -0,0 +1,28 @@ +"use strict"; + +var Foo = (function (_Bar) { + var _Foo = Foo; + + function Foo() { + babelHelpers.classCallCheck(this, _Foo); + + babelHelpers.get(Object.getPrototypeOf(_Foo.prototype), "constructor", this).call(this); + } + + babelHelpers.inherits(Foo, _Bar); + Foo = bar(Foo) || Foo; + return Foo; +})(Bar); + +var Foo2 = (function (_Bar2) { + var _class = function Foo2() { + babelHelpers.classCallCheck(this, _class2); + + babelHelpers.get(Object.getPrototypeOf(_class2.prototype), "constructor", this).call(this); + }; + + var _class2 = _class; + babelHelpers.inherits(_class, _Bar2); + _class = bar(_class) || _class; + return _class; +})(Bar); diff --git a/test/core/fixtures/transformation/es7.decorators/class/expected.js b/test/core/fixtures/transformation/es7.decorators/class/expected.js index 96ff2b4054..1ae24bcda1 100644 --- a/test/core/fixtures/transformation/es7.decorators/class/expected.js +++ b/test/core/fixtures/transformation/es7.decorators/class/expected.js @@ -1,55 +1,60 @@ "use strict"; var Foo = (function () { + var _Foo = Foo; + function Foo() { babelHelpers.classCallCheck(this, _Foo); } - var _Foo = Foo; Foo = foo(Foo) || Foo; return Foo; })(); var Bar = (function () { + var _Bar = Bar; + function Bar() { babelHelpers.classCallCheck(this, _Bar); } - var _Bar = Bar; Bar = bar(Bar) || Bar; Bar = foo(Bar) || Bar; return Bar; })(); var Foo2 = (function () { + var _Foo2 = Foo; + function Foo() { babelHelpers.classCallCheck(this, _Foo2); } - var _Foo2 = Foo; Foo = bar(Foo) || Foo; return Foo; })(); var Bar2 = (function () { + var _Bar2 = Bar; + function Bar() { babelHelpers.classCallCheck(this, _Bar2); } - var _Bar2 = Bar; Bar = bar(Bar) || Bar; Bar = foo(Bar) || Bar; return Bar; })(); var Baz = (function () { + var _Baz = Baz; + function Baz(baz) { babelHelpers.classCallCheck(this, _Baz); this.baz = baz; } - var _Baz = Baz; Baz = foo(Baz) || Baz; return Baz; })();