From d1a9ce647676a25b03842bdbcaa319c65bf5c531 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Tue, 25 Jun 2024 17:15:15 +0300 Subject: [PATCH] fix corner case in `hoist_vars` fixes #5863 --- lib/compress.js | 14 ++++++++------ test/compress/default-values.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 6f86d3709dd..96626b3d141 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -8484,7 +8484,8 @@ Compressor.prototype.compress = function(node) { && self.find_variable(sym.name) === sym.definition(); })) return node; node.definitions.forEach(function(defn) { - vars.set(defn.name.name, defn); + var name = defn.name.name; + if (!vars.has(name)) vars.set(name, defn); }); var seq = node.to_assignments(); if (p instanceof AST_ForEnumeration && p.init === node) { @@ -8511,13 +8512,14 @@ Compressor.prototype.compress = function(node) { return !ref.in_arg; })) vars.del(argname.name); }); - vars.each(function(defn, name) { + vars.each(function(defn) { + var name = defn.name; defn = defn.clone(); - defn.name = defn.name.clone(); + defn.name = name.clone(); defn.value = null; defns.push(defn); - vars.set(name, defn); - defn.name.definition().orig.unshift(defn.name); + var orig = name.definition().orig; + orig.splice(orig.indexOf(name), 0, defn.name); }); if (defns.length > 0) hoisted.push(make_node(AST_Var, self, { definitions: defns })); } @@ -10348,8 +10350,8 @@ Compressor.prototype.compress = function(node) { } }); def.references.push(name); + def.assignments++; } - def.assignments++; def.eliminated++; def.single_use = false; return a; diff --git a/test/compress/default-values.js b/test/compress/default-values.js index aa83b52122e..72044ab3e08 100644 --- a/test/compress/default-values.js +++ b/test/compress/default-values.js @@ -3107,3 +3107,31 @@ issue_5774: { expect_stdout: "PASS" node_version: ">=6" } + +issue_5863: { + options = { + collapse_vars: true, + hoist_vars: true, + } + input: { + console.log(typeof function f(a = function() { + f = 42; + return f; + }()) { + var f; + var f; + return a; + }()); + } + expect: { + console.log(typeof function f(a = function() { + f = 42; + return f; + }()) { + var f; + return a; + }()); + } + expect_stdout: "function" + node_version: ">=6" +}