Skip to content

Commit

Permalink
fix corner case in reduce_vars (#5913)
Browse files Browse the repository at this point in the history
fixes #5912
  • Loading branch information
alexlamsl authored Aug 7, 2024
1 parent 4661b34 commit 69f4e3a
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 20 deletions.
38 changes: 18 additions & 20 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,14 +646,12 @@ Compressor.prototype.compress = function(node) {
scope.may_call_this = undefined;
}

function push(tw, sequential) {
function push(tw, sequential, conditional) {
var defined_ids = Object.create(tw.defined_ids);
var safe_ids = Object.create(tw.safe_ids);
if (!sequential) {
defined_ids.seq = Object.create(null);
safe_ids.seq = {};
}
if (!sequential || conditional) defined_ids.seq = Object.create(null);
tw.defined_ids = defined_ids;
var safe_ids = Object.create(tw.safe_ids);
if (!sequential) safe_ids.seq = {};
tw.safe_ids = safe_ids;
}

Expand Down Expand Up @@ -855,7 +853,7 @@ Compressor.prototype.compress = function(node) {
var scanner = new TreeWalker(function(node) {
if (node instanceof AST_DefaultValue) {
reset_flags(node);
push(tw, true);
push(tw, true, true);
node.value.walk(tw);
pop(tw);
var save = fixed;
Expand Down Expand Up @@ -1042,7 +1040,7 @@ Compressor.prototype.compress = function(node) {
return walk_lazy();
}
var safe = safe_to_read(tw, ld);
if (lazy) push(tw, true);
if (lazy) push(tw, true, true);
right.walk(tw);
if (lazy) pop(tw);
if (safe && !left.in_arg && safe_to_assign(tw, ld)) {
Expand Down Expand Up @@ -1127,7 +1125,7 @@ Compressor.prototype.compress = function(node) {
function walk_lazy() {
if (!lazy) return;
left.walk(tw);
push(tw, true);
push(tw, true, true);
right.walk(tw);
pop(tw);
return true;
Expand All @@ -1136,7 +1134,7 @@ Compressor.prototype.compress = function(node) {
def(AST_Binary, function(tw) {
if (!lazy_op[this.operator]) return;
this.left.walk(tw);
push(tw, true);
push(tw, true, true);
this.right.walk(tw);
pop(tw);
return true;
Expand Down Expand Up @@ -1166,7 +1164,7 @@ Compressor.prototype.compress = function(node) {
}
exp.walk(tw);
var optional = node.optional;
if (optional) push(tw, true);
if (optional) push(tw, true, true);
node.args.forEach(function(arg) {
arg.walk(tw);
});
Expand Down Expand Up @@ -1251,16 +1249,16 @@ Compressor.prototype.compress = function(node) {
});
def(AST_Conditional, function(tw) {
this.condition.walk(tw);
push(tw, true);
push(tw, true, true);
this.consequent.walk(tw);
pop(tw);
push(tw, true);
push(tw, true, true);
this.alternative.walk(tw);
pop(tw);
return true;
});
def(AST_DefaultValue, function(tw) {
push(tw, true);
push(tw, true, true);
this.value.walk(tw);
pop(tw);
this.name.walk(tw);
Expand Down Expand Up @@ -1349,11 +1347,11 @@ Compressor.prototype.compress = function(node) {
});
def(AST_If, function(tw) {
this.condition.walk(tw);
push(tw, true);
push(tw, true, true);
this.body.walk(tw);
pop(tw);
if (this.alternative) {
push(tw, true);
push(tw, true, true);
this.alternative.walk(tw);
pop(tw);
}
Expand Down Expand Up @@ -1396,7 +1394,7 @@ Compressor.prototype.compress = function(node) {
var expr = node.expression;
if (node.optional) {
expr.walk(tw);
push(tw, true);
push(tw, true, true);
node.property.walk(tw);
pop(tw);
} else {
Expand All @@ -1420,15 +1418,15 @@ Compressor.prototype.compress = function(node) {
branch.expression.walk(tw);
if (first) {
first = false;
push(tw, true);
push(tw, true, true);
}
});
if (!first) pop(tw);
walk_body(node, tw);
return true;
});
def(AST_SwitchBranch, function(tw) {
push(tw, true);
push(tw, true, true);
walk_body(this, tw);
pop(tw);
return true;
Expand Down Expand Up @@ -1567,7 +1565,7 @@ Compressor.prototype.compress = function(node) {
walk_body(node, tw);
pop(tw);
if (node.bcatch) {
push(tw, true);
push(tw, true, true);
node.bcatch.walk(tw);
pop(tw);
}
Expand Down
32 changes: 32 additions & 0 deletions test/compress/optional-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,35 @@ issue_5905: {
expect_stdout: "PASS"
node_version: ">=14"
}

issue_5912: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
var a, b = {};
b = b.p;
a?.[b.q];
try {
b.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
var a, b = {};
b = b.p;
a?.[b.q];
try {
b.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
node_version: ">=14"
}
101 changes: 101 additions & 0 deletions test/compress/side_effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,3 +967,104 @@ issue_5860_keep_3: {
}
expect_stdout: "PASS"
}

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

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

issue_5912_3: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
var a = {};
a = a.p;
try {
console;
} catch (e) {
a.q;
}
try {
a.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
var a = {};
a = a.p;
try {
console;
} catch (e) {
a.q;
}
try {
a.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}
74 changes: 74 additions & 0 deletions test/compress/switches.js
Original file line number Diff line number Diff line change
Expand Up @@ -1798,3 +1798,77 @@ issue_5892_2: {
}
expect_stdout: "PASS"
}

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

issue_5912_2: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
var a, b = {};
b = b.p;
switch (a) {
case void 0:
case b.q:
}
try {
b.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
var a, b = {};
b = b.p;
switch (a) {
case void 0:
case b.q:
}
try {
b.r;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

0 comments on commit 69f4e3a

Please sign in to comment.