Skip to content

Commit

Permalink
improve fix for #5917 (#5921)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Aug 14, 2024
1 parent abefd94 commit 23f98ba
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,19 @@ Compressor.prototype.compress = function(node) {
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) {
var safe_ids = fixed.safe_ids;
switch (safe_ids) {
case null:
case false:
return;
default:
if (safe_ids && safe_ids.seq !== tw.safe_ids.seq) return;
}
}
d.fixed = false;
});
}
Expand Down Expand Up @@ -648,6 +661,7 @@ Compressor.prototype.compress = function(node) {
tw.defined_ids = defined_ids;
var safe_ids = Object.create(tw.safe_ids);
if (!sequential) safe_ids.seq = {};
if (conditional) safe_ids.cond = true;
tw.safe_ids = safe_ids;
}

Expand Down Expand Up @@ -1009,7 +1023,7 @@ Compressor.prototype.compress = function(node) {
walk_assign();
right.parent_scope.resolve().fn_defs.push(right);
right.safe_ids = null;
if (!ld.fixed || !node.write_only) mark_fn_def(tw, ld, right);
if (!ld.fixed || !node.write_only || tw.safe_ids.cond) mark_fn_def(tw, ld, right);
return true;
}
if (scan) {
Expand Down
45 changes: 44 additions & 1 deletion test/compress/pure_getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -1716,7 +1716,7 @@ issue_5856: {
expect_stdout: "PASS"
}

issue_5917: {
issue_5917_1: {
options = {
pure_getters: "strict",
reduce_vars: true,
Expand Down Expand Up @@ -1751,3 +1751,46 @@ issue_5917: {
}
expect_stdout: "PASS"
}

issue_5917_2: {
options = {
passes: 2,
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
toplevel: true,
}
input: {
var b;
if (!console) {
b = function() {};
FAIL(f);
}
function f() {
b.p;
}
try {
f();
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
var b;
if (!console) {
b = function() {};
FAIL(f);
}
function f() {
b.p;
}
try {
f();
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

0 comments on commit 23f98ba

Please sign in to comment.