From 5ae04b35452693e886a7f836e62e3290b08016a1 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 16 Mar 2017 13:22:26 +0800 Subject: [PATCH] make `collapse_vars` consistent with `toplevel` (#1608) fixes #1605 --- lib/compress.js | 4 ++- test/compress/collapse_vars.js | 47 +++++++++++++++++++++++++++++++++- test/compress/issue-973.js | 1 + test/mocha/cli.js | 2 +- test/mocha/minify.js | 1 + 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 49b618e7..dac1f364 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -521,6 +521,7 @@ merge(Compressor.prototype, { var self = compressor.self(); var var_defs_removed = false; + var toplevel = compressor.option("toplevel"); for (var stat_index = statements.length; --stat_index >= 0;) { var stat = statements[stat_index]; if (stat instanceof AST_Definitions) continue; @@ -558,7 +559,8 @@ merge(Compressor.prototype, { // Only interested in cases with just one reference to the variable. var def = self.find_variable && self.find_variable(var_name); - if (!def || !def.references || def.references.length !== 1 || var_name == "arguments") { + if (!def || !def.references || def.references.length !== 1 + || var_name == "arguments" || (!toplevel && def.global)) { side_effects_encountered = true; continue; } diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 6d7e2d9f..558a9ee0 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -1152,7 +1152,8 @@ collapse_vars_arguments: { options = { collapse_vars:true, sequences:true, properties:true, dead_code:true, conditionals:true, comparisons:true, evaluate:true, booleans:true, loops:true, unused:true, hoist_funs:true, - keep_fargs:true, if_return:true, join_vars:true, cascade:true, side_effects:true + keep_fargs:true, if_return:true, join_vars:true, cascade:true, side_effects:true, + toplevel:true } input: { var outer = function() { @@ -1335,6 +1336,7 @@ issue_1537: { issue_1562: { options = { collapse_vars: true, + toplevel: true, } input: { var v = 1, B = 2; @@ -1363,3 +1365,46 @@ issue_1562: { for (; f(z + 2) ;) bar(30); } } + +issue_1605_1: { + options = { + collapse_vars: true, + toplevel: false, + } + input: { + function foo(x) { + var y = x; + return y; + } + var o = new Object; + o.p = 1; + } + expect: { + function foo(x) { + return x; + } + var o = new Object; + o.p = 1; + } +} + +issue_1605_2: { + options = { + collapse_vars: true, + toplevel: "vars", + } + input: { + function foo(x) { + var y = x; + return y; + } + var o = new Object; + o.p = 1; + } + expect: { + function foo(x) { + return x; + } + (new Object).p = 1; + } +} diff --git a/test/compress/issue-973.js b/test/compress/issue-973.js index 0e040922..30f886a8 100644 --- a/test/compress/issue-973.js +++ b/test/compress/issue-973.js @@ -50,6 +50,7 @@ this_binding_conditionals: { this_binding_collapse_vars: { options = { collapse_vars: true, + toplevel: true, }; input: { var c = a; c(); diff --git a/test/mocha/cli.js b/test/mocha/cli.js index fa952d91..2b44c901 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -152,7 +152,7 @@ describe("bin/uglifyjs", function () { }); }); it("Should process inline source map", function(done) { - var command = uglifyjscmd + ' test/input/issue-520/input.js -cm toplevel --in-source-map inline --source-map-inline'; + var command = uglifyjscmd + ' test/input/issue-520/input.js -mc toplevel --in-source-map inline --source-map-inline'; exec(command, function (err, stdout) { if (err) throw err; diff --git a/test/mocha/minify.js b/test/mocha/minify.js index 51c46b28..a4587cb7 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -78,6 +78,7 @@ describe("minify", function() { }); it("Should process inline source map", function() { var code = Uglify.minify("./test/input/issue-520/input.js", { + compress: { toplevel: true }, inSourceMap: "inline", sourceMapInline: true }).code + "\n"; -- 2.34.1