general improvements around AST_ForIn (#2796)

- compress using `collapse_vars`
- remove unused `name`
- simplify `loop_body`
This commit is contained in:
Alex Lam S.L
2018-01-16 17:03:12 +08:00
committed by GitHub
parent 424173d311
commit b4aef753e7
4 changed files with 35 additions and 7 deletions

View File

@@ -267,11 +267,10 @@ var AST_For = DEFNODE("For", "init condition step", {
} }
}, AST_IterationStatement); }, AST_IterationStatement);
var AST_ForIn = DEFNODE("ForIn", "init name object", { var AST_ForIn = DEFNODE("ForIn", "init object", {
$documentation: "A `for ... in` statement", $documentation: "A `for ... in` statement",
$propdoc: { $propdoc: {
init: "[AST_Node] the `for/in` initialization code", init: "[AST_Node] the `for/in` initialization code",
name: "[AST_SymbolRef?] the loop variable, only if `init` is AST_Var",
object: "[AST_Node] the object that we're looping through" object: "[AST_Node] the object that we're looping through"
}, },
_walk: function(visitor) { _walk: function(visitor) {

View File

@@ -847,9 +847,8 @@ merge(Compressor.prototype, {
}; };
function loop_body(x) { function loop_body(x) {
if (x instanceof AST_Switch) return x; if (x instanceof AST_IterationStatement) {
if (x instanceof AST_For || x instanceof AST_ForIn || x instanceof AST_DWLoop) { return x.body instanceof AST_BlockStatement ? x.body : x;
return (x.body instanceof AST_BlockStatement ? x.body : x);
} }
return x; return x;
}; };
@@ -1193,6 +1192,11 @@ merge(Compressor.prototype, {
if (!(expr.body instanceof AST_Block)) { if (!(expr.body instanceof AST_Block)) {
extract_candidates(expr.body); extract_candidates(expr.body);
} }
} else if (expr instanceof AST_ForIn) {
extract_candidates(expr.object);
if (!(expr.body instanceof AST_Block)) {
extract_candidates(expr.body);
}
} else if (expr instanceof AST_If) { } else if (expr instanceof AST_If) {
extract_candidates(expr.condition); extract_candidates(expr.condition);
if (!(expr.body instanceof AST_Block)) { if (!(expr.body instanceof AST_Block)) {

View File

@@ -1054,12 +1054,10 @@ function parse($TEXT, options) {
}; };
function for_in(init) { function for_in(init) {
var lhs = init instanceof AST_Var ? init.definitions[0].name : null;
var obj = expression(true); var obj = expression(true);
expect(")"); expect(")");
return new AST_ForIn({ return new AST_ForIn({
init : init, init : init,
name : lhs,
object : obj, object : obj,
body : in_loop(statement) body : in_loop(statement)
}); });

View File

@@ -4062,3 +4062,30 @@ cascade_statement: {
} }
} }
} }
cascade_forin: {
options = {
collapse_vars: true,
}
input: {
var a;
function f(b) {
return [ b, b, b ];
}
for (var c in a = console, f(a))
console.log(c);
}
expect: {
var a;
function f(b) {
return [ b, b, b ];
}
for (var c in f(a = console))
console.log(c);
}
expect_stdout: [
"0",
"1",
"2",
]
}