From 1d42e9ad556d6024f58ff3a4fee6df310cbcf34d Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 4 Sep 2022 03:10:31 +0100 Subject: [PATCH] fix corner case in `collapse_vars` (#5644) fixes #5643 --- lib/compress.js | 8 ++++++-- test/compress/collapse_vars.js | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index c7f13ed7..56a962c6 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3416,13 +3416,17 @@ Compressor.prototype.compress = function(node) { if (def.references.length - def.replaced == referenced) return true; if (!def.fixed) return false; if (!lhs.fixed) return false; + var assigns = lhs.fixed.assigns; var matched = 0; if (!all(def.references, function(ref, index) { var fixed = ref.fixed; if (!fixed) return false; if (fixed.to_binary || fixed.to_prefix) return false; - if (fixed === lhs.fixed) matched++; - return true; + if (fixed === lhs.fixed) { + matched++; + return true; + } + return assigns && fixed.assigns && assigns[0] !== fixed.assigns[0]; })) return false; if (matched != referenced) return false; verify_ref = true; diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 4b2fe126..2f96bd77 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -10098,3 +10098,26 @@ issue_5638_4: { } expect_stdout: "foo 42" } + +issue_5643: { + options = { + collapse_vars: true, + reduce_vars: true, + toplevel: true, + } + input: { + var a = 3, b; + a *= 7; + b = !!this; + console || console.log(b); + console.log(a * ++b); + } + expect: { + var a = 3, b; + a *= 7; + b = !!this; + console || console.log(b); + console.log(a * ++b); + } + expect_stdout: "42" +}