From: Alex Lam S.L Date: Fri, 5 Mar 2021 20:25:44 +0000 (+0000) Subject: fix corner case in `awaits` (#4740) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=83c3838b07ed9ff32a83a39d5c1cbf40155c46f2;p=UglifyJS.git fix corner case in `awaits` (#4740) fixes #4738 --- diff --git a/lib/compress.js b/lib/compress.js index 9d390d09..20558ba8 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -7205,10 +7205,10 @@ merge(Compressor.prototype, { }); def(AST_Await, function(compressor) { if (!compressor.option("awaits")) return this; - var exp = this.expression.drop_side_effect_free(compressor); - if (exp === this.expression) return this; + var exp = this.expression; + if (!is_primitive(compressor, exp)) return this; var node = this.clone(); - node.expression = exp || make_node(AST_Number, this, { value: 0 }); + node.expression = exp.drop_side_effect_free(compressor) || make_node(AST_Number, this, { value: 0 }); return node; }); def(AST_Binary, function(compressor, first_in_statement) { @@ -9555,6 +9555,25 @@ merge(Compressor.prototype, { || node instanceof AST_Object; } + function is_primitive(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_Binary) { + return !lazy_op[node.operator] + || is_primitive(compressor, node.left) && is_primitive(compressor, node.right); + } + if (node instanceof AST_Conditional) { + return is_primitive(compressor, node.consequent) && is_primitive(compressor, node.alternative); + } + if (node instanceof AST_Sequence) return is_primitive(compressor, node.tail_node()); + if (node instanceof AST_SymbolRef) { + var fixed = node.fixed_value(); + return fixed && is_primitive(compressor, fixed); + } + if (node instanceof AST_Template) return !node.tag || is_raw_tag(compressor, node.tag); + if (node instanceof AST_Unary) return true; + } + function repeatable(compressor, node) { if (node instanceof AST_Dot) return repeatable(compressor, node.expression); if (node instanceof AST_Sub) { diff --git a/test/compress/awaits.js b/test/compress/awaits.js index 974134c6..0534d186 100644 --- a/test/compress/awaits.js +++ b/test/compress/awaits.js @@ -23,16 +23,34 @@ async_label: { } await_await: { + options = { + awaits: true, + side_effects: true, + } input: { (async function() { - console.log("PASS"); - await await 42; + await await { + then(resolve) { + resolve({ + then() { + console.log("PASS"); + }, + }); + }, + }; })(); } expect: { (async function() { - console.log("PASS"); - await await 42; + await { + then(resolve) { + resolve({ + then() { + console.log("PASS"); + }, + }); + }, + }; })(); } expect_stdout: "PASS" @@ -1280,3 +1298,84 @@ issue_4717: { expect_stdout: "PASS" node_version: ">=8" } + +issue_4738_1: { + options = { + awaits: true, + side_effects: true, + } + input: { + (async function() { + await { + then() { + console.log("PASS"); + }, + }; + })(); + } + expect: { + (async function() { + await { + then() { + console.log("PASS"); + }, + }; + })(); + } + expect_stdout: "PASS" + node_version: ">=8" +} + +issue_4738_2: { + options = { + awaits: true, + side_effects: true, + } + input: { + (async function() { + await { + get then() { + console.log("PASS"); + }, + }; + })(); + } + expect: { + (async function() { + await { + get then() { + console.log("PASS"); + }, + }; + })(); + } + expect_stdout: "PASS" + node_version: ">=8" +} + +issue_4738_3: { + options = { + awaits: true, + side_effects: true, + } + input: { + (async function() { + await { + then: function() { + console.log("PASS"); + }, + }; + })(); + } + expect: { + (async function() { + await { + then: function() { + console.log("PASS"); + }, + }; + })(); + } + expect_stdout: "PASS" + node_version: ">=8" +}