From aa6eb0d5be20cd686a212bf9bd6255f36b0c8fb9 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 2 Jan 2022 06:59:32 +0000 Subject: [PATCH] fix corner cases in `inline` (#5252) fixes #5249 fixes #5250 --- lib/compress.js | 33 +++++++++++++++++-- test/compress/awaits.js | 65 +++++++++++++++++++++++++++++++++++++ test/compress/functions.js | 66 +++++++++++++++++++++++++++++++++++++- 3 files changed, 161 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 8e932730..1df3bb76 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -12831,7 +12831,36 @@ Compressor.prototype.compress = function(node) { def(AST_Node, noop); def(AST_Assign, noop); def(AST_Await, function(compressor, scope, no_return, in_loop) { - return this.expression.try_inline(compressor, scope, no_return, in_loop); + var self = this; + var inlined = sync(self.expression).try_inline(compressor, scope, no_return, in_loop); + if (!inlined) return; + 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 })}), + }) ], + }); + + function sync(node) { + if (!no_return) return node; + if (node.TYPE != "Call") return node; + var fn = node.expression; + switch (fn.CTOR) { + case AST_AsyncArrow: + fn = make_node(AST_Arrow, fn, fn); + break; + case AST_AsyncFunction: + fn = make_node(AST_Function, fn, fn); + break; + case AST_AsyncGeneratorFunction: + fn = make_node(AST_GeneratorFunction, fn, fn); + break; + default: + return node; + } + node = node.clone(); + node.expression = fn; + return node; + } }); def(AST_Binary, function(compressor, scope, no_return, in_loop) { if (no_return === undefined) return; @@ -12843,7 +12872,7 @@ Compressor.prototype.compress = function(node) { return make_node(AST_If, self, { condition: make_condition(self.left), body: inlined, - alternative: null, + alternative: no_return ? null : make_node(AST_Return, self, { value: null }), }); function make_condition(cond) { diff --git a/test/compress/awaits.js b/test/compress/awaits.js index 170bfd15..9dd9be2b 100644 --- a/test/compress/awaits.js +++ b/test/compress/awaits.js @@ -512,6 +512,42 @@ inline_block_await: { } inline_block_await_async: { + options = { + inline: true, + } + input: { + (async function() { + console.log("foo"); + await (async function() { + while (await console.log("bar")); + console.log("baz"); + })(); + console.log("moo"); + })().then(console.log); + console.log("moz"); + } + expect: { + (async function() { + console.log("foo"); + while (await console.log("bar")); + console.log("baz"); + await 0; + console.log("moo"); + })().then(console.log); + console.log("moz"); + } + expect_stdout: [ + "foo", + "bar", + "moz", + "baz", + "moo", + "undefined", + ] + node_version: ">=8" +} + +inline_block_await_async_return: { options = { awaits: true, if_return: true, @@ -2540,3 +2576,32 @@ issue_5177: { expect_stdout: "function" node_version: ">=8" } + +issue_5250: { + options = { + inline: true, + } + input: { + (async function() { + await function() { + while (console.log("foo")); + }(); + console.log("bar"); + })(); + console.log("baz"); + } + expect: { + (async function() { + while (console.log("foo")); + await 0; + console.log("bar"); + })(); + console.log("baz"); + } + expect_stdout: [ + "foo", + "baz", + "bar", + ] + node_version: ">=8" +} diff --git a/test/compress/functions.js b/test/compress/functions.js index 3cf48571..bb951d2b 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -628,7 +628,8 @@ inline_binary_and: { while (console.log("baz")); return void "moo"; return; - } + } else + return; }()); } expect_stdout: [ @@ -7686,3 +7687,66 @@ issue_5240_2: { } expect_stdout: "undefined" } + +issue_5249_1: { + options = { + inline: true, + } + input: { + console.log(function() { + if (!console) + var a = "FAIL 1"; + else + return void (a && function() { + while (console.log("FAIL 2")); + }()); + throw "FAIL 3"; + }()); + } + expect: { + console.log(function() { + if (!console) + var a = "FAIL 1"; + else if (a) { + while (console.log("FAIL 2")); + return; + } else + return; + throw "FAIL 3"; + }()); + } + expect_stdout: "undefined" +} + +issue_5249_2: { + options = { + conditionals: true, + dead_code: true, + evaluate: true, + if_return: true, + inline: true, + passes: 3, + reduce_vars: true, + sequences: true, + side_effects: true, + unused: true, + } + input: { + console.log(function() { + if (!console) + var a = "FAIL 1"; + else + return void (a && function() { + while (console.log("FAIL 2")); + }()); + throw "FAIL 3"; + }()); + } + expect: { + console.log(function() { + if (!console) + throw "FAIL 3"; + }()); + } + expect_stdout: "undefined" +}