Skip to content

Commit

Permalink
enhance awaits (#4971)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed May 28, 2021
1 parent 749a828 commit 8cd95dd
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 35 deletions.
83 changes: 48 additions & 35 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -7519,11 +7519,13 @@ merge(Compressor.prototype, {
return make_sequence(this, [ lhs, rhs ]);
});
function drop_returns(compressor, exp) {
var arrow = is_arrow(exp);
var async = is_async(exp);
var drop_body = false;
if (compressor.option("arrows") && is_arrow(exp)) {
if (arrow && compressor.option("arrows")) {
if (!exp.value) {
drop_body = true;
} else if (!is_async(exp) || is_primitive(compressor, exp.value)) {
} else if (!async || is_primitive(compressor, exp.value)) {
exp.value = exp.value.drop_side_effect_free(compressor);
}
} else if (exp instanceof AST_AsyncFunction || exp instanceof AST_Function) {
Expand All @@ -7535,7 +7537,6 @@ merge(Compressor.prototype, {
}
}
if (drop_body) {
var async = is_async(exp);
exp.process_expression(false, function(node) {
var value = node.value;
if (value) {
Expand All @@ -7552,43 +7553,55 @@ merge(Compressor.prototype, {
node.value = value.drop_side_effect_free(compressor);
}
});
if (async && compressor.option("awaits")) {
exp.process_expression(true, function(node) {
var body = node.body;
if (body instanceof AST_Await) {
if (is_primitive(compressor, body.expression)) {
body = body.expression.drop_side_effect_free(compressor, true);
if (!body) return make_node(AST_EmptyStatement, node);
node.body = body;
}
} else if (body instanceof AST_Sequence) {
var tail = body.tail_node();
if (tail instanceof AST_Await && is_primitive(compressor, tail.expression)) {
tail = tail.expression.drop_side_effect_free(compressor);
body.expressions.pop();
if (tail) body.expressions.push(tail);
}
}
if (async && compressor.option("awaits")) {
if (drop_body) exp.process_expression(true, function(node) {
var body = node.body;
if (body instanceof AST_Await) {
if (is_primitive(compressor, body.expression)) {
body = body.expression.drop_side_effect_free(compressor, true);
if (!body) return make_node(AST_EmptyStatement, node);
node.body = body;
}
return node;
});
if (all(exp.body, is_empty) && !(is_arrow(exp) && exp.value)) {
var ctor;
switch (exp.CTOR) {
case AST_AsyncArrow:
ctor = AST_Arrow;
break;
case AST_AsyncFunction:
ctor = AST_Function;
break;
case AST_AsyncGeneratorFunction:
ctor = AST_GeneratorFunction;
break;
} else if (body instanceof AST_Sequence) {
var tail = body.tail_node();
if (tail instanceof AST_Await && is_primitive(compressor, tail.expression)) {
tail = tail.expression.drop_side_effect_free(compressor);
body.expressions.pop();
if (tail) body.expressions.push(tail);
}
return make_node(ctor, exp, exp);
}
return node;
});
var abort = arrow && exp.value && !is_primitive(compressor, exp.value);
var tw = new TreeWalker(function(node) {
if (abort) return true;
if (node instanceof AST_Await) return abort = true;
if (node instanceof AST_Return) {
if (node.value && !is_primitive(compressor, node.value)) return abort = true;
return;
}
if (node instanceof AST_Scope && node !== exp) return true;
if (tw.parent() === exp && node.may_throw(compressor)) return abort = true;
});
exp.walk(tw);
if (!abort) {
var ctor;
switch (exp.CTOR) {
case AST_AsyncArrow:
ctor = AST_Arrow;
break;
case AST_AsyncFunction:
ctor = AST_Function;
break;
case AST_AsyncGeneratorFunction:
ctor = AST_GeneratorFunction;
break;
}
return make_node(ctor, exp, exp);
}
return exp.clone();
}
return drop_body && exp.clone();
}
def(AST_Call, function(compressor, first_in_statement) {
var self = this;
Expand Down
45 changes: 45 additions & 0 deletions test/compress/awaits.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,51 @@ collapse_property_lambda: {
node_version: ">=8"
}

drop_async_1: {
options = {
awaits: true,
inline: true,
side_effects: true,
}
input: {
console.log(function(a) {
(async function() {
a *= 7;
})();
return a;
}(6));
}
expect: {
console.log(function(a) {
void (a *= 7);
return a;
}(6));
}
expect_stdout: "42"
node_version: ">=8"
}

drop_async_2: {
options = {
awaits: true,
collapse_vars: true,
evaluate: true,
inline: true,
side_effects: true,
}
input: {
console.log(function(a) {
(async b => await (a *= b))(7);
return a;
}(6));
}
expect: {
console.log(42);
}
expect_stdout: "42"
node_version: ">=8"
}

drop_return: {
options = {
side_effects: true,
Expand Down
20 changes: 20 additions & 0 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6208,3 +6208,23 @@ reduce_cross_reference_4_toplevel: {
expect: {}
expect_stdout: true
}

recursive_collapse: {
options = {
collapse_vars: true,
reduce_vars: true,
}
input: {
console.log(function f(a) {
var b = a && f();
return b;
}("FAIL") || "PASS");
}
expect: {
console.log(function f(a) {
var b;
return a && f();
}("FAIL") || "PASS");
}
expect_stdout: "PASS"
}

0 comments on commit 8cd95dd

Please sign in to comment.