diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-04-18 11:52:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-18 18:52:44 +0800 |
commit | 60d4e7b09fb962598a9f9f0fb64f92416a9f7e9c (patch) | |
tree | cecb9e23567d6b75d97933be4002975f3ca5cc52 | |
parent | b38838c6bf064bfe54f0419a5eb7cd4298bfc3a3 (diff) | |
download | tracifyjs-60d4e7b09fb962598a9f9f0fb64f92416a9f7e9c.tar.gz tracifyjs-60d4e7b09fb962598a9f9f0fb64f92416a9f7e9c.zip |
enhance `unused` (#3794)
-rw-r--r-- | lib/compress.js | 45 | ||||
-rw-r--r-- | test/compress/asm.js | 3 | ||||
-rw-r--r-- | test/compress/collapse_vars.js | 3 | ||||
-rw-r--r-- | test/compress/drop-unused.js | 20 | ||||
-rw-r--r-- | test/compress/keep_fargs.js | 3 | ||||
-rw-r--r-- | test/compress/reduce_vars.js | 10 |
6 files changed, 54 insertions, 30 deletions
diff --git a/lib/compress.js b/lib/compress.js index d6f6a710..3aa9f0bc 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4134,6 +4134,7 @@ merge(Compressor.prototype, { // side effects, we can cascade the init. code // into the next one, or next statement. var side_effects = []; + var duplicated = 0; node.definitions.forEach(function(def) { if (def.value) def.value = def.value.transform(tt); var sym = def.name.definition(); @@ -4142,24 +4143,17 @@ merge(Compressor.prototype, { def.value = def.value.drop_side_effect_free(compressor); } var var_defs = var_defs_by_id.get(sym.id); - if (var_defs.length > 1 && (!def.value || sym.orig.indexOf(def.name) > sym.eliminated)) { - AST_Node.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: ref, - right: def.value - }); - if (fixed_ids[sym.id] === def) { - fixed_ids[sym.id] = assign; - } - side_effects.push(assign.transform(tt)); + if (var_defs.length > 1) { + if (!def.value) { + AST_Node.warn("Dropping duplicated declaration of variable {name} [{file}:{line},{col}]", template(def.name)); + remove(var_defs, def); + sym.eliminated++; + return; + } + if (sym.orig.indexOf(def.name) > sym.eliminated) { + remove(var_defs, def); + duplicated++; } - remove(var_defs, def); - sym.eliminated++; - return; } if (!def.value) { head.push(def); @@ -4220,7 +4214,22 @@ merge(Compressor.prototype, { return !def || fn.name && def === fn.name.definition(); } }); - if (head.length > 0 || tail.length > 0) { + if (head.length == 0 && tail.length == duplicated) { + [].unshift.apply(side_effects, tail.map(function(def) { + AST_Node.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); + var sym = def.name.definition(); + var ref = make_node(AST_SymbolRef, def.name, def.name); + sym.references.push(ref); + var assign = make_node(AST_Assign, def, { + operator: "=", + left: ref, + right: def.value + }); + if (fixed_ids[sym.id] === def) fixed_ids[sym.id] = assign; + sym.eliminated++; + return assign.transform(tt); + })); + } else if (head.length > 0 || tail.length > 0) { node.definitions = head.concat(tail); body.push(node); } diff --git a/test/compress/asm.js b/test/compress/asm.js index 0ea60874..54cf90f4 100644 --- a/test/compress/asm.js +++ b/test/compress/asm.js @@ -91,8 +91,7 @@ asm_mixed: { function no_asm_GeometricMean(stdlib, foreign, buffer) { function logSum(start, end) { start |= 0, end |= 0; - var sum = 0, p = 0, q = 0; - for (p = start << 3, q = end << 3; (0 | p) < (0 | q); p = p + 8 | 0) sum += +log(values[p >> 3]); + for (var sum = 0, p = 0, q = 0, p = start << 3, q = end << 3; (0 | p) < (0 | q); p = p + 8 | 0) sum += +log(values[p >> 3]); return +sum; } function geometricMean(start, end) { diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 1c3af6fa..17d9c981 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -2570,8 +2570,7 @@ chained_3: { } expect: { console.log(function(a, b) { - var c = 1; - c = b; + var c = 1, c = b; b++; return c; }(0, 2)); diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 99ab7b40..0f4c6dc6 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -2444,3 +2444,23 @@ issue_3746: { } expect_stdout: "PASS" } + +drop_duplicated_side_effects: { + options = { + toplevel: true, + unused: true, + } + input: { + var a = 0; + for (var i = 1; i--;) + var a = 0, b = ++a; + console.log(a); + } + expect: { + var a = 0; + for (var i = 1; i--;) + a = 0, ++a; + console.log(a); + } + expect_stdout: "1" +} diff --git a/test/compress/keep_fargs.js b/test/compress/keep_fargs.js index 85fcb0bf..c2a4ceef 100644 --- a/test/compress/keep_fargs.js +++ b/test/compress/keep_fargs.js @@ -1226,8 +1226,7 @@ chained_3: { } expect: { console.log(function(b) { - var c = 1; - c = b; + var c = 1, c = b; b++; return c; }(2)); diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 4e11de48..6be1171f 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -5434,8 +5434,7 @@ lvalues_def_1: { } expect: { var b = 1; - var a = b++; - b = NaN; + var a = b++, b = NaN; console.log(a, b); } expect_stdout: "1 NaN" @@ -5454,8 +5453,7 @@ lvalues_def_2: { } expect: { var b = 1; - var a = b += 1; - b = NaN; + var a = b += 1, b = NaN; console.log(a, b); } expect_stdout: "2 NaN" @@ -6867,9 +6865,9 @@ issue_3666: { } finally { for (;!a;) a++; - var b = a = "PASS"; + a = "PASS"; } - console.log(a, b); + console.log(a, "PASS"); } expect_stdout: "PASS PASS" } |