Skip to content

Commit

Permalink
fix corner case in reduce_vars (#5056)
Browse files Browse the repository at this point in the history
fixes #5055
  • Loading branch information
alexlamsl committed Jul 6, 2021
1 parent 0668fad commit 1fefe3f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
31 changes: 19 additions & 12 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,26 +526,33 @@ merge(Compressor.prototype, {
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) {
if (!HOP(fn, "safe_ids")) return;
var marker = fn.safe_ids;
if (marker === false) return;
if (fn.parent_scope.resolve().may_call_this === return_true) return;
if (marker) {
if (fn.parent_scope.resolve().may_call_this === return_true) {
if (member(fn, tw.fn_visited)) revisit_fn_def(tw, fn);
} else if (marker) {
var visited = member(fn, tw.fn_visited);
if (marker === tw.safe_ids) {
if (!visited) walk_fn_def(tw, fn);
} else if (!visited) {
} else if (visited) {
revisit_fn_def(tw, fn);
} else {
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()) {
fn.safe_ids = false;
} else {
Expand Down
46 changes: 46 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -7725,3 +7725,49 @@ issue_5050: {
"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"
}

0 comments on commit 1fefe3f

Please sign in to comment.