aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-01-02 12:35:48 +0000
committerGitHub <noreply@github.com>2021-01-02 20:35:48 +0800
commit15ef2727903a00534efbfe5f416340fd73979767 (patch)
tree0ad7de9c29842f37a95745f20608f744e94265bf /lib/compress.js
parentb3a706114ce775f75c4a6da37bff4a8ef32415d5 (diff)
downloadtracifyjs-15ef2727903a00534efbfe5f416340fd73979767.tar.gz
tracifyjs-15ef2727903a00534efbfe5f416340fd73979767.zip
introduce `awaits` (#4495)
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js25
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 0ab65868..d5c22063 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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,
@@ -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();
@@ -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);
@@ -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) {
@@ -8665,6 +8675,7 @@ 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);
@@ -8672,7 +8683,15 @@ merge(Compressor.prototype, {
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;
});