fix corner case in reduce_vars (#5056)

fixes #5055
This commit is contained in:
Alex Lam S.L
2021-07-06 19:23:09 +01:00
committed by GitHub
parent 0668fad5e9
commit 1fefe3f1d1
2 changed files with 65 additions and 12 deletions

View File

@@ -526,26 +526,33 @@ merge(Compressor.prototype, {
tw.fn_scanning = was_scanning; tw.fn_scanning = was_scanning;
} }
function revisit_fn_def(tw, fn) {
fn.enclosed.forEach(function(d) {
if (fn.variables.get(d.name) === d) return;
if (safe_to_read(tw, d)) return;
d.single_use = false;
var fixed = d.fixed;
if (typeof fixed == "function") fixed = fixed();
if (fixed instanceof AST_Lambda && HOP(fixed, "safe_ids")) return;
d.fixed = false;
});
}
function mark_fn_def(tw, def, fn) { function mark_fn_def(tw, def, fn) {
if (!HOP(fn, "safe_ids")) return; if (!HOP(fn, "safe_ids")) return;
var marker = fn.safe_ids; var marker = fn.safe_ids;
if (marker === false) return; if (marker === false) return;
if (fn.parent_scope.resolve().may_call_this === return_true) return; if (fn.parent_scope.resolve().may_call_this === return_true) {
if (marker) { if (member(fn, tw.fn_visited)) revisit_fn_def(tw, fn);
} else if (marker) {
var visited = member(fn, tw.fn_visited); var visited = member(fn, tw.fn_visited);
if (marker === tw.safe_ids) { if (marker === tw.safe_ids) {
if (!visited) walk_fn_def(tw, fn); if (!visited) walk_fn_def(tw, fn);
} else if (!visited) { } else if (visited) {
revisit_fn_def(tw, fn);
} else {
fn.safe_ids = false; fn.safe_ids = false;
} else fn.enclosed.forEach(function(d) { }
if (fn.variables.get(d.name) === d) return;
if (safe_to_read(tw, d)) return;
d.single_use = false;
var fixed = d.fixed;
if (typeof fixed == "function") fixed = fixed();
if (fixed instanceof AST_Lambda && HOP(fixed, "safe_ids")) return;
d.fixed = false;
});
} else if (tw.fn_scanning && tw.fn_scanning !== def.scope.resolve()) { } else if (tw.fn_scanning && tw.fn_scanning !== def.scope.resolve()) {
fn.safe_ids = false; fn.safe_ids = false;
} else { } else {

View File

@@ -7725,3 +7725,49 @@ issue_5050: {
"3", "3",
] ]
} }
issue_5055_1: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
}
input: {
var a = "PASS";
function f() {
console.log(a || "FAIL");
}
f(0 && (a = 0)(f(this)));
}
expect: {
var a = "PASS";
function f() {
console.log(a || "FAIL");
}
f(0);
}
expect_stdout: "PASS"
}
issue_5055_2: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = "PASS";
function f() {
console.log(a || "FAIL");
}
f(0 && (a = 0)(f(this)));
}
expect: {
var a = "PASS";
function f() {
console.log(a || "FAIL");
}
f(0 && (a = 0)(f()));
}
expect_stdout: "PASS"
}