From 8ceb4b0492b1ba99cb43f77d3791ddb7a2131dc5 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 3 Feb 2022 05:13:35 +0000 Subject: [PATCH] fix corner case in `inline` (#5333) fixes #5332 --- lib/compress.js | 25 +++++++++++++---- test/compress/functions.js | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 77d32768..1ea3b553 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -13049,17 +13049,30 @@ Compressor.prototype.compress = function(node) { function init_ref(compressor, name) { var sym = make_node(AST_SymbolRef, name, name); - var def = name.definition(); - def.assignments++; - def.references.push(sym); var assign = make_node(AST_Assign, name, { operator: "=", left: sym, right: make_node(AST_Undefined, name).transform(compressor), }); - sym.fixed = function() { - return assign.right; - }; + var def = name.definition(); + if (def.fixed) { + sym.fixed = function() { + return assign.right; + }; + sym.fixed.assigns = [ assign ]; + var visited = []; + def.references.forEach(function(ref) { + var fixed = ref.fixed; + if (!fixed || !push_uniq(visited, fixed)) return; + if (fixed.assigns) { + fixed.assigns.unshift(assign); + } else { + fixed.assigns = [ assign ]; + } + }); + } + def.assignments++; + def.references.push(sym); return assign; } diff --git a/test/compress/functions.js b/test/compress/functions.js index 7559755b..fa26d408 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -8151,3 +8151,59 @@ issue_5328: { } expect_stdout: "" } + +issue_5332_1: { + options = { + inline: true, + merge_vars: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + do { + var a = {}; + for (A in a) + a; + } while (function() { + console.log(b); + var b = b; + }()); + } + expect: { + do { + var a = {}; + for (A in a); + } while (a = void 0, void console.log(a)); + } + expect_stdout: "undefined" +} + +issue_5332_2: { + options = { + inline: true, + merge_vars: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + do { + var a = 42 in []; + for (A in a) + a; + } while (function() { + console.log(++b); + var b = b; + }()); + } + expect: { + do { + var a = 42 in []; + for (A in a); + } while (a = void 0, void console.log(++a)); + } + expect_stdout: "NaN" +}