turn inserted helper declarations into function declarations if possible

This commit is contained in:
Sebastian McKenzie 2015-05-03 17:22:51 +01:00
parent 63b44a3e6e
commit b30bdf2294
2 changed files with 18 additions and 8 deletions

View File

@ -232,14 +232,14 @@ class CodeGenerator {
this.map.mark(node, "end");
if (opts.after) opts.after();
this.format.concise = oldConcise;
newline(false);
this.printTrailingComments(node, parent);
} else {
throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node && node.constructor.name)}`);
}
this.format.concise = oldConcise;
}
printJoin(print, nodes, opts = {}) {

View File

@ -370,13 +370,23 @@ export default class File {
}
var ref = util.template("helper-" + name);
ref._compact = true;
var uid = this.declarations[name] = this.scope.generateUidIdentifier(name);
this.scope.push({
id: uid,
init: ref,
unique: true
});
if (t.isFunctionExpression(ref) && !ref.id) {
ref.body._compact = true;
ref._generated = true;
ref.id = uid;
ref.type = "FunctionDeclaration";
this.path.unshiftContainer("body", ref);
} else {
ref._compact = true;
this.scope.push({
id: uid,
init: ref,
unique: true
});
}
return uid;
}