Skip to content

Commit

Permalink
fix corner case in dead_code (#5507)
Browse files Browse the repository at this point in the history
fixes #5506
  • Loading branch information
alexlamsl authored Jun 11, 2022
1 parent 053cb27 commit 99946a3
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -12329,7 +12329,7 @@ Compressor.prototype.compress = function(node) {
parent = compressor.parent(level++);
if (parent instanceof AST_Assign) {
if (parent.left instanceof AST_SymbolRef && parent.left.definition() === def) {
if (in_try(level, parent)) break;
if (in_try(level, parent, !local)) break;
return strip_assignment(def);
}
if (parent.left.match_symbol(function(node) {
Expand Down Expand Up @@ -12441,14 +12441,16 @@ Compressor.prototype.compress = function(node) {
if (parent instanceof AST_Try) return parent.bfinally ? parent.bfinally === stat : parent.bcatch === stat;
}

function in_try(level, node) {
function in_try(level, node, sync) {
var right = self.right;
self.right = make_node(AST_Null, right);
var may_throw = node.may_throw(compressor);
self.right = right;
for (var parent; parent = compressor.parent(level++); node = parent) {
if (parent === scope) return false;
if (parent instanceof AST_Try) {
if (sync && parent instanceof AST_Lambda) {
if (parent.name || is_async(parent) || is_generator(parent)) return true;
} else if (parent instanceof AST_Try) {
if (parent.bfinally && parent.bfinally !== node) return true;
if (may_throw && parent.bcatch && parent.bcatch !== node) return true;
}
Expand Down
24 changes: 24 additions & 0 deletions test/compress/awaits.js
Original file line number Diff line number Diff line change
Expand Up @@ -2982,3 +2982,27 @@ issue_5493: {
expect_stdout: "undefined"
node_version: ">=8"
}

issue_5506: {
options = {
dead_code: true,
}
input: {
console.log(function(a) {
(async function() {
a = null in (a = "PASS");
})();
return a;
}("FAIL"));
}
expect: {
console.log(function(a) {
(async function() {
a = null in (a = "PASS");
})();
return a;
}("FAIL"));
}
expect_stdout: "PASS"
node_version: ">=8"
}
38 changes: 38 additions & 0 deletions test/compress/dead-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -1669,3 +1669,41 @@ issue_5106_2: {
}
expect_stdout: "PASS"
}

issue_5506: {
options = {
dead_code: true,
}
input: {
try {
(function(a) {
var b = 1;
(function f() {
try {
b-- && f();
} catch (c) {}
console.log(a);
a = 42 in (a = "bar");
})();
})("foo");
} catch (e) {}
}
expect: {
try {
(function(a) {
var b = 1;
(function f() {
try {
b-- && f();
} catch (c) {}
console.log(a);
a = 42 in (a = "bar");
})();
})("foo");
} catch (e) {}
}
expect_stdout: [
"foo",
"bar",
]
}
32 changes: 32 additions & 0 deletions test/compress/yields.js
Original file line number Diff line number Diff line change
Expand Up @@ -1515,3 +1515,35 @@ issue_5456: {
expect_stdout: "foo"
node_version: ">=4"
}

issue_5506: {
options = {
dead_code: true,
}
input: {
console.log(function(a) {
var b = function*() {
a = null in (a = "PASS");
}();
try {
b.next();
} catch (e) {
return a;
}
}("FAIL"));
}
expect: {
console.log(function(a) {
var b = function*() {
a = null in (a = "PASS");
}();
try {
b.next();
} catch (e) {
return a;
}
}("FAIL"));
}
expect_stdout: "PASS"
node_version: ">=4"
}

0 comments on commit 99946a3

Please sign in to comment.