statements[++j] = stat;
defs = stat;
}
+ continue;
} else if (stat instanceof AST_Exit) {
stat.value = join_assigns_expr(stat.value);
} else if (stat instanceof AST_For) {
if (exprs) {
CHANGED = true;
stat.init = exprs.length ? make_sequence(stat.init, exprs) : null;
- statements[++j] = stat;
} else if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) {
if (stat.init) {
prev.definitions = prev.definitions.concat(stat.init.definitions);
}
- stat.init = prev;
- statements[j] = stat;
+ defs = stat.init = prev;
+ statements[j] = merge_defns(stat);
CHANGED = true;
+ continue;
} else if (defs && stat.init && defs.TYPE == stat.init.TYPE && declarations_only(stat.init)) {
defs.definitions = defs.definitions.concat(stat.init.definitions);
stat.init = null;
- statements[++j] = stat;
CHANGED = true;
- } else {
- statements[++j] = stat;
+ } else if (stat.init instanceof AST_Definitions) {
+ defs = stat.init;
}
} else if (stat instanceof AST_ForIn) {
stat.object = join_assigns_expr(stat.object);
if (!exprs.length) continue;
stat.body = make_sequence(stat.body, exprs);
}
- statements[++j] = stat;
} else if (stat instanceof AST_Switch) {
stat.expression = join_assigns_expr(stat.expression);
} else if (stat instanceof AST_With) {
stat.expression = join_assigns_expr(stat.expression);
- } else {
- statements[++j] = stat;
}
+ statements[++j] = defs ? merge_defns(stat) : stat;
}
statements.length = j + 1;
function join_assigns_expr(value) {
- statements[++j] = stat;
var exprs = join_assigns(prev, value, 1);
if (!exprs) return value;
CHANGED = true;
if (exprs[exprs.length - 1] !== tail) exprs.push(tail.left);
return make_sequence(value, exprs);
}
+
+ function merge_defns(stat) {
+ return stat.transform(new TreeTransformer(function(node, descend, in_list) {
+ if (node instanceof AST_Definitions) {
+ if (defs === node) return node;
+ if (defs.TYPE != node.TYPE) return node;
+ var parent = this.parent();
+ if (parent instanceof AST_ForIn && parent.init === node) return node;
+ if (!declarations_only(node)) return node;
+ defs.definitions = defs.definitions.concat(node.definitions);
+ CHANGED = true;
+ return in_list ? List.skip : make_node(AST_EmptyStatement, node);
+ }
+ if (node instanceof AST_Scope) return node;
+ if (!(node instanceof AST_Statement)) return node;
+ }));
+ }
}
}