fix corner case in hoist_vars (#5627)

fixes #5626
This commit is contained in:
Alex Lam S.L
2022-08-23 17:19:47 +01:00
committed by GitHub
parent 4653e8aec0
commit 4db81065ee
6 changed files with 119 additions and 65 deletions

View File

@@ -8221,59 +8221,9 @@ Compressor.prototype.compress = function(node) {
vars.set(name, defn);
defn.name.definition().orig.unshift(defn.name);
});
if (defns.length > 0) {
// try to merge in assignments
insert_vars(self.body);
hoisted.push(make_node(AST_Var, self, { definitions: defns }));
}
if (defns.length > 0) hoisted.push(make_node(AST_Var, self, { definitions: defns }));
}
self.body = dirs.concat(hoisted, self.body);
function insert_vars(body) {
while (body.length) {
var stat = body[0];
if (stat instanceof AST_SimpleStatement) {
var expr = stat.body, sym, assign;
if (expr instanceof AST_Assign
&& expr.operator == "="
&& (sym = expr.left) instanceof AST_Symbol
&& vars.has(sym.name)) {
var defn = vars.get(sym.name);
if (defn.value) break;
var value = expr.right;
if (value instanceof AST_Sequence) value = value.clone();
defn.value = value;
remove(defns, defn);
defns.push(defn);
body.shift();
continue;
}
if (expr instanceof AST_Sequence
&& (assign = expr.expressions[0]) instanceof AST_Assign
&& assign.operator == "="
&& (sym = assign.left) instanceof AST_Symbol
&& vars.has(sym.name)) {
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;
}
}
if (stat instanceof AST_EmptyStatement) {
body.shift();
continue;
}
if (stat instanceof AST_BlockStatement && !insert_vars(stat.body)) {
body.shift();
continue;
}
break;
}
return body.length;
}
});
function scan_local_returns(fn, transform) {