Skip to content

Commit 1247576

Browse files
authoredJul 25, 2024··
fix corner case in reduce_vars (#5891)
fixes #5890
1 parent 7a4fb85 commit 1247576

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed
 

‎lib/compress.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -1397,15 +1397,24 @@ Compressor.prototype.compress = function(node) {
13971397
first = false;
13981398
push(tw, true);
13991399
}
1400-
})
1400+
});
14011401
if (!first) pop(tw);
1402-
walk_body(node, tw);
1402+
var defined_ids = tw.defined_ids;
1403+
var safe_ids = tw.safe_ids;
1404+
node.body.forEach(function(branch) {
1405+
push(tw, true);
1406+
branch.walk(tw);
1407+
if (aborts(branch)) {
1408+
tw.defined_ids = defined_ids;
1409+
tw.safe_ids = safe_ids;
1410+
}
1411+
});
1412+
tw.defined_ids = defined_ids;
1413+
tw.safe_ids = safe_ids;
14031414
return true;
14041415
});
14051416
def(AST_SwitchBranch, function(tw) {
1406-
push(tw, true);
14071417
walk_body(this, tw);
1408-
pop(tw);
14091418
return true;
14101419
});
14111420
def(AST_SymbolCatch, function() {
@@ -1501,7 +1510,7 @@ Compressor.prototype.compress = function(node) {
15011510
}
15021511
mark_fn_def(tw, d, value);
15031512
});
1504-
def(AST_Template, function(tw, descend) {
1513+
def(AST_Template, function(tw) {
15051514
var node = this;
15061515
var tag = node.tag;
15071516
if (!tag) return;
@@ -1549,7 +1558,7 @@ Compressor.prototype.compress = function(node) {
15491558
if (node.bfinally) node.bfinally.walk(tw);
15501559
return true;
15511560
});
1552-
def(AST_Unary, function(tw, descend) {
1561+
def(AST_Unary, function(tw) {
15531562
var node = this;
15541563
if (!UNARY_POSTFIX[node.operator]) return;
15551564
var exp = node.expression;

‎test/compress/functions.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -5720,9 +5720,9 @@ issue_3929: {
57205720
var abc = function f() {
57215721
(function() {
57225722
switch (f) {
5723-
default:
5723+
default:
57245724
var abc = 0;
5725-
case 0:
5725+
case 0:
57265726
abc.p;
57275727
}
57285728
console.log(typeof f);
@@ -5736,10 +5736,9 @@ issue_3929: {
57365736
var abc = function f() {
57375737
(function() {
57385738
switch (f) {
5739-
default:
5740-
var abc = 0;
5741-
case 0:
5742-
abc.p;
5739+
default:
5740+
case 0:
5741+
0..p;
57435742
}
57445743
console.log(typeof f);
57455744
})();

‎test/compress/switches.js

+39
Original file line numberDiff line numberDiff line change
@@ -1688,3 +1688,42 @@ issue_5543_2: {
16881688
}
16891689
expect_stdout: "PASS"
16901690
}
1691+
1692+
issue_5890: {
1693+
options = {
1694+
pure_getters: "strict",
1695+
reduce_vars: true,
1696+
side_effects: true,
1697+
}
1698+
input: {
1699+
var a = {};
1700+
a.p;
1701+
try {
1702+
switch (42) {
1703+
default:
1704+
a = null;
1705+
case false:
1706+
a.q;
1707+
}
1708+
console.log("FAIL");
1709+
} catch (e) {
1710+
console.log("PASS");
1711+
}
1712+
}
1713+
expect: {
1714+
var a = {};
1715+
a.p;
1716+
try {
1717+
switch (42) {
1718+
default:
1719+
a = null;
1720+
case false:
1721+
a.q;
1722+
}
1723+
console.log("FAIL");
1724+
} catch (e) {
1725+
console.log("PASS");
1726+
}
1727+
}
1728+
expect_stdout: "PASS"
1729+
}

0 commit comments

Comments
 (0)
Please sign in to comment.