Skip to content

Commit

Permalink
fix corner case in if_return (#5650)
Browse files Browse the repository at this point in the history
fixes #5649
  • Loading branch information
alexlamsl authored Sep 8, 2022
1 parent 32bd65a commit 4e4a2f8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3854,28 +3854,28 @@ Compressor.prototype.compress = function(node) {
return j;
}

function eliminate_returns(stat, in_block) {
function eliminate_returns(stat, keep_throws, in_block) {
if (stat instanceof AST_Exit) {
var mode = match_return(stat, true);
var mode = !(keep_throws && stat instanceof AST_Throw) && match_return(stat, true);
if (mode) {
changed = true;
var value = trim_return(stat.value, mode);
if (value) return make_node(AST_SimpleStatement, value, { body: value });
return in_block ? null : make_node(AST_EmptyStatement, stat);
}
} else if (stat instanceof AST_If) {
stat.body = eliminate_returns(stat.body);
if (stat.alternative) stat.alternative = eliminate_returns(stat.alternative);
stat.body = eliminate_returns(stat.body, keep_throws);
if (stat.alternative) stat.alternative = eliminate_returns(stat.alternative, keep_throws);
} else if (stat instanceof AST_LabeledStatement) {
stat.body = eliminate_returns(stat.body);
stat.body = eliminate_returns(stat.body, keep_throws);
} else if (stat instanceof AST_Try) {
if (!stat.bfinally || !jump.value || jump.value.is_constant()) {
if (stat.bcatch) eliminate_returns(stat.bcatch);
var trimmed = eliminate_returns(stat.body.pop(), true);
if (stat.bcatch) eliminate_returns(stat.bcatch, keep_throws);
var trimmed = eliminate_returns(stat.body.pop(), true, true);
if (trimmed) stat.body.push(trimmed);
}
} else if (stat instanceof AST_Block && !(stat instanceof AST_Scope || stat instanceof AST_Switch)) {
var trimmed = eliminate_returns(stat.body.pop(), true);
var trimmed = eliminate_returns(stat.body.pop(), keep_throws, true);
if (trimmed) stat.body.push(trimmed);
}
return stat;
Expand Down
27 changes: 27 additions & 0 deletions test/compress/if_return.js
Original file line number Diff line number Diff line change
Expand Up @@ -2385,3 +2385,30 @@ issue_5619_2: {
}
expect_stdout: "PASS"
}

issue_5649: {
options = {
if_return: true,
}
input: {
console.log(function() {
try {
throw new Error("FAIL");
} catch (e) {
return "PASS";
}
throw new Error("FAIL");
}());
}
expect: {
console.log(function() {
try {
throw new Error("FAIL");
} catch (e) {
return "PASS";
}
throw new Error("FAIL");
}());
}
expect_stdout: "PASS"
}

0 comments on commit 4e4a2f8

Please sign in to comment.