diff --git a/lib/compress.js b/lib/compress.js index 86ea2806..41e5d4cd 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3914,7 +3914,7 @@ Compressor.prototype.compress = function(node) { var node = stat.body; if (!(node instanceof AST_Await)) break; var exp = node.expression; - if (!is_primitive(compressor, exp)) break; + if (!needs_enqueuing(compressor, exp)) break; changed = true; exp = exp.drop_side_effect_free(compressor, true); if (exp) { @@ -8569,7 +8569,7 @@ Compressor.prototype.compress = function(node) { if (arrow && compressor.option("arrows")) { if (!exp.value) { drop_body = true; - } else if (!async || is_primitive(compressor, exp.value)) { + } else if (!async || needs_enqueuing(compressor, exp.value)) { var dropped = exp.value.drop_side_effect_free(compressor); if (dropped !== exp.value) { changed = true; @@ -8588,7 +8588,7 @@ Compressor.prototype.compress = function(node) { exp.process_expression(false, function(node) { var value = node.value; if (value) { - if (async && !is_primitive(compressor, value)) return node; + if (async && !needs_enqueuing(compressor, value)) return node; value = value.drop_side_effect_free(compressor, true); } changed = true; @@ -8598,11 +8598,11 @@ Compressor.prototype.compress = function(node) { scan_local_returns(exp, function(node) { var value = node.value; if (value) { - if (async && !is_primitive(compressor, value)) return; + if (async && !needs_enqueuing(compressor, value)) return; var dropped = value.drop_side_effect_free(compressor); if (dropped !== value) { changed = true; - if (dropped && async && !is_primitive(compressor, dropped)) { + if (dropped && async && !needs_enqueuing(compressor, dropped)) { dropped = dropped.negate(compressor); } node.value = dropped; @@ -8614,7 +8614,7 @@ Compressor.prototype.compress = function(node) { if (drop_body) exp.process_expression("awaits", function(node) { var body = node.body; if (body instanceof AST_Await) { - if (is_primitive(compressor, body.expression)) { + if (needs_enqueuing(compressor, body.expression)) { changed = true; body = body.expression.drop_side_effect_free(compressor, true); if (!body) return make_node(AST_EmptyStatement, node); @@ -8626,7 +8626,7 @@ Compressor.prototype.compress = function(node) { var tail = exprs[i]; if (!(tail instanceof AST_Await)) break; var value = tail.expression; - if (!is_primitive(compressor, value)) break; + if (!needs_enqueuing(compressor, value)) break; changed = true; if (exprs[i] = value.drop_side_effect_free(compressor)) break; } @@ -8643,14 +8643,14 @@ Compressor.prototype.compress = function(node) { } return node; }); - var abort = !drop_body && exp.name || arrow && exp.value && !is_primitive(compressor, exp.value); + var abort = !drop_body && exp.name || arrow && exp.value && !needs_enqueuing(compressor, exp.value); var tw = new TreeWalker(function(node) { if (abort) return true; if (tw.parent() === exp && node.may_throw(compressor)) return abort = true; if (node instanceof AST_Await) return abort = true; if (node instanceof AST_ForAwaitOf) return abort = true; if (node instanceof AST_Return) { - if (node.value && !is_primitive(compressor, node.value)) return abort = true; + if (node.value && !needs_enqueuing(compressor, node.value)) return abort = true; return; } if (node instanceof AST_Scope && node !== exp) return true; @@ -8735,13 +8735,13 @@ Compressor.prototype.compress = function(node) { def(AST_Await, function(compressor) { if (!compressor.option("awaits")) return this; var exp = this.expression; - if (!is_primitive(compressor, exp)) return this; + if (!needs_enqueuing(compressor, exp)) return this; if (exp instanceof AST_UnaryPrefix && exp.operator == "!") exp = exp.expression; var dropped = exp.drop_side_effect_free(compressor); if (dropped === exp) return this; if (!dropped) { dropped = make_node(AST_Number, exp, { value: 0 }); - } else if (!is_primitive(compressor, dropped)) { + } else if (!needs_enqueuing(compressor, dropped)) { dropped = dropped.negate(compressor); } var node = this.clone(); @@ -8998,7 +8998,7 @@ Compressor.prototype.compress = function(node) { expressions = expressions.slice(0, -1); end--; var expr = expressions[end]; - last.expression = is_primitive(compressor, expr) ? expr : expr.negate(compressor); + last.expression = needs_enqueuing(compressor, expr) ? expr : expr.negate(compressor); expressions[end] = last; } var assign, cond, lhs; @@ -11489,20 +11489,21 @@ Compressor.prototype.compress = function(node) { } } - function is_primitive(compressor, node) { + function needs_enqueuing(compressor, node) { if (node.is_constant()) return true; - if (node instanceof AST_Assign) return node.operator != "=" || is_primitive(compressor, node.right); + if (node instanceof AST_Assign) return node.operator != "=" || needs_enqueuing(compressor, node.right); if (node instanceof AST_Binary) { return !lazy_op[node.operator] - || is_primitive(compressor, node.left) && is_primitive(compressor, node.right); + || needs_enqueuing(compressor, node.left) && needs_enqueuing(compressor, node.right); } + if (node instanceof AST_Call) return is_async(node.expression); if (node instanceof AST_Conditional) { - return is_primitive(compressor, node.consequent) && is_primitive(compressor, node.alternative); + return needs_enqueuing(compressor, node.consequent) && needs_enqueuing(compressor, node.alternative); } - if (node instanceof AST_Sequence) return is_primitive(compressor, node.tail_node()); + if (node instanceof AST_Sequence) return needs_enqueuing(compressor, node.tail_node()); if (node instanceof AST_SymbolRef) { var fixed = node.fixed_value(); - return fixed && is_primitive(compressor, fixed); + return fixed && needs_enqueuing(compressor, fixed); } if (node instanceof AST_Template) return !node.tag || is_raw_tag(compressor, node.tag); if (node instanceof AST_Unary) return true; @@ -13630,6 +13631,7 @@ Compressor.prototype.compress = function(node) { def(AST_Node, noop); def(AST_Assign, noop); def(AST_Await, function(compressor, scope, no_return, in_loop) { + if (!compressor.option("awaits")) return; var self = this; var inlined = self.expression.try_inline(compressor, scope, no_return, in_loop, true); if (!inlined) return; diff --git a/test/compress/awaits.js b/test/compress/awaits.js index 3dc3472e..4f451631 100644 --- a/test/compress/awaits.js +++ b/test/compress/awaits.js @@ -529,6 +529,7 @@ inline_block_await: { inline_block_await_async: { options = { + awaits: true, inline: true, } input: { @@ -2747,6 +2748,7 @@ issue_5177: { issue_5250: { options = { + awaits: true, inline: true, } input: { @@ -2855,6 +2857,7 @@ issue_5298: { issue_5305_1: { options = { + awaits: true, inline: true, } input: { @@ -2888,6 +2891,7 @@ issue_5305_1: { issue_5305_2: { options = { + awaits: true, inline: true, } input: { @@ -3231,6 +3235,57 @@ issue_5528_4: { } issue_5634_1: { + options = { + awaits: true, + inline: true, + } + input: { + var a = "foo"; + (async function() { + (async function() { + try { + return { + then(resolve) { + console.log("bar"); + resolve(); + console.log("baz"); + }, + }; + } finally { + a = "moo"; + } + })(); + })(); + console.log(a); + } + expect: { + var a = "foo"; + (async function() { + (async function() { + try { + return { + then(resolve) { + console.log("bar"); + resolve(); + console.log("baz"); + }, + }; + } finally { + a = "moo"; + } + })(); + })(); + console.log(a); + } + expect_stdout: [ + "moo", + "bar", + "baz", + ] + node_version: ">=8" +} + +issue_5634_1_side_effects: { options = { awaits: true, inline: true, @@ -3282,6 +3337,7 @@ issue_5634_1: { issue_5634_2: { options = { + awaits: true, inline: true, } input: { @@ -3330,6 +3386,56 @@ issue_5634_2: { node_version: ">=8" } +issue_5634_2_side_effects: { + options = { + awaits: true, + inline: true, + side_effects: true, + } + input: { + var a = "foo"; + (async function() { + await async function() { + try { + return { + then(resolve) { + console.log("bar"); + resolve(); + console.log("baz"); + }, + }; + } finally { + a = "moo"; + } + }(); + })(); + console.log(a); + } + expect: { + var a = "foo"; + (async function() { + try { + return { + then(resolve) { + console.log("bar"); + resolve(); + console.log("baz"); + }, + }; + } finally { + a = "moo"; + } + })(); + console.log(a); + } + expect_stdout: [ + "moo", + "bar", + "baz", + ] + node_version: ">=8" +} + issue_5634_3: { options = { awaits: true, @@ -3380,3 +3486,53 @@ issue_5634_3: { ] node_version: ">=8" } + +issue_5634_3_side_effects: { + options = { + awaits: true, + inline: true, + side_effects: true, + } + input: { + var a = "foo"; + (async function() { + return async function() { + try { + return { + then(resolve) { + console.log("bar"); + resolve(); + console.log("baz"); + }, + }; + } finally { + a = "moo"; + } + }(); + })(); + console.log(a); + } + expect: { + var a = "foo"; + (async function() { + try { + return { + then(resolve) { + console.log("bar"); + resolve(); + console.log("baz"); + }, + }; + } finally { + a = "moo"; + } + })(); + console.log(a); + } + expect_stdout: [ + "moo", + "bar", + "baz", + ] + node_version: ">=8" +}