Use a functionExpression with class decorators. Fixes #1161.

This commit is contained in:
dignifiedquire 2015-04-07 11:32:33 +02:00
parent 0cb5a7c91e
commit 148aa3f96d
3 changed files with 37 additions and 11 deletions

View File

@ -30,7 +30,7 @@ var collectPropertyReferencesVisitor = {
} }
if (this.isReferenced() && scope.getBinding(node.name) === state.scope.getBinding(node.name)) { if (this.isReferenced() && scope.getBinding(node.name) === state.scope.getBinding(node.name)) {
state.references[node.name] = true;; state.references[node.name] = true;
} }
} }
} }
@ -140,7 +140,7 @@ class ClassTransformer {
var constructorBody = this.constructorBody = t.blockStatement([]); var constructorBody = this.constructorBody = t.blockStatement([]);
var constructor; var constructor;
if (this.className) { if (this.className && !this.node.decorators) {
constructor = t.functionDeclaration(this.className, [], constructorBody); constructor = t.functionDeclaration(this.className, [], constructorBody);
body.push(constructor); body.push(constructor);
} else { } else {
@ -189,6 +189,14 @@ class ClassTransformer {
} }
if (this.className) { if (this.className) {
if (decorators) {
constructor = t.functionExpression(this.className, constructor.params, constructorBody);
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(classRef, constructor)
]));
}
// named class with only a constructor // named class with only a constructor
if (body.length === 1) return t.toExpression(body[0]); if (body.length === 1) return t.toExpression(body[0]);
} else { } else {

View File

@ -12,3 +12,10 @@ var Foo2 = @bar class Foo {
var Bar2 = @foo @bar class Bar { var Bar2 = @foo @bar class Bar {
}; };
@foo
class Baz{
constructor(baz) {
this.baz = baz;
}
}

View File

@ -1,18 +1,18 @@
"use strict"; "use strict";
var Foo = (function () { var Foo = (function () {
function Foo() { var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo); babelHelpers.classCallCheck(this, Foo);
} };
Foo = foo(Foo) || Foo; Foo = foo(Foo) || Foo;
return Foo; return Foo;
})(); })();
var Bar = (function () { var Bar = (function () {
function Bar() { var Bar = function Bar() {
babelHelpers.classCallCheck(this, Bar); babelHelpers.classCallCheck(this, Bar);
} };
Bar = foo(Bar) || Bar; Bar = foo(Bar) || Bar;
Bar = bar(Bar) || Bar; Bar = bar(Bar) || Bar;
@ -20,20 +20,31 @@ var Bar = (function () {
})(); })();
var Foo2 = (function () { var Foo2 = (function () {
function Foo() { var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo); babelHelpers.classCallCheck(this, Foo);
} };
Foo = bar(Foo) || Foo; Foo = bar(Foo) || Foo;
return Foo; return Foo;
})(); })();
var Bar2 = (function () { var Bar2 = (function () {
function Bar() { var Bar = function Bar() {
babelHelpers.classCallCheck(this, Bar); babelHelpers.classCallCheck(this, Bar);
} };
Bar = foo(Bar) || Bar; Bar = foo(Bar) || Bar;
Bar = bar(Bar) || Bar; Bar = bar(Bar) || Bar;
return Bar; return Bar;
})(); })();
var Baz = (function () {
var Baz = function Baz(baz) {
babelHelpers.classCallCheck(this, Baz);
this.baz = baz;
};
Baz = foo(Baz) || Baz;
return Baz;
})();