Skip to content

Commit

Permalink
fix corner case in hoist_vars (#5379)
Browse files Browse the repository at this point in the history
fixes #5378
  • Loading branch information
alexlamsl committed Mar 6, 2022
1 parent 042c228 commit 12a6728
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
47 changes: 24 additions & 23 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -7844,15 +7844,15 @@ Compressor.prototype.compress = function(node) {
&& !consts.has(sym.name)
&& self.find_variable(sym.name) === sym.definition();
})) return node;
node.definitions.forEach(function(def) {
vars.set(def.name.name, def);
node.definitions.forEach(function(defn) {
vars.set(defn.name.name, defn);
++vars_found;
});
var seq = node.to_assignments();
if (p instanceof AST_ForEnumeration && p.init === node) {
if (seq) return seq;
var def = node.definitions[0].name;
return make_node(AST_SymbolRef, def, def);
var sym = node.definitions[0].name;
return make_node(AST_SymbolRef, sym, sym);
}
if (p instanceof AST_For && p.init === node) return seq;
if (!seq) return in_list ? List.skip : make_node(AST_EmptyStatement, node);
Expand All @@ -7867,21 +7867,22 @@ Compressor.prototype.compress = function(node) {
self.transform(tt);
if (vars_found > 0) {
// collect only vars which don't show up in self's arguments list
var defs = [];
var defns = [];
if (self instanceof AST_Lambda) self.each_argname(function(argname) {
vars.del(argname.name);
});
vars.each(function(def, name) {
def = def.clone();
def.value = null;
defs.push(def);
vars.set(name, def);
vars.each(function(defn, name) {
defn = defn.clone();
defn.name = defn.name.clone();
defn.value = null;
defns.push(defn);
vars.set(name, defn);
defn.name.definition().orig.unshift(defn.name);
});
if (defs.length > 0) {
if (defns.length > 0) {
// try to merge in assignments
insert_vars(self.body);
defs = make_node(AST_Var, self, { definitions: defs });
hoisted.push(defs);
hoisted.push(make_node(AST_Var, self, { definitions: defns }));
}
}
self.body = dirs.concat(hoisted, self.body);
Expand All @@ -7895,13 +7896,13 @@ Compressor.prototype.compress = function(node) {
&& expr.operator == "="
&& (sym = expr.left) instanceof AST_Symbol
&& vars.has(sym.name)) {
var def = vars.get(sym.name);
if (def.value) break;
var defn = vars.get(sym.name);
if (defn.value) break;
var value = expr.right;
if (value instanceof AST_Sequence) value = value.clone();
def.value = value;
remove(defs, def);
defs.push(def);
defn.value = value;
remove(defns, defn);
defns.push(defn);
body.shift();
continue;
}
Expand All @@ -7910,11 +7911,11 @@ Compressor.prototype.compress = function(node) {
&& assign.operator == "="
&& (sym = assign.left) instanceof AST_Symbol
&& vars.has(sym.name)) {
var def = vars.get(sym.name);
if (def.value) break;
def.value = assign.right;
remove(defs, def);
defs.push(def);
var defn = vars.get(sym.name);
if (defn.value) break;
defn.value = assign.right;
remove(defns, defn);
defns.push(defn);
stat.body = make_sequence(expr, expr.expressions.slice(1));
continue;
}
Expand Down
31 changes: 31 additions & 0 deletions test/compress/hoist_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,34 @@ issue_5195: {
}
expect_stdout: "[object Object]"
}

issue_5378: {
options = {
hoist_vars: true,
inline: true,
toplevel: true,
}
input: {
var a = 2;
while (a--)
(function() {
var b;
var c;
while (console.log(b));
--b;
})();
}
expect: {
var a = 2;
while (a--) {
b = void 0;
var b, c;
while (console.log(b));
--b;
}
}
expect_stdout: [
"undefined",
"undefined",
]
}

0 comments on commit 12a6728

Please sign in to comment.