Skip to content

Commit

Permalink
fix corner case in dead_code (#5642)
Browse files Browse the repository at this point in the history
fixes #5641
  • Loading branch information
alexlamsl committed Sep 3, 2022
1 parent e012f04 commit 78f354b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -12706,13 +12706,23 @@ Compressor.prototype.compress = function(node) {
return try_evaluate(compressor, self);

function is_tail(node, parent) {
if (parent instanceof AST_Binary) {
return parent.right === node || parent.right.is_constant_expression(scope);
if (parent instanceof AST_Binary) switch (node) {
case parent.left:
return parent.right.is_constant_expression(scope);
case parent.right:
return true;
default:
return false;
}
if (parent instanceof AST_Conditional) {
return parent.condition !== node
|| parent.consequent.is_constant_expression(scope)
&& parent.alternative.is_constant_expression(scope);
if (parent instanceof AST_Conditional) switch (node) {
case parent.condition:
return parent.consequent.is_constant_expression(scope)
&& parent.alternative.is_constant_expression(scope);
case parent.consequent:
case parent.alternative:
return true;
default:
return false;
}
if (parent instanceof AST_Sequence) {
var exprs = parent.expressions;
Expand All @@ -12723,7 +12733,7 @@ Compressor.prototype.compress = function(node) {
}
return true;
}
if (parent instanceof AST_UnaryPrefix) return true;
return parent instanceof AST_UnaryPrefix;
}

function is_tail_block(stat, parent) {
Expand Down
25 changes: 25 additions & 0 deletions test/compress/dead-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -1705,3 +1705,28 @@ issue_5506: {
"bar",
]
}

issue_5641: {
options = {
collapse_vars: true,
conditionals: true,
dead_code: true,
}
input: {
function f(a) {
if (a || b) {
var b = "PASS", c = b && console.log(b);
} else
var d = a || b;
}
f(42);
}
expect: {
function f(a) {
var b, c, d;
(a || b) && (b = "PASS") && console.log(b);
}
f(42);
}
expect_stdout: "PASS"
}

0 comments on commit 78f354b

Please sign in to comment.