diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-01-02 04:39:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-02 12:39:51 +0800 |
commit | b3a706114ce775f75c4a6da37bff4a8ef32415d5 (patch) | |
tree | 2f4546ba7fbb3788eb06f1d5f6bfac6d2dace7ce | |
parent | cc2d7acaf0ed2479c8d3a5d3cbbce15a3cb2333b (diff) | |
download | tracifyjs-b3a706114ce775f75c4a6da37bff4a8ef32415d5.tar.gz tracifyjs-b3a706114ce775f75c4a6da37bff4a8ef32415d5.zip |
enhance `if_return` & `side_effects` (#4494)
-rw-r--r-- | lib/compress.js | 19 | ||||
-rw-r--r-- | test/compress/async.js | 22 |
2 files changed, 30 insertions, 11 deletions
diff --git a/lib/compress.js b/lib/compress.js index 0a46fb26..0ab65868 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2600,10 +2600,18 @@ merge(Compressor.prototype, { statements.splice(i, 1); continue; } - if (stat.value instanceof AST_UnaryPrefix && stat.value.operator == "void") { + var tail = stat.value.tail_node(); + if (tail instanceof AST_UnaryPrefix && tail.operator == "void") { CHANGED = true; + var body; + if (tail === stat.value) { + body = tail.expression; + } else { + body = stat.value.clone(); + body.expressions[body.length - 1] = tail.expression; + } statements[i] = make_node(AST_SimpleStatement, stat, { - body: stat.value.expression + body: body, }); continue; } @@ -6642,6 +6650,13 @@ merge(Compressor.prototype, { return this; }); def(AST_AsyncFunction, return_null); + def(AST_Await, function(compressor) { + var exp = this.expression.drop_side_effect_free(compressor); + if (exp === this.expression) return this; + var node = this.clone(); + node.expression = exp || make_node(AST_Number, this, { value: 0 }); + return node; + }); def(AST_Binary, function(compressor, first_in_statement) { if (this.operator == "in" && !is_object(this.right)) { var left = this.left.drop_side_effect_free(compressor, first_in_statement); diff --git a/test/compress/async.js b/test/compress/async.js index 51aba26c..4039dbb1 100644 --- a/test/compress/async.js +++ b/test/compress/async.js @@ -184,7 +184,7 @@ inline_await_1_trim: { } expect: { (async function() { - await 42; + await 0; })(); console.log("PASS"); } @@ -228,7 +228,7 @@ inline_await_2_trim: { input: { (async function() { async function f(a) { - await a; + await a.log; } return await f(console); })(); @@ -236,7 +236,7 @@ inline_await_2_trim: { } expect: { (async function() { - await console; + await console.log; })(); console.log("PASS"); } @@ -299,18 +299,22 @@ await_unary: { side_effects: true, } input: { + var a; (async function() { - console.log("PASS"); - await +[]; - console.log("FAIL"); + a = "PASS"; + await delete a.p; + a = "FAIL"; })(); + console.log(a); } expect: { + var a; (async function() { - console.log("PASS"); - await +[]; - console.log("FAIL"); + a = "PASS"; + await delete a.p; + a = "FAIL"; })(); + console.log(a); } expect_stdout: "PASS" node_version: ">=8" |