Skip to content

Commit

Permalink
fix corner case in reduce_vars (#5891)
Browse files Browse the repository at this point in the history
fixes #5890
  • Loading branch information
alexlamsl authored Jul 25, 2024
1 parent 7a4fb85 commit 1247576
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
21 changes: 15 additions & 6 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1397,15 +1397,24 @@ Compressor.prototype.compress = function(node) {
first = false;
push(tw, true);
}
})
});
if (!first) pop(tw);
walk_body(node, tw);
var defined_ids = tw.defined_ids;
var safe_ids = tw.safe_ids;
node.body.forEach(function(branch) {
push(tw, true);
branch.walk(tw);
if (aborts(branch)) {
tw.defined_ids = defined_ids;
tw.safe_ids = safe_ids;
}
});
tw.defined_ids = defined_ids;
tw.safe_ids = safe_ids;
return true;
});
def(AST_SwitchBranch, function(tw) {
push(tw, true);
walk_body(this, tw);
pop(tw);
return true;
});
def(AST_SymbolCatch, function() {
Expand Down Expand Up @@ -1501,7 +1510,7 @@ Compressor.prototype.compress = function(node) {
}
mark_fn_def(tw, d, value);
});
def(AST_Template, function(tw, descend) {
def(AST_Template, function(tw) {
var node = this;
var tag = node.tag;
if (!tag) return;
Expand Down Expand Up @@ -1549,7 +1558,7 @@ Compressor.prototype.compress = function(node) {
if (node.bfinally) node.bfinally.walk(tw);
return true;
});
def(AST_Unary, function(tw, descend) {
def(AST_Unary, function(tw) {
var node = this;
if (!UNARY_POSTFIX[node.operator]) return;
var exp = node.expression;
Expand Down
11 changes: 5 additions & 6 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5720,9 +5720,9 @@ issue_3929: {
var abc = function f() {
(function() {
switch (f) {
default:
default:
var abc = 0;
case 0:
case 0:
abc.p;
}
console.log(typeof f);
Expand All @@ -5736,10 +5736,9 @@ issue_3929: {
var abc = function f() {
(function() {
switch (f) {
default:
var abc = 0;
case 0:
abc.p;
default:
case 0:
0..p;
}
console.log(typeof f);
})();
Expand Down
39 changes: 39 additions & 0 deletions test/compress/switches.js
Original file line number Diff line number Diff line change
Expand Up @@ -1688,3 +1688,42 @@ issue_5543_2: {
}
expect_stdout: "PASS"
}

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

0 comments on commit 1247576

Please sign in to comment.