clean up default constructor in derived classes - fixes #1748

This commit is contained in:
Sebastian McKenzie
2015-06-14 23:44:21 +01:00
parent 52c3c143f9
commit eba9f0ffbd
15 changed files with 55 additions and 94 deletions

View File

@@ -0,0 +1,3 @@
(function () {
super(...arguments);
})

View File

@@ -1,3 +0,0 @@
if (SUPER_NAME != null) {
SUPER_NAME.apply(this, arguments);
}

View File

@@ -1,3 +0,0 @@
if (SUPER_NAME != null) {
SUPER_NAME.apply(this, arguments);
}

View File

@@ -1,5 +0,0 @@
if (SUPER_NAME != null) {
var NATIVE_REF = new SUPER_NAME(...arguments);
NATIVE_REF.__proto__ = CLASS_NAME.prototype;
return NATIVE_REF;
}

View File

@@ -251,20 +251,43 @@ class ClassTransformer {
}
}
/**
* https://www.youtube.com/watch?v=fWNaR-rxAic
*/
constructorMeMaybe() {
if (!this.hasSuper) return;
var hasConstructor = false;
var paths = this.path.get("body.body");
for (var path of (paths: Array)) {
hasConstructor = path.equals("kind", "constructor");
if (hasConstructor) break;
}
if (!hasConstructor) {
this.path.get("body").unshiftContainer("body", t.methodDefinition(
t.identifier("constructor"),
util.template("class-derived-default-constructor"),
"constructor"
));
}
}
/**
* Description
*/
buildBody() {
this.constructorMeMaybe();
var constructorBody = this.constructorBody;
var classBody = this.node.body.body;
var classBodyPaths = this.path.get("body.body");
var body = this.body;
var classBodyPaths = this.path.get("body").get("body");
for (var i = 0; i < classBody.length; i++) {
var node = classBody[i];
var path = classBodyPaths[i];
for (var path of (classBodyPaths: Array)) {
var node = path.node;
if (node.decorators) {
memoiseDecorators(node.decorators, this.scope);
@@ -297,16 +320,6 @@ class ClassTransformer {
}
}
// we have no constructor, but we're a derived class
if (!this.hasConstructor && this.hasSuper) {
var helperName = "class-super-constructor-call";
if (this.isLoose) helperName += "-loose";
constructorBody.body.push(util.template(helperName, {
CLASS_NAME: this.classRef,
SUPER_NAME: this.superName
}, true));
}
//
this.placePropertyInitializers();

View File

@@ -16,7 +16,7 @@ export function insertBefore(nodes) {
} else if (this.isNodeType("Expression") || (this.parentPath.isForStatement() && this.key === "init")) {
if (this.node) nodes.push(this.node);
this.replaceExpressionWithStatements(nodes);
} else if (this.isNodeType("Statement") || !this.type) {
} else {
this._maybePopFromStatements(nodes);
if (Array.isArray(this.container)) {
return this._containerInsertBefore(nodes);
@@ -26,8 +26,6 @@ export function insertBefore(nodes) {
} else {
throw new Error("We don't know what to do with this node type. We were previously a Statement but we can't fit in here?");
}
} else {
throw new Error("No clue what to do with this node type.");
}
return [this];
@@ -94,7 +92,7 @@ export function insertAfter(nodes) {
nodes.push(t.expressionStatement(temp));
}
this.replaceExpressionWithStatements(nodes);
} else if (this.isNodeType("Statement") || !this.type) {
} else {
this._maybePopFromStatements(nodes);
if (Array.isArray(this.container)) {
return this._containerInsertAfter(nodes);
@@ -104,8 +102,6 @@ export function insertAfter(nodes) {
} else {
throw new Error("We don't know what to do with this node type. We were previously a Statement but we can't fit in here?");
}
} else {
throw new Error("No clue what to do with this node type.");
}
return [this];