Skip to content

Commit

Permalink
fix corner case in reduce_vars (#5862)
Browse files Browse the repository at this point in the history
fixes #5860
fixes #5861
  • Loading branch information
alexlamsl authored Jun 20, 2024
1 parent 95d3ede commit 205a1d1
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,6 @@ Compressor.prototype.compress = function(node) {
var scan = ld || left instanceof AST_Destructured;
switch (node.operator) {
case "=":
if (ld) assign(tw, ld);
if (left.equals(right) && !left.has_side_effects(compressor)) {
right.walk(tw);
walk_prop(left);
Expand All @@ -1009,12 +1008,12 @@ Compressor.prototype.compress = function(node) {
case "||=":
case "??=":
var lazy = true;
if (ld) assign(tw, ld);
default:
if (!scan) {
mark_assignment_to_arguments(left);
return walk_lazy();
}
assign(tw, ld);
ld.assignments++;
var fixed = ld.fixed;
if (is_modified(compressor, tw, node, node, 0)) {
Expand Down Expand Up @@ -1083,6 +1082,7 @@ Compressor.prototype.compress = function(node) {
return;
}
var d = sym.definition();
assign(tw, d);
d.assignments++;
if (!fixed || sym.in_arg || !safe_to_assign(tw, d)) {
walk();
Expand Down Expand Up @@ -1613,6 +1613,7 @@ Compressor.prototype.compress = function(node) {
return node.value || make_node(AST_Undefined, node);
}, function(name, fixed) {
var d = name.definition();
assign(tw, d);
if (!d.first_decl && d.references.length == 0) d.first_decl = name;
if (fixed && safe_to_assign(tw, d, true)) {
mark(tw, d);
Expand Down
140 changes: 140 additions & 0 deletions test/compress/side_effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,3 +827,143 @@ keep_access_after_call: {
}
expect_stdout: "PASS"
}

issue_5860_drop_1: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
var a = {};
a.p;
var a;
a.q;
console.log("PASS");
}
expect: {
var a = {};
a.p;
var a;
console.log("PASS");
}
expect_stdout: "PASS"
}

issue_5860_drop_2: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
a = {};
a.p;
var a;
a.q;
console.log("PASS");
}
expect: {
a = {};
a.p;
var a;
console.log("PASS");
}
expect_stdout: "PASS"
}

issue_5860_keep_1: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
var a = {};
a.p;
a.q;
var a = null;
try {
a.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
var a = {};
a.p;
var a = null;
try {
a.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

issue_5860_keep_2: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
a = {};
a.p;
a.q;
var a = null;
try {
a.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
a = {};
a.p;
var a = null;
try {
a.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

issue_5860_keep_3: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
var a = {};
a.p;
a.q;
a = null;
try {
a.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
var a = {};
a.p;
a = null;
try {
a.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

0 comments on commit 205a1d1

Please sign in to comment.