From: Alex Lam S.L Date: Wed, 10 Feb 2021 15:41:00 +0000 (+0000) Subject: fix corner cases with asynchronous generators (#4642) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=083679bcad5711a90e49272e8a695a0c7b189a47;p=UglifyJS.git fix corner cases with asynchronous generators (#4642) fixes #4641 --- diff --git a/lib/compress.js b/lib/compress.js index 8b5b020d..3d857190 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1307,6 +1307,10 @@ merge(Compressor.prototype, { return found; }); + function in_async_generator(scope) { + return scope instanceof AST_AsyncGeneratorDefun || scope instanceof AST_AsyncGeneratorFunction; + } + function find_scope(compressor) { var level = 0, node; while (node = compressor.parent(level++)) { @@ -2987,7 +2991,9 @@ merge(Compressor.prototype, { var stat = statements[i]; if (prev) { if (stat instanceof AST_Exit) { - stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat)).transform(compressor); + if (stat.value || !in_async_generator(scope)) { + stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat)).transform(compressor); + } } else if (stat instanceof AST_For) { if (!(stat.init instanceof AST_Definitions)) { var abort = false; @@ -11045,7 +11051,10 @@ merge(Compressor.prototype, { }); OPT(AST_Return, function(self, compressor) { - if (self.value && is_undefined(self.value, compressor)) { + if (compressor.option("side_effects") + && self.value + && is_undefined(self.value, compressor) + && !in_async_generator(compressor.find_parent(AST_Scope))) { self.value = null; } return self; diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 04df8e11..fe963b4a 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -5879,6 +5879,7 @@ collapse_rhs_this: { collapse_rhs_undefined: { options = { collapse_vars: true, + side_effects: true, } input: { var a, b; diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 185a39cf..5ae0884b 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -2460,6 +2460,7 @@ delay_def: { evaluate: true, reduce_funcs: true, reduce_vars: true, + side_effects: true, unused: true, } input: { diff --git a/test/compress/yields.js b/test/compress/yields.js index 78a5389a..eac47e22 100644 --- a/test/compress/yields.js +++ b/test/compress/yields.js @@ -889,3 +889,64 @@ issue_4639_2: { expect_stdout: "undefined" node_version: ">=4" } + +issue_4641_1: { + options = { + sequences: true, + } + input: { + console.log(typeof async function*() { + try { + console.log("foo"); + return; + } finally { + console.log("bar"); + } + }().next().then); + } + expect: { + console.log(typeof async function*() { + try { + console.log("foo"); + return; + } finally { + console.log("bar"); + } + }().next().then); + } + expect_stdout: [ + "foo", + "bar", + "function", + ] + node_version: ">=10" +} + +issue_4641_2: { + options = { + side_effects: true, + } + input: { + console.log(typeof async function*() { + try { + return void "FAIL"; + } finally { + console.log("PASS"); + } + }().next().then); + } + expect: { + console.log(typeof async function*() { + try { + return void 0; + } finally { + console.log("PASS"); + } + }().next().then); + } + expect_stdout: [ + "function", + "PASS", + ] + node_version: ">=10" +}