From 0f7aa41e339b574ee0f67c093438d356a8a85477 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 24 Oct 2019 01:13:57 +0800 Subject: [PATCH] fix corner case in `collapse_vars` (#3521) fixes #3520 --- lib/compress.js | 6 +++--- test/compress/collapse_vars.js | 38 ++++++++++++++++++++++++++++++++++ test/compress/drop-unused.js | 3 ++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 7202219a..c8eb3487 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1128,6 +1128,8 @@ merge(Compressor.prototype, { if (!stop_if_hit && in_conditional(node, parent)) { stop_if_hit = parent; } + // Skip transient nodes caused by single-use variable replacement + if (node.single_use && parent instanceof AST_VarDef && parent.value === node) return node; // Replace variable with assignment when found var hit_rhs; if (can_replace @@ -3737,9 +3739,7 @@ merge(Compressor.prototype, { def.value = null; head.push(def); } else { - var value = def.value - && !def.value.single_use - && def.value.drop_side_effect_free(compressor); + var value = def.value && def.value.drop_side_effect_free(compressor); if (value) { AST_Node.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", template(def.name)); side_effects.push(value); diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index a793ef6c..5723f6d9 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -6258,3 +6258,41 @@ cond_sequence_return: { } expect_stdout: "2" } + +issue_3520: { + options = { + collapse_vars: true, + reduce_vars: true, + unused: true, + } + input: { + var a = 0; + var b = function(c) { + for (var i = 2; --i >= 0;) { + (function f() { + c = 0; + var i = void 0; + var f = f && f[i]; + })(); + a += b; + c && b++; + } + }(b = 1); + console.log(a); + } + expect: { + var a = 0; + var b = function(c) { + for (var i = 2; --i >= 0;) { + (function() { + c = 0; + var f = f && f[void 0]; + })(); + a += b; + c && b++; + } + }(b = 1); + console.log(a); + } + expect_stdout: "2" +} diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index dfe33cec..f77072b0 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -2121,7 +2121,8 @@ issue_3515_1: { expect: { var c = 0; (function() { - for (var key20 in !(this[c++] = 0)); + this[c++] = 0; + for (var key20 in !0); })(); console.log(c); } -- 2.34.1