From 8b69a3d18e38a63565dce1758dba79615b54cc79 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 2 Jul 2017 04:28:11 +0800 Subject: [PATCH] drop argument value after `collapse_vars` (#2190) --- lib/compress.js | 14 +++++++++--- test/compress/collapse_vars.js | 41 +++++++++++++++++++++++++++++++++- test/compress/drop-unused.js | 2 +- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index dffdd6ed..eb3ba756 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -902,6 +902,14 @@ merge(Compressor.prototype, { } function remove_candidate(expr) { + if (expr.name instanceof AST_SymbolFunarg) { + var index = compressor.self().argnames.indexOf(expr.name); + var args = compressor.parent().args; + if (args[index]) args[index] = make_node(AST_Number, args[index], { + value: 0 + }); + return true; + } var found = false; return statements[stat_index].transform(new TreeTransformer(function(node, descend, in_list) { if (found) return node; @@ -3205,7 +3213,7 @@ merge(Compressor.prototype, { var value = stat.value; if (!value || value.is_constant_expression()) { var args = self.args.concat(value || make_node(AST_Undefined, self)); - return make_sequence(self, args).transform(compressor); + return make_sequence(self, args).optimize(compressor); } } if (exp instanceof AST_Function) { @@ -3246,12 +3254,12 @@ merge(Compressor.prototype, { } if (value) { var args = self.args.concat(value); - return make_sequence(self, args).transform(compressor); + return make_sequence(self, args).optimize(compressor); } } if (compressor.option("side_effects") && all(exp.body, is_empty)) { var args = self.args.concat(make_node(AST_Undefined, self)); - return make_sequence(self, args).transform(compressor); + return make_sequence(self, args).optimize(compressor); } } if (compressor.option("drop_console")) { diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index f3eb7816..24f8ffa3 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -2187,7 +2187,7 @@ compound_assignment: { expect_stdout: "4" } -issue_2187: { +issue_2187_1: { options = { collapse_vars: true, unused: true, @@ -2217,3 +2217,42 @@ issue_2187: { "2", ] } + +issue_2187_2: { + options = { + collapse_vars: true, + unused: true, + } + input: { + var b = 1; + console.log(function(a) { + return a && ++b; + }(b--)); + } + expect: { + var b = 1; + console.log(function(a) { + return b-- && ++b; + }()); + } + expect_stdout: "1" +} + +issue_2187_3: { + options = { + collapse_vars: true, + inline: true, + unused: true, + } + input: { + var b = 1; + console.log(function(a) { + return a && ++b; + }(b--)); + } + expect: { + var b = 1; + console.log(b-- && ++b); + } + expect_stdout: "1" +} diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index e1acdc10..a44107ae 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -1113,7 +1113,7 @@ issue_2105: { options = { collapse_vars: true, inline: true, - passes: 2, + passes: 3, reduce_vars: true, side_effects: true, unused: true, -- 2.34.1