From ea1049890245d0f8b671640038d38a03ca7ef078 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 23 Nov 2021 10:00:47 +0000 Subject: [PATCH] fix corner case in `hoist_vars` (#5188) fixes #5187 --- lib/compress.js | 7 +++++-- test/compress/hoist_vars.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index f59a9966..f431b16d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -9071,10 +9071,13 @@ merge(Compressor.prototype, { right: value, }); a.push(assign); - name.fixed = function() { + var fixed = function() { return assign.right; }; - name.fixed.assigns = [ assign ]; + fixed.assigns = [ assign ]; + fixed.direct_access = def.direct_access; + fixed.escaped = def.escaped; + name.fixed = fixed; def.references.forEach(function(ref) { var assigns = ref.fixed && ref.fixed.assigns; if (assigns && assigns[0] === defn) assigns[0] = assign; diff --git a/test/compress/hoist_vars.js b/test/compress/hoist_vars.js index 983b47ed..fde3639d 100644 --- a/test/compress/hoist_vars.js +++ b/test/compress/hoist_vars.js @@ -418,3 +418,32 @@ issue_4898: { } expect_stdout: "PASS" } + +issue_5187: { + options = { + hoist_props: true, + hoist_vars: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + function f() { + var a = 42; + do { + var b = { 0: a++ }; + } while (console.log(b[b ^= 0])); + } + f(); + } + expect: { + (function() { + var b, a = 42; + do { + b = { 0: a++ }; + } while (console.log(b[b ^= 0])); + })(); + } + expect_stdout: "42" +}