Skip to content

Commit 99946a3

Browse files
authored
fix corner case in dead_code (#5507)
fixes #5506
1 parent 053cb27 commit 99946a3

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

lib/compress.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12329,7 +12329,7 @@ Compressor.prototype.compress = function(node) {
1232912329
parent = compressor.parent(level++);
1233012330
if (parent instanceof AST_Assign) {
1233112331
if (parent.left instanceof AST_SymbolRef && parent.left.definition() === def) {
12332-
if (in_try(level, parent)) break;
12332+
if (in_try(level, parent, !local)) break;
1233312333
return strip_assignment(def);
1233412334
}
1233512335
if (parent.left.match_symbol(function(node) {
@@ -12441,14 +12441,16 @@ Compressor.prototype.compress = function(node) {
1244112441
if (parent instanceof AST_Try) return parent.bfinally ? parent.bfinally === stat : parent.bcatch === stat;
1244212442
}
1244312443

12444-
function in_try(level, node) {
12444+
function in_try(level, node, sync) {
1244512445
var right = self.right;
1244612446
self.right = make_node(AST_Null, right);
1244712447
var may_throw = node.may_throw(compressor);
1244812448
self.right = right;
1244912449
for (var parent; parent = compressor.parent(level++); node = parent) {
1245012450
if (parent === scope) return false;
12451-
if (parent instanceof AST_Try) {
12451+
if (sync && parent instanceof AST_Lambda) {
12452+
if (parent.name || is_async(parent) || is_generator(parent)) return true;
12453+
} else if (parent instanceof AST_Try) {
1245212454
if (parent.bfinally && parent.bfinally !== node) return true;
1245312455
if (may_throw && parent.bcatch && parent.bcatch !== node) return true;
1245412456
}

test/compress/awaits.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,3 +2982,27 @@ issue_5493: {
29822982
expect_stdout: "undefined"
29832983
node_version: ">=8"
29842984
}
2985+
2986+
issue_5506: {
2987+
options = {
2988+
dead_code: true,
2989+
}
2990+
input: {
2991+
console.log(function(a) {
2992+
(async function() {
2993+
a = null in (a = "PASS");
2994+
})();
2995+
return a;
2996+
}("FAIL"));
2997+
}
2998+
expect: {
2999+
console.log(function(a) {
3000+
(async function() {
3001+
a = null in (a = "PASS");
3002+
})();
3003+
return a;
3004+
}("FAIL"));
3005+
}
3006+
expect_stdout: "PASS"
3007+
node_version: ">=8"
3008+
}

test/compress/dead-code.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,3 +1669,41 @@ issue_5106_2: {
16691669
}
16701670
expect_stdout: "PASS"
16711671
}
1672+
1673+
issue_5506: {
1674+
options = {
1675+
dead_code: true,
1676+
}
1677+
input: {
1678+
try {
1679+
(function(a) {
1680+
var b = 1;
1681+
(function f() {
1682+
try {
1683+
b-- && f();
1684+
} catch (c) {}
1685+
console.log(a);
1686+
a = 42 in (a = "bar");
1687+
})();
1688+
})("foo");
1689+
} catch (e) {}
1690+
}
1691+
expect: {
1692+
try {
1693+
(function(a) {
1694+
var b = 1;
1695+
(function f() {
1696+
try {
1697+
b-- && f();
1698+
} catch (c) {}
1699+
console.log(a);
1700+
a = 42 in (a = "bar");
1701+
})();
1702+
})("foo");
1703+
} catch (e) {}
1704+
}
1705+
expect_stdout: [
1706+
"foo",
1707+
"bar",
1708+
]
1709+
}

test/compress/yields.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,3 +1515,35 @@ issue_5456: {
15151515
expect_stdout: "foo"
15161516
node_version: ">=4"
15171517
}
1518+
1519+
issue_5506: {
1520+
options = {
1521+
dead_code: true,
1522+
}
1523+
input: {
1524+
console.log(function(a) {
1525+
var b = function*() {
1526+
a = null in (a = "PASS");
1527+
}();
1528+
try {
1529+
b.next();
1530+
} catch (e) {
1531+
return a;
1532+
}
1533+
}("FAIL"));
1534+
}
1535+
expect: {
1536+
console.log(function(a) {
1537+
var b = function*() {
1538+
a = null in (a = "PASS");
1539+
}();
1540+
try {
1541+
b.next();
1542+
} catch (e) {
1543+
return a;
1544+
}
1545+
}("FAIL"));
1546+
}
1547+
expect_stdout: "PASS"
1548+
node_version: ">=4"
1549+
}

0 commit comments

Comments
 (0)