From 79cfac77bdee04021db9f60111f55005919f7b67 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 17 Jan 2018 13:58:27 +0800 Subject: [PATCH] extend `join_vars` & `sequences` (#2798) --- lib/compress.js | 88 +++++++++++++++++++++---------------- test/compress/properties.js | 48 ++++++++++++++++++-- test/compress/sequences.js | 18 ++++++++ 3 files changed, 111 insertions(+), 43 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index cd5651a2..5fd49cf1 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1662,37 +1662,34 @@ merge(Compressor.prototype, { for (var i = 0; i < statements.length; i++) { var stat = statements[i]; if (prev) { - if (stat instanceof AST_For && !(stat.init instanceof AST_Definitions)) { - var abort = false; - prev.body.walk(new TreeWalker(function(node) { - if (abort || node instanceof AST_Scope) return true; - if (node instanceof AST_Binary && node.operator == "in") { - abort = true; - return true; - } - })); - if (!abort) { - if (stat.init) stat.init = cons_seq(stat.init); - else { - stat.init = prev.body; - n--; - CHANGED = true; + if (stat instanceof AST_Exit) { + stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat).transform(compressor)); + } else if (stat instanceof AST_For) { + if (!(stat.init instanceof AST_Definitions)) { + var abort = false; + prev.body.walk(new TreeWalker(function(node) { + if (abort || node instanceof AST_Scope) return true; + if (node instanceof AST_Binary && node.operator == "in") { + abort = true; + return true; + } + })); + if (!abort) { + if (stat.init) stat.init = cons_seq(stat.init); + else { + stat.init = prev.body; + n--; + CHANGED = true; + } } } - } - else if (stat instanceof AST_If) { + } else if (stat instanceof AST_ForIn) { + stat.object = cons_seq(stat.object); + } else if (stat instanceof AST_If) { stat.condition = cons_seq(stat.condition); - } - else if (stat instanceof AST_With) { + } else if (stat instanceof AST_Switch) { stat.expression = cons_seq(stat.expression); - } - else if (stat instanceof AST_Exit && stat.value) { - stat.value = cons_seq(stat.value); - } - else if (stat instanceof AST_Exit) { - stat.value = cons_seq(make_node(AST_Undefined, stat).transform(compressor)); - } - else if (stat instanceof AST_Switch) { + } else if (stat instanceof AST_With) { stat.expression = cons_seq(stat.expression); } } @@ -1775,18 +1772,7 @@ merge(Compressor.prototype, { defs = stat; } } else if (stat instanceof AST_Exit) { - var exprs = join_object_assignments(prev, stat.value); - if (exprs) { - CHANGED = true; - if (exprs.length) { - stat.value = make_sequence(stat.value, exprs); - } else if (stat.value instanceof AST_Sequence) { - stat.value = stat.value.tail_node().left; - } else { - stat.value = stat.value.left; - } - } - statements[++j] = stat; + stat.value = extract_object_assignments(stat.value); } else if (stat instanceof AST_For) { var exprs = join_object_assignments(prev, stat.init); if (exprs) { @@ -1808,6 +1794,10 @@ merge(Compressor.prototype, { } else { statements[++j] = stat; } + } else if (stat instanceof AST_ForIn) { + stat.object = extract_object_assignments(stat.object); + } else if (stat instanceof AST_If) { + stat.condition = extract_object_assignments(stat.condition); } else if (stat instanceof AST_SimpleStatement) { var exprs = join_object_assignments(prev, stat.body); if (exprs) { @@ -1816,11 +1806,31 @@ merge(Compressor.prototype, { stat.body = make_sequence(stat.body, exprs); } statements[++j] = stat; + } else if (stat instanceof AST_Switch) { + stat.expression = extract_object_assignments(stat.expression); + } else if (stat instanceof AST_With) { + stat.expression = extract_object_assignments(stat.expression); } else { statements[++j] = stat; } } statements.length = j + 1; + + function extract_object_assignments(value) { + statements[++j] = stat; + var exprs = join_object_assignments(prev, value); + if (exprs) { + CHANGED = true; + if (exprs.length) { + return make_sequence(value, exprs); + } else if (value instanceof AST_Sequence) { + return value.tail_node().left; + } else { + return value.left; + } + } + return value; + } } } diff --git a/test/compress/properties.js b/test/compress/properties.js index 7df53d39..933774d5 100644 --- a/test/compress/properties.js +++ b/test/compress/properties.js @@ -1189,7 +1189,7 @@ join_object_assignments_3: { expect_stdout: "PASS" } -join_object_assignments_4: { +join_object_assignments_return_1: { options = { join_vars: true, } @@ -1213,7 +1213,7 @@ join_object_assignments_4: { expect_stdout: "foo" } -join_object_assignments_5: { +join_object_assignments_return_2: { options = { join_vars: true, } @@ -1239,7 +1239,7 @@ join_object_assignments_5: { expect_stdout: "bar" } -join_object_assignments_6: { +join_object_assignments_return_3: { options = { join_vars: true, } @@ -1271,7 +1271,7 @@ join_object_assignments_6: { ] } -join_object_assignments_7: { +join_object_assignments_for: { options = { join_vars: true, } @@ -1298,3 +1298,43 @@ join_object_assignments_7: { "3", ] } + +join_object_assignments_if: { + options = { + join_vars: true, + } + input: { + console.log(function() { + var o = {}; + if (o.a = "PASS") return o.a; + }()) + } + expect: { + console.log(function() { + var o = { a: "PASS" }; + if (o.a) return o.a; + }()); + } + expect_stdout: "PASS" +} + +join_object_assignments_forin: { + options = { + join_vars: true, + } + input: { + console.log(function() { + var o = {}; + for (var a in o.a = "PASS", o) + return o[a]; + }()) + } + expect: { + console.log(function() { + var o = { a: "PASS" }; + for (var a in o) + return o[a]; + }()); + } + expect_stdout: "PASS" +} diff --git a/test/compress/sequences.js b/test/compress/sequences.js index 3d12fb0b..2c900796 100644 --- a/test/compress/sequences.js +++ b/test/compress/sequences.js @@ -859,3 +859,21 @@ for_init_var: { } expect_stdout: "PASS" } + +forin: { + options = { + sequences: true, + } + input: { + var o = []; + o.push("PASS"); + for (var a in o) + console.log(o[a]); + } + expect: { + var o = []; + for (var a in o.push("PASS"), o) + console.log(o[a]); + } + expect_stdout: "PASS" +} -- 2.34.1