From a570c0025141e46e795a9adf9fb318b8cf21031e Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 27 Sep 2022 17:04:32 +0100 Subject: [PATCH] fix corner case in `conditionals` & `if_return` (#5685) fixes #5684 --- lib/compress.js | 24 ++++++++++++++++++++---- test/compress/yields.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 1fa0cf86299..78304c26e2c 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3529,6 +3529,7 @@ Compressor.prototype.compress = function(node) { var declare_only, jump, merge_jump; var in_iife = in_lambda && parent && parent.TYPE == "Call" && parent.expression === self; var chain_if_returns = in_lambda && compressor.option("conditionals") && compressor.option("sequences"); + var drop_return_void = !(in_try && in_try.bfinally && in_async_generator(in_lambda)); var multiple_if_returns = has_multiple_if_returns(statements); for (var i = statements.length; --i >= 0;) { var stat = statements[i]; @@ -3536,8 +3537,7 @@ Compressor.prototype.compress = function(node) { var next = statements[j]; if (in_lambda && declare_only && !next && stat instanceof AST_Return - && !(self instanceof AST_SwitchBranch) - && !(in_try && in_try.bfinally && in_async_generator(in_lambda))) { + && drop_return_void && !(self instanceof AST_SwitchBranch)) { var body = stat.value; if (!body) { changed = true; @@ -3633,7 +3633,7 @@ Compressor.prototype.compress = function(node) { var in_bool = stat.body.in_bool || next instanceof AST_Return && next.in_bool; // if (foo()) return x; return y; ---> return foo() ? x : y; if (!stat.alternative && next instanceof AST_Return - && (!value == !next.value || !in_async_generator(in_lambda))) { + && (drop_return_void || !value == !next.value)) { changed = true; stat = stat.clone(); stat.alternative = make_node(AST_BlockStatement, next, { @@ -9686,7 +9686,7 @@ Compressor.prototype.compress = function(node) { self.body, ], }).optimize(compressor); - if (cons_value && alt_value || !in_async_generator(compressor.find_parent(AST_Scope))) { + if (cons_value && alt_value || !keep_return_void()) { var exit = make_node(self.body.CTOR, self, { value: make_node(AST_Conditional, self, { condition: self.condition, @@ -9762,6 +9762,22 @@ Compressor.prototype.compress = function(node) { return node instanceof AST_BlockStatement ? node.body : [ node ]; } + function keep_return_void() { + var has_finally = false, level = 0, node = compressor.self(); + do { + if (node instanceof AST_Catch) { + if (compressor.parent(level).bfinally) has_finally = true; + level++; + } else if (node instanceof AST_Finally) { + level++; + } else if (node instanceof AST_Scope) { + return has_finally && in_async_generator(node); + } else if (node instanceof AST_Try) { + if (node.bfinally) has_finally = true; + } + } while (node = compressor.parent(level++)); + } + function last_index(stats) { for (var index = stats.length; --index >= 0;) { if (!is_declaration(stats[index], true)) break; diff --git a/test/compress/yields.js b/test/compress/yields.js index f2bb9b295f0..8478d541c9a 100644 --- a/test/compress/yields.js +++ b/test/compress/yields.js @@ -1989,3 +1989,32 @@ issue_5679_6: { expect_stdout: "PASS" node_version: ">=10" } + +issue_5684: { + options = { + conditionals: true, + if_return: true, + } + input: { + (async function*() { + switch (42) { + default: + if (console.log("PASS")) + return; + return null; + case false: + } + })().next(); + } + expect: { + (async function*() { + switch (42) { + default: + return console.log("PASS") ? void 0 : null; + case false: + } + })().next(); + } + expect_stdout: "PASS" + node_version: ">=10" +}