Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce awaits #4495

Merged
merged 1 commit into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function Compressor(options, false_by_default) {
arguments : !false_by_default,
arrows : !false_by_default,
assignments : !false_by_default,
awaits : !false_by_default,
booleans : !false_by_default,
collapse_vars : !false_by_default,
comparisons : !false_by_default,
Expand Down Expand Up @@ -6651,6 +6652,7 @@ merge(Compressor.prototype, {
});
def(AST_AsyncFunction, return_null);
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 node = this.clone();
Expand Down Expand Up @@ -6811,13 +6813,20 @@ merge(Compressor.prototype, {
var expressions = trim(this.expressions, compressor, first_in_statement);
if (!expressions) return null;
var end = expressions.length - 1;
var last = expressions[end];
if (compressor.option("awaits") && end > 0 && last instanceof AST_Await && last.expression.is_constant()) {
expressions = expressions.slice(0, -1);
end--;
last.expression = expressions[end];
expressions[end] = last;
}
var assign, cond, lhs;
if (compressor.option("conditionals")
&& end > 0
&& (assign = expressions[end - 1]) instanceof AST_Assign
&& assign.operator == "="
&& (lhs = assign.left) instanceof AST_SymbolRef
&& (cond = to_conditional_assignment(compressor, lhs.definition(), assign.right, expressions[end]))) {
&& (cond = to_conditional_assignment(compressor, lhs.definition(), assign.right, last))) {
assign = assign.clone();
assign.right = cond;
expressions = expressions.slice(0, -2);
Expand Down Expand Up @@ -8033,7 +8042,8 @@ merge(Compressor.prototype, {
}
}
var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp;
var is_func = fn instanceof AST_Lambda && (!is_async(fn) || compressor.parent() instanceof AST_Await);
var is_func = fn instanceof AST_Lambda && (!is_async(fn)
|| compressor.option("awaits") && compressor.parent() instanceof AST_Await);
var stat = is_func && fn.first_statement();
var has_default = false;
var can_drop = is_func && all(fn.argnames, function(argname, index) {
Expand Down Expand Up @@ -8665,14 +8675,23 @@ merge(Compressor.prototype, {
});

OPT(AST_Await, function(self, compressor) {
if (!compressor.option("awaits")) return self;
if (compressor.option("sequences")) {
var seq = lift_sequence_in_expression(self, compressor);
if (seq !== self) return seq.optimize(compressor);
}
if (compressor.option("side_effects")) {
var exp = self.expression;
if (exp instanceof AST_Await) return exp;
if (exp instanceof AST_UnaryPrefix && exp.expression instanceof AST_Await) return exp;
if (exp instanceof AST_UnaryPrefix) {
if (exp.expression instanceof AST_Await) return exp;
if (exp.operator == "void") return make_node(AST_UnaryPrefix, self, {
operator: "void",
expression: make_node(AST_Await, self, {
expression: exp.expression,
}),
});
}
}
return self;
});
Expand Down
29 changes: 29 additions & 0 deletions test/compress/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ dont_inline: {

inline_await_1: {
options = {
awaits: true,
inline: true,
reduce_vars: true,
unused: true,
Expand All @@ -167,6 +168,7 @@ inline_await_1: {

inline_await_1_trim: {
options = {
awaits: true,
if_return: true,
inline: true,
reduce_vars: true,
Expand Down Expand Up @@ -194,6 +196,7 @@ inline_await_1_trim: {

inline_await_2: {
options = {
awaits: true,
inline: true,
reduce_vars: true,
unused: true,
Expand All @@ -219,6 +222,7 @@ inline_await_2: {

inline_await_2_trim: {
options = {
awaits: true,
if_return: true,
inline: true,
reduce_vars: true,
Expand Down Expand Up @@ -246,6 +250,7 @@ inline_await_2_trim: {

inline_await_3: {
options = {
awaits: true,
inline: true,
reduce_vars: true,
unused: true,
Expand All @@ -270,6 +275,7 @@ inline_await_3: {

inline_await_3_trim: {
options = {
awaits: true,
inline: true,
reduce_vars: true,
sequences: true,
Expand All @@ -296,6 +302,7 @@ inline_await_3_trim: {

await_unary: {
options = {
awaits: true,
side_effects: true,
}
input: {
Expand All @@ -320,6 +327,28 @@ await_unary: {
node_version: ">=8"
}

await_void: {
options = {
awaits: true,
if_return: true,
sequences: true,
side_effects: true,
}
input: {
(async function() {
console.log("PASS");
return await void 42;
})();
}
expect: {
(async function() {
await console.log("PASS");
})();
}
expect_stdout: "PASS"
node_version: ">=8"
}

evaluate: {
options = {
evaluate: true,
Expand Down
4 changes: 2 additions & 2 deletions tools/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ var fs = require("fs");
exports.FILES = [
require.resolve("../lib/utils.js"),
require.resolve("../lib/ast.js"),
require.resolve("../lib/parse.js"),
require.resolve("../lib/transform.js"),
require.resolve("../lib/parse.js"),
require.resolve("../lib/scope.js"),
require.resolve("../lib/output.js"),
require.resolve("../lib/compress.js"),
require.resolve("../lib/output.js"),
require.resolve("../lib/sourcemap.js"),
require.resolve("../lib/mozilla-ast.js"),
require.resolve("../lib/propmangle.js"),
Expand Down