aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2018-01-16 17:03:12 +0800
committerGitHub <noreply@github.com>2018-01-16 17:03:12 +0800
commitb4aef753e7f65c0919c6c40b2b28d9f149bc81ed (patch)
tree33b2c9b5ea33dd349237b8e743cdc6415669b51c
parent424173d311cd9c35a10a276abdab1de902158ac2 (diff)
downloadtracifyjs-b4aef753e7f65c0919c6c40b2b28d9f149bc81ed.tar.gz
tracifyjs-b4aef753e7f65c0919c6c40b2b28d9f149bc81ed.zip
general improvements around `AST_ForIn` (#2796)
- compress using `collapse_vars` - remove unused `name` - simplify `loop_body`
-rw-r--r--lib/ast.js3
-rw-r--r--lib/compress.js10
-rw-r--r--lib/parse.js2
-rw-r--r--test/compress/collapse_vars.js27
4 files changed, 35 insertions, 7 deletions
diff --git a/lib/ast.js b/lib/ast.js
index 65918675..4e41659c 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -267,11 +267,10 @@ var AST_For = DEFNODE("For", "init condition step", {
}
}, AST_IterationStatement);
-var AST_ForIn = DEFNODE("ForIn", "init name object", {
+var AST_ForIn = DEFNODE("ForIn", "init object", {
$documentation: "A `for ... in` statement",
$propdoc: {
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"
},
_walk: function(visitor) {
diff --git a/lib/compress.js b/lib/compress.js
index fb1c2fdb..cd5651a2 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -847,9 +847,8 @@ merge(Compressor.prototype, {
};
function loop_body(x) {
- if (x instanceof AST_Switch) return x;
- if (x instanceof AST_For || x instanceof AST_ForIn || x instanceof AST_DWLoop) {
- return (x.body instanceof AST_BlockStatement ? x.body : x);
+ if (x instanceof AST_IterationStatement) {
+ return x.body instanceof AST_BlockStatement ? x.body : x;
}
return x;
};
@@ -1193,6 +1192,11 @@ merge(Compressor.prototype, {
if (!(expr.body instanceof AST_Block)) {
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) {
extract_candidates(expr.condition);
if (!(expr.body instanceof AST_Block)) {
diff --git a/lib/parse.js b/lib/parse.js
index 001587bc..eba833dc 100644
--- a/lib/parse.js
+++ b/lib/parse.js
@@ -1054,12 +1054,10 @@ function parse($TEXT, options) {
};
function for_in(init) {
- var lhs = init instanceof AST_Var ? init.definitions[0].name : null;
var obj = expression(true);
expect(")");
return new AST_ForIn({
init : init,
- name : lhs,
object : obj,
body : in_loop(statement)
});
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index f252a7f4..9597b67f 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -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",
+ ]
+}