From 32def5ebf5c9157937dcf802ce71fb9d1d3bc189 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 28 Nov 2017 14:02:39 +0800 Subject: improve synergy between `collapse_vars` & `unused` (#2521) --- lib/compress.js | 44 +++++++++++++-------------------------- test/compress/collapse_vars.js | 47 ++++++++++++++++++++++++++++-------------- test/compress/drop-unused.js | 2 +- test/compress/reduce_vars.js | 4 ++-- 4 files changed, 48 insertions(+), 49 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index b51fdfc7..f49f0b24 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -800,14 +800,6 @@ merge(Compressor.prototype, { || compressor.option("unsafe") && global_names(this.name); }); - function drop_decl(def) { - def.eliminated++; - if (def.orig.length == def.eliminated) { - def.scope.functions.del(def.name); - def.scope.variables.del(def.name); - } - } - function is_identifier_atom(node) { return node instanceof AST_Infinity || node instanceof AST_NaN @@ -1138,7 +1130,6 @@ merge(Compressor.prototype, { if (node === expr || node.body === expr) { found = true; if (node instanceof AST_VarDef) { - drop_decl(node.name.definition()); node.value = null; return node; } @@ -2484,7 +2475,7 @@ merge(Compressor.prototype, { var def = node.name.definition(); if (!(def.id in in_use_ids)) { compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name)); - drop_decl(def); + def.eliminated++; return make_node(AST_EmptyStatement, node); } return node; @@ -2503,10 +2494,17 @@ merge(Compressor.prototype, { if (!drop_vars || sym.id in in_use_ids) { if (def.name instanceof AST_SymbolVar) { var var_defs = var_defs_by_id.get(sym.id); - if (var_defs.length > 1 && !def.value) { + 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) { + merge_sequence(side_effects, make_node(AST_Assign, def, { + operator: "=", + left: make_node(AST_SymbolRef, def.name, def.name), + right: def.value + })); + } remove(var_defs, def); - drop_decl(sym); + sym.eliminated++; return; } } @@ -2539,25 +2537,9 @@ merge(Compressor.prototype, { } else { compressor[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name)); } - drop_decl(sym); + sym.eliminated++; } }); - if (head.length == 0 && tail.length == 1 && tail[0].name instanceof AST_SymbolVar) { - var var_defs = var_defs_by_id.get(tail[0].name.definition().id); - if (var_defs.length > 1) { - var def = tail.pop(); - compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", template(def.name)); - remove(var_defs, def); - side_effects.unshift(make_node(AST_Assign, def, { - operator: "=", - left: make_node(AST_SymbolRef, def.name, def.name), - right: def.value - })); - def = def.name.definition(); - drop_decl(def); - def.replaced--; - } - } if (head.length > 0 || tail.length > 0) { node.definitions = head.concat(tail); body.push(node); @@ -3385,7 +3367,9 @@ merge(Compressor.prototype, { })); if (reduce_vars) name.definition().fixed = false; } - drop_decl(def.name.definition()); + def = def.name.definition(); + def.eliminated++; + def.replaced--; return a; }, []); if (assignments.length == 0) return null; diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index f968ff20..68313354 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -2013,7 +2013,8 @@ chained_3: { } expect: { console.log(function(a, b) { - var c = 1, c = b; + var c = 1; + c = b; b++; return c; }(0, 2)); @@ -2081,7 +2082,7 @@ inner_lvalues: { expect_stdout: true } -double_def: { +double_def_1: { options = { collapse_vars: true, unused: true, @@ -2091,8 +2092,23 @@ double_def: { a(); } expect: { - var a = x; - (a = a && y)(); + var a; + (a = (a = x) && y)(); + } +} + +double_def_2: { + options = { + collapse_vars: true, + toplevel: true, + unused: true, + } + input: { + var a = x, a = a && y; + a(); + } + expect: { + (x && y)(); } } @@ -2201,7 +2217,7 @@ lvalues_def: { } expect: { var a = 0, b = 1; - var a = b++, b = +void 0; + a = b++, b = +void 0; a && a[a++]; console.log(a, b); } @@ -3074,10 +3090,9 @@ issue_2437: { var result = !!req.onreadystatechange; Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {}); return result; - } - else { + } else { var req = new XMLHttpRequest(); - var detectFunc = function () { }; + var detectFunc = function () {}; req.onreadystatechange = detectFunc; var result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc; req.onreadystatechange = null; @@ -3093,9 +3108,9 @@ issue_2437: { return Object.defineProperty(XMLHttpRequest.prototype, "onreadystatechange", xhrDesc || {}), result; } - var req = new XMLHttpRequest(), detectFunc = function() {}; - req.onreadystatechange = detectFunc, - result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc, + var req, detectFunc = function() {}; + (req = new XMLHttpRequest()).onreadystatechange = detectFunc; + result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc; req.onreadystatechange = null; }(); } @@ -3609,15 +3624,15 @@ issue_2497: { expect: { function sample() { if (true) - for (i = 0; i < 1; ++i) - for (k = 0; k < 1; ++k) { + for (var i = 0; i < 1; ++i) + for (var k = 0; k < 1; ++k) { value = 1; value = value ? value + 1 : 0; } else - for (var i = 0; i < 1; ++i) - for (var k = 0; k < 1; ++k) - var value=1; + for (i = 0; i < 1; ++i) + for (k = 0; k < 1; ++k) + var value = 1; } } } diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index eb6f9df4..189a0008 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -1294,8 +1294,8 @@ issue_2288: { expect: { function foo(o) { o.a; - for (i = 0; i < 0; i++); for (var i = 0; i < 0; i++); + for (i = 0; i < 0; i++); } } } diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 02ff5e43..82b5ad6d 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -972,8 +972,8 @@ inner_var_for_2: { } expect: { !function() { - a = 1; - for (var b = 1; --b;) var a = 2; + var a = 1; + for (var b = 1; --b;) a = 2; console.log(a); }(); } -- cgit v1.2.3