Skip to content

Commit

Permalink
fix corner case in hoist_vars (#5864)
Browse files Browse the repository at this point in the history
fixes #5863
  • Loading branch information
alexlamsl authored Jun 25, 2024
1 parent 205a1d1 commit 6669ea1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 }));
}
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions test/compress/default-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 6669ea1

Please sign in to comment.