From 887e086890a67c8529143d4efd8621f7a72c2d6a Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 17 Aug 2022 02:10:33 +0100 Subject: [PATCH] fix corner case in `if_return` (#5620) fixes #5619 --- lib/compress.js | 3 ++- test/compress/if_return.js | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index f408195e..d86b2df5 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3620,7 +3620,7 @@ Compressor.prototype.compress = function(node) { continue; } - if (jump && jump === next) eliminate_returns(stat); + if (declare_only && jump && jump === next) eliminate_returns(stat); } return changed; @@ -3787,6 +3787,7 @@ Compressor.prototype.compress = function(node) { if (stat instanceof AST_Exit) { var mode = 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); diff --git a/test/compress/if_return.js b/test/compress/if_return.js index abe26073..7ad20043 100644 --- a/test/compress/if_return.js +++ b/test/compress/if_return.js @@ -2305,3 +2305,56 @@ issue_5597: { } expect_stdout: "PASS" } + +issue_5619_1: { + options = { + if_return: true, + } + input: { + console.log(function() { + if (console) + if (console) + return "PASS"; + var a = FAIL; + return "PASS"; + }()); + } + expect: { + console.log(function() { + if (console) + if (console) + return "PASS"; + var a = FAIL; + return "PASS"; + }()); + } + expect_stdout: "PASS" +} + +issue_5619_2: { + options = { + dead_code: true, + if_return: true, + loops: true, + } + input: { + console.log(function() { + if (console) + while (console) + return "PASS"; + var a = FAIL; + return "PASS"; + }()); + } + expect: { + console.log(function() { + if (console) { + if (console) + return "PASS"; + } + var a = FAIL; + return "PASS"; + }()); + } + expect_stdout: "PASS" +}