add constructor to body in order that it was supplied in - fixes #1877

This commit is contained in:
Sebastian McKenzie 2015-06-30 10:10:52 +01:00
parent e55ce575cd
commit 092d98fb27
3 changed files with 67 additions and 23 deletions

View File

@ -107,10 +107,11 @@ class ClassTransformer {
this.staticPropBody = []; this.staticPropBody = [];
this.body = []; this.body = [];
this.hasConstructor = false; this.pushedConstructor = false;
this.hasDecorators = false; this.hasConstructor = false;
this.className = this.node.id; this.hasDecorators = false;
this.classRef = this.node.id || this.scope.generateUidIdentifier("class"); this.className = this.node.id;
this.classRef = this.node.id || this.scope.generateUidIdentifier("class");
this.superName = this.node.superClass || t.identifier("Function"); this.superName = this.node.superClass || t.identifier("Function");
this.hasSuper = !!this.node.superClass; this.hasSuper = !!this.node.superClass;
@ -140,7 +141,6 @@ class ClassTransformer {
if (this.className) { if (this.className) {
constructor = t.functionDeclaration(this.className, [], constructorBody); constructor = t.functionDeclaration(this.className, [], constructorBody);
body.push(constructor);
} else { } else {
constructor = t.functionExpression(null, [], constructorBody); constructor = t.functionExpression(null, [], constructorBody);
} }
@ -207,15 +207,6 @@ class ClassTransformer {
if (this.className) { if (this.className) {
// 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 {
// infer class name if this is a nameless class expression
constructor = nameMethod.bare(constructor, this.parent, this.scope) || constructor;
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(classRef, constructor)
]));
t.inheritsComments(body[0], this.node);
} }
// //
@ -258,23 +249,26 @@ class ClassTransformer {
*/ */
constructorMeMaybe() { constructorMeMaybe() {
if (!this.hasSuper) return;
var hasConstructor = false; var hasConstructor = false;
var paths = this.path.get("body.body"); var paths = this.path.get("body.body");
for (var path of (paths: Array)) { for (var path of (paths: Array)) {
hasConstructor = path.equals("kind", "constructor"); hasConstructor = path.equals("kind", "constructor");
if (hasConstructor) break; if (hasConstructor) break;
} }
if (hasConstructor) return;
if (!hasConstructor) { var constructor;
this.path.get("body").unshiftContainer("body", t.methodDefinition( if (this.hasSuper) {
t.identifier("constructor"), constructor = util.template("class-derived-default-constructor");
util.template("class-derived-default-constructor"), } else {
"constructor" constructor = t.functionExpression(null, [], t.blockStatement());
));
} }
this.path.get("body").unshiftContainer("body", t.methodDefinition(
t.identifier("constructor"),
constructor,
"constructor"
));
} }
/** /**
@ -568,5 +562,23 @@ class ClassTransformer {
construct.params = fn.params; construct.params = fn.params;
t.inherits(construct.body, fn.body); t.inherits(construct.body, fn.body);
// push constructor to body
if (!this.pushedConstructor) {
this.pushedConstructor = true;
if (this.className) {
this.body.push(construct);
} else {
// infer class name if this is a nameless class expression
this.constructor = nameMethod.bare(construct, this.parent, this.scope) || construct;
this.body.push(t.variableDeclaration("var", [
t.variableDeclarator(classRef, constructor)
]));
t.inheritsComments(this.body[0], this.node);
}
}
} }
} }

View File

@ -0,0 +1,13 @@
class x {
f() {
1
2
3
}
constructor() {
4
5
6
}
}

View File

@ -0,0 +1,19 @@
"use strict";
var x = (function () {
x.prototype.f = function f() {
1;
2;
3;
};
function x() {
4;
5;
6;
babelHelpers.classCallCheck(this, x);
}
return x;
})();