diff --git a/lib/compress.js b/lib/compress.js index 1807bbfe..404c0621 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1869,22 +1869,26 @@ Compressor.prototype.compress = function(node) { if (handle_if_return(statements, compressor)) changed = 3; if (!changed && last_changed == 3) break; } - if (compressor.option("inline") >= 4) { - if (inline_iife(statements, compressor)) changed = 4; + if (compressor.option("awaits") && compressor.option("side_effects")) { + if (trim_awaits(statements, compressor)) changed = 4; if (!changed && last_changed == 4) break; } - if (compressor.sequences_limit > 0) { - if (sequencesize(statements, compressor)) changed = 5; + if (compressor.option("inline") >= 4) { + if (inline_iife(statements, compressor)) changed = 5; if (!changed && last_changed == 5) break; - if (sequencesize_2(statements, compressor)) changed = 6; - if (!changed && last_changed == 6) break; } - if (compressor.option("join_vars")) { - if (join_consecutive_vars(statements)) changed = 7; + if (compressor.sequences_limit > 0) { + if (sequencesize(statements, compressor)) changed = 6; + if (!changed && last_changed == 6) break; + if (sequencesize_2(statements, compressor)) changed = 7; if (!changed && last_changed == 7) break; } + if (compressor.option("join_vars")) { + if (join_consecutive_vars(statements)) changed = 8; + if (!changed && last_changed == 8) break; + } if (compressor.option("collapse_vars")) { - if (collapse(statements, compressor)) changed = 8; + if (collapse(statements, compressor)) changed = 9; } } while (changed && max_iter-- > 0); return statements; @@ -3524,6 +3528,27 @@ Compressor.prototype.compress = function(node) { return statements.length != len; } + function trim_awaits(statements, compressor) { + if (!in_lambda || in_try && in_try.bfinally) return; + var changed = false; + for (var index = statements.length; --index >= 0;) { + var stat = statements[index]; + if (!(stat instanceof AST_SimpleStatement)) break; + var node = stat.body; + if (!(node instanceof AST_Await)) break; + var exp = node.expression; + if (!is_primitive(compressor, exp)) break; + changed = true; + exp = exp.drop_side_effect_free(compressor, true); + if (exp) { + stat.body = exp; + break; + } + } + statements.length = index + 1; + return changed; + } + function inline_iife(statements, compressor) { var changed = false; var index = statements.length - 1; @@ -12987,6 +13012,14 @@ Compressor.prototype.compress = function(node) { var self = this; var inlined = sync(self.expression).try_inline(compressor, scope, no_return, in_loop); if (!inlined) return; + if (!no_return) scan_local_returns(inlined, function(node) { + node.in_bool = false; + var value = node.value; + if (value instanceof AST_Await) return; + node.value = make_node(AST_Await, self, { + expression: value || make_node(AST_Undefined, node).transform(compressor), + }); + }); return aborts(inlined) ? inlined : make_node(AST_BlockStatement, self, { body: [ inlined, make_node(AST_SimpleStatement, self, { body: make_node(AST_Await, self, { expression: make_node(AST_Number, self, { value: 0 })}), diff --git a/test/compress/awaits.js b/test/compress/awaits.js index 91c5c3ef..5f65e3e1 100644 --- a/test/compress/awaits.js +++ b/test/compress/awaits.js @@ -496,6 +496,7 @@ inline_block_await: { awaits: true, if_return: true, inline: true, + side_effects: true, } input: { console.log("foo"); @@ -567,6 +568,7 @@ inline_block_await_async_return: { awaits: true, if_return: true, inline: true, + side_effects: true, } input: { console.log("foo"); @@ -2855,3 +2857,105 @@ issue_5298: { expect_stdout: "PASS" node_version: ">=8" } + +issue_5305_1: { + options = { + inline: true, + } + input: { + var a = "PASS"; + (async function() { + try { + return await function() { + while (!console); + }(); + } finally { + a = "FAIL"; + } + })(); + console.log(a); + } + expect: { + var a = "PASS"; + (async function() { + try { + while (!console); + return await void 0; + } finally { + a = "FAIL"; + } + })(); + console.log(a); + } + expect_stdout: "PASS" + node_version: ">=8" +} + +issue_5305_2: { + options = { + inline: true, + } + input: { + var a = "PASS"; + (async function() { + try { + throw null; + } catch (e) { + return await function() { + while (!console); + }(); + } finally { + a = "FAIL"; + } + })(); + console.log(a); + } + expect: { + var a = "PASS"; + (async function() { + try { + throw null; + } catch (e) { + while (!console); + return await void 0; + } finally { + a = "FAIL"; + } + })(); + console.log(a); + } + expect_stdout: "PASS" + node_version: ">=8" +} + +issue_5305_3: { + options = { + awaits: true, + inline: true, + side_effects: true, + } + input: { + var a = "PASS"; + (async function() { + try { + await function() { + while (!console); + }(); + } catch (e) { + a = "FAIL"; + } + })(); + console.log(a); + } + expect: { + var a = "PASS"; + try { + while (!console); + } catch (e) { + a = "FAIL"; + } + console.log(a); + } + expect_stdout: "PASS" + node_version: ">=8" +}