Skip to content

Commit 6669ea1

Browse files
authoredJun 25, 2024··
fix corner case in hoist_vars (#5864)
fixes #5863
1 parent 205a1d1 commit 6669ea1

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed
 

‎lib/compress.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -8484,7 +8484,8 @@ Compressor.prototype.compress = function(node) {
84848484
&& self.find_variable(sym.name) === sym.definition();
84858485
})) return node;
84868486
node.definitions.forEach(function(defn) {
8487-
vars.set(defn.name.name, defn);
8487+
var name = defn.name.name;
8488+
if (!vars.has(name)) vars.set(name, defn);
84888489
});
84898490
var seq = node.to_assignments();
84908491
if (p instanceof AST_ForEnumeration && p.init === node) {
@@ -8511,13 +8512,14 @@ Compressor.prototype.compress = function(node) {
85118512
return !ref.in_arg;
85128513
})) vars.del(argname.name);
85138514
});
8514-
vars.each(function(defn, name) {
8515+
vars.each(function(defn) {
8516+
var name = defn.name;
85158517
defn = defn.clone();
8516-
defn.name = defn.name.clone();
8518+
defn.name = name.clone();
85178519
defn.value = null;
85188520
defns.push(defn);
8519-
vars.set(name, defn);
8520-
defn.name.definition().orig.unshift(defn.name);
8521+
var orig = name.definition().orig;
8522+
orig.splice(orig.indexOf(name), 0, defn.name);
85218523
});
85228524
if (defns.length > 0) hoisted.push(make_node(AST_Var, self, { definitions: defns }));
85238525
}
@@ -10348,8 +10350,8 @@ Compressor.prototype.compress = function(node) {
1034810350
}
1034910351
});
1035010352
def.references.push(name);
10353+
def.assignments++;
1035110354
}
10352-
def.assignments++;
1035310355
def.eliminated++;
1035410356
def.single_use = false;
1035510357
return a;

‎test/compress/default-values.js

+28
Original file line numberDiff line numberDiff line change
@@ -3107,3 +3107,31 @@ issue_5774: {
31073107
expect_stdout: "PASS"
31083108
node_version: ">=6"
31093109
}
3110+
3111+
issue_5863: {
3112+
options = {
3113+
collapse_vars: true,
3114+
hoist_vars: true,
3115+
}
3116+
input: {
3117+
console.log(typeof function f(a = function() {
3118+
f = 42;
3119+
return f;
3120+
}()) {
3121+
var f;
3122+
var f;
3123+
return a;
3124+
}());
3125+
}
3126+
expect: {
3127+
console.log(typeof function f(a = function() {
3128+
f = 42;
3129+
return f;
3130+
}()) {
3131+
var f;
3132+
return a;
3133+
}());
3134+
}
3135+
expect_stdout: "function"
3136+
node_version: ">=6"
3137+
}

0 commit comments

Comments
 (0)
Please sign in to comment.