From 5e2cd07d6f6866b1ddae51a4c9b280d2cd527973 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 23 Jan 2018 01:28:09 +0800 Subject: [PATCH 1/5] handle duplicate function declarations correctly (#2837) fixes #2836 --- lib/scope.js | 2 +- test/compress/reduce_vars.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/scope.js b/lib/scope.js index af852bb1..6c883c66 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -305,7 +305,7 @@ AST_Scope.DEFMETHOD("find_variable", function(name){ AST_Scope.DEFMETHOD("def_function", function(symbol, init){ var def = this.def_variable(symbol, init); - if (!def.init) def.init = init; + if (!def.init || def.init instanceof AST_Defun) def.init = init; this.functions.set(symbol.name, def); return def; }); diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 33175d1b..4009c35b 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -5360,3 +5360,26 @@ issue_2799_2: { } expect_stdout: "PASS" } + +issue_2836: { + options = { + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function f() { + return "FAIL"; + } + console.log(f()); + function f() { + return "PASS"; + } + } + expect: { + console.log(function() { + return "PASS"; + }()); + } + expect_stdout: "PASS" +} From ec4202590d040942639e01eb8ae83bf7be7180dd Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 23 Jan 2018 02:49:54 +0800 Subject: [PATCH 2/5] drop assignments to constant expressions only (#2839) fixes #2838 --- lib/compress.js | 6 ++++-- test/compress/pure_getters.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 2803b85c..a8fa0e1c 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3469,8 +3469,10 @@ merge(Compressor.prototype, { while (left instanceof AST_PropAccess) { left = left.expression; } - if (left instanceof AST_Symbol) return this; - return this.right.drop_side_effect_free(compressor); + if (left.is_constant_expression(compressor.find_parent(AST_Scope))) { + return this.right.drop_side_effect_free(compressor); + } + return this; }); def(AST_Conditional, function(compressor){ var consequent = this.consequent.drop_side_effect_free(compressor); diff --git a/test/compress/pure_getters.js b/test/compress/pure_getters.js index 80b0e8ea..4e9ae4f4 100644 --- a/test/compress/pure_getters.js +++ b/test/compress/pure_getters.js @@ -694,3 +694,30 @@ issue_2678: { } expect_stdout: "PASS" } + +issue_2838: { + options = { + pure_getters: true, + side_effects: true, + } + input: { + function f(a, b) { + (a || b).c = "PASS"; + (function() { + return f(a, b); + }).prototype.foo = "bar"; + } + var o = {}; + f(null, o); + console.log(o.c); + } + expect: { + function f(a, b) { + (a || b).c = "PASS"; + } + var o = {}; + f(null, o); + console.log(o.c); + } + expect_stdout: "PASS" +} From 95cfce68eae6173a76e8f01fae08a91f5d8996d2 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 23 Jan 2018 05:45:45 +0800 Subject: [PATCH 3/5] backport of #2835 (#2841) --- lib/compress.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index a8fa0e1c..33b43134 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1642,7 +1642,7 @@ merge(Compressor.prototype, { var stat = null; for (var i = 0, len = block.body.length; i < len; i++) { var line = block.body[i]; - if (line instanceof AST_Definitions && declarations_only(line)) { + if (line instanceof AST_Var && declarations_only(line)) { decls.push(line); } else if (stat) { return false; From 193612ac67ef269d6b05fdf61c9638a5eebd76f7 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 26 Jan 2018 14:21:11 +0800 Subject: [PATCH 4/5] fix accounting after conversion to assignment (#2847) Missing reference to `AST_SymbolRef` created by `unused` causes `collapse_vars` to misbehave. fixes #2846 --- lib/compress.js | 4 +++- test/compress/drop-unused.js | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 33b43134..07128aff 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3038,9 +3038,11 @@ merge(Compressor.prototype, { if (var_defs.length > 1 && (!def.value || sym.orig.indexOf(def.name) > sym.eliminated)) { compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); if (def.value) { + var ref = make_node(AST_SymbolRef, def.name, def.name); + sym.references.push(ref); var assign = make_node(AST_Assign, def, { operator: "=", - left: make_node(AST_SymbolRef, def.name, def.name), + left: ref, right: def.value }); if (fixed_ids[sym.id] === def) { diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 0ac7bb33..e99d7ab1 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -1692,3 +1692,30 @@ issue_2768: { } expect_stdout: "PASS undefined" } + +issue_2846: { + options = { + collapse_vars: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function f(a, b) { + var a = 0; + b && b(a); + return a++; + } + var c = f(); + console.log(c); + } + expect: { + var c = function(a, b) { + a = 0; + b && b(a); + return a++; + }(); + console.log(c); + } + expect_stdout: "0" +} From 4eb4cb656cc4f3850c403689cf29e529f4c67944 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 27 Jan 2018 12:56:34 +0000 Subject: [PATCH 5/5] v3.3.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a8ea8408..e6beb738 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "http://lisperator.net/uglifyjs", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", - "version": "3.3.8", + "version": "3.3.9", "engines": { "node": ">=0.8.0" },