optimise ES6 tail call transformer to only try TOC on functions that include a call to themselves

This commit is contained in:
Sebastian McKenzie 2015-05-31 15:40:41 +01:00
parent 152ccb2ce8
commit 255c819727

View File

@ -9,18 +9,35 @@ export var metadata = {
group: "builtin-trailing"
};
export function Func/*tion*/(node, parent, scope, file) {
if (node.generator || node.async) return;
var tailCall = new TailCallTransformer(this, scope, file);
tailCall.run();
export function CallExpression(node) {
var callee = this.get("callee");
if (!callee.isIdentifier()) return;
var binding = this.scope.getBinding(callee.node.name);
if (!binding) return;
if (!binding.path.isFunction()) return;
if (binding.identifier !== binding.path.node.id) return;
binding.path.setData("shouldTryTCO", true);
}
export var Func/*tion*/ = {
exit(node, parent, scope, file) {
if (node.generator || node.async) return;
if (!this.getData("shouldTryTCO")) return;
var tailCall = new TailCallTransformer(this, scope, file);
tailCall.run();
}
};
function returnBlock(expr) {
return t.blockStatement([t.returnStatement(expr)]);
}
var visitor = {
enter(node, parent) {
BlockStatement(node, parent) {
if (t.isTryStatement(parent)) {
if (node === parent.block) {
this.skip();