From 58bea676acfccd1d88206656fb370acc9caf113f Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 7 Jan 2022 06:33:42 +0000 Subject: [PATCH] fix corner case in `unused` (#5272) fixes #5271 --- lib/compress.js | 12 +++++------- test/compress/drop-unused.js | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 09b725b1..32cd445b 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6577,7 +6577,6 @@ Compressor.prototype.compress = function(node) { } }); // pass 3: we should drop declarations not in_use - var trim_defns = []; var unused_fn_names = []; var calls_to_drop_args = []; var fns_with_marked_args = []; @@ -6822,14 +6821,16 @@ Compressor.prototype.compress = function(node) { var sym = def.name.definition(); var drop_sym = is_var ? can_drop_symbol(def.name) : is_safe_lexical(sym); if (!drop_sym || !drop_vars || sym.id in in_use_ids) { - if (value && (indexOf_assign(sym, def) < 0 || self_assign(value.tail_node()))) { + var index; + if (value && ((index = indexOf_assign(sym, def)) < 0 || self_assign(value.tail_node()))) { value = value.drop_side_effect_free(compressor); if (value) { AST_Node.warn("Side effects in definition of variable {name} [{file}:{line},{col}]", template(def.name)); side_effects.push(value); } - value = null; - trim_defns.push(def); + def = def.clone(); + def.value = value = null; + if (index >= 0) assign_in_use[sym.id][index] = def; } var old_def, fn; if (!value && !(node instanceof AST_Let)) { @@ -7114,9 +7115,6 @@ Compressor.prototype.compress = function(node) { && self.body[0].value == "use strict") { self.body.length = 0; } - trim_defns.forEach(function(def) { - def.value = null; - }); unused_fn_names.forEach(function(fn) { fn.name = null; }); diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 324c8f37..36aa2a9e 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -3561,3 +3561,29 @@ issue_5224: { } expect_stdout: "Infinity" } + +issue_5271: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function f() { + do { + var a = b = 0 ^ f, b = b; + } while (console.log(42 - b)); + } + f(); + } + expect: { + (function f() { + do { + var b; + b = 0 ^ f; + } while (console.log(42 - b)); + })(); + } + expect_stdout: "42" +}