From 67278e76c8777d57d40b42151be28b527e692ba2 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 19 Nov 2019 04:26:41 +0800 Subject: [PATCH] fix corner case in `unused` (#3599) fixes #3598 --- lib/compress.js | 4 ++-- test/compress/drop-unused.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index aec31a48..8c1ed5b3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -455,7 +455,7 @@ merge(Compressor.prototype, { if (def.fixed == null) { if (is_arguments(def)) return false; if (def.global && def.name == "arguments") return false; - def.fixed = make_node(AST_Undefined, def.orig); + def.fixed = make_node(AST_Undefined, def.orig[0]); } return true; } @@ -3870,7 +3870,7 @@ merge(Compressor.prototype, { def.value = null; head.push(def); } else { - var value = def.value && def.value.drop_side_effect_free(compressor); + var value = def.value && !def.value.single_use && 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/drop-unused.js b/test/compress/drop-unused.js index 297fa01a..15420d8a 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -2235,3 +2235,34 @@ function_assign: { } expect_stdout: "PASS" } + +issue_3598: { + options = { + collapse_vars: true, + reduce_vars: true, + unused: true, + } + input: { + var a = "FAIL"; + try { + (function() { + var b = void 0; + a = "PASS"; + c.p = 0; + var c = b[!1]; + })(); + } catch (e) {} + console.log(a); + } + expect: { + var a = "FAIL"; + try { + (function() { + a = "PASS"; + var c = (void (c.p = 0))[!1]; + })(); + } catch (e) {} + console.log(a); + } + expect_stdout: "PASS" +} -- 2.34.1