diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-12-06 21:22:40 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-07 05:22:40 +0800 |
commit | 2cbbf5c375a0fae88345c3ed3bc2829b4b1ac250 (patch) | |
tree | f1dd106d04820e8002d2f99cba76206d690d7c96 /lib/compress.js | |
parent | 3c384cf9a8ed4230cf87f14ab017b613b38df628 (diff) | |
download | tracifyjs-2cbbf5c375a0fae88345c3ed3bc2829b4b1ac250.tar.gz tracifyjs-2cbbf5c375a0fae88345c3ed3bc2829b4b1ac250.zip |
support async function (#4333)
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/lib/compress.js b/lib/compress.js index e84465f6..cf138377 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -322,7 +322,7 @@ merge(Compressor.prototype, { if (value instanceof AST_String) return native_fns.String[name]; if (name == "valueOf") return false; if (value instanceof AST_Array) return native_fns.Array[name]; - if (value instanceof AST_Function) return native_fns.Function[name]; + if (value instanceof AST_Lambda) return native_fns.Function[name]; if (value instanceof AST_Object) return native_fns.Object[name]; if (value instanceof AST_RegExp) return native_fns.RegExp[name] && !value.value.global; } @@ -650,14 +650,6 @@ merge(Compressor.prototype, { lhs.walk(scanner); } - def(AST_Accessor, function(tw, descend, compressor) { - push(tw); - reset_variables(tw, compressor, this); - descend(); - pop(tw); - walk_defuns(tw, this); - return true; - }); def(AST_Assign, function(tw, descend, compressor) { var node = this; var left = node.left; @@ -935,6 +927,14 @@ merge(Compressor.prototype, { pop(tw); return true; }); + def(AST_Lambda, function(tw, descend, compressor) { + push(tw); + reset_variables(tw, compressor, this); + descend(); + pop(tw); + walk_defuns(tw, this); + return true; + }); def(AST_Switch, function(tw, descend, compressor) { this.variables.each(function(def) { reset_def(tw, compressor, def); @@ -1365,7 +1365,7 @@ merge(Compressor.prototype, { function is_iife_call(node) { if (node.TYPE != "Call") return false; - return node.expression instanceof AST_Function || is_iife_call(node.expression); + return is_function(node.expression) || is_iife_call(node.expression); } function is_undeclared_ref(node) { @@ -1744,6 +1744,7 @@ merge(Compressor.prototype, { } function is_last_node(node, parent) { + if (node instanceof AST_Await) return true; if (node.TYPE == "Binary") return node.operator == "in" && !is_object(node.right); if (node instanceof AST_Call) { var def, fn = node.expression; @@ -1887,6 +1888,8 @@ merge(Compressor.prototype, { if (expr.left instanceof AST_SymbolRef) { assignments[expr.left.name] = (assignments[expr.left.name] || 0) + 1; } + } else if (expr instanceof AST_Await) { + extract_candidates(expr.expression); } else if (expr instanceof AST_Binary) { extract_candidates(expr.left); extract_candidates(expr.right); @@ -1975,6 +1978,7 @@ merge(Compressor.prototype, { var parent = scanner.parent(level); if (parent instanceof AST_Array) return node; if (parent instanceof AST_Assign) return node; + if (parent instanceof AST_Await) return node; if (parent instanceof AST_Binary) return node; if (parent instanceof AST_Call) return node; if (parent instanceof AST_Case) return node; @@ -2074,6 +2078,7 @@ merge(Compressor.prototype, { if (parent instanceof AST_Assign) { return may_throw(parent) ? node : find_stop_unused(parent, level + 1); } + if (parent instanceof AST_Await) return node; if (parent instanceof AST_Binary) return find_stop_unused(parent, level + 1); if (parent instanceof AST_Call) return find_stop_unused(parent, level + 1); if (parent instanceof AST_Case) return find_stop_unused(parent, level + 1); @@ -4015,7 +4020,7 @@ merge(Compressor.prototype, { def(AST_Call, function(compressor, ignore_side_effects, cached, depth) { var exp = this.expression; var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp; - if (fn instanceof AST_Lambda) { + if (fn instanceof AST_Defun || fn instanceof AST_Function) { if (fn.evaluating) return this; if (fn.name && fn.name.definition().recursive_refs > 0) return this; if (this.is_expr_pure(compressor)) return this; @@ -4142,6 +4147,9 @@ merge(Compressor.prototype, { def(AST_Statement, function() { throw new Error("Cannot negate a statement"); }); + def(AST_AsyncFunction, function() { + return basic_negation(this); + }); def(AST_Function, function() { return basic_negation(this); }); @@ -5107,7 +5115,7 @@ merge(Compressor.prototype, { } if (node === self) return; if (scope === self) { - if (node instanceof AST_Defun) { + if (node instanceof AST_AsyncDefun || node instanceof AST_Defun) { var def = node.name.definition(); if (!drop_funcs && !(def.id in in_use_ids)) { in_use_ids[def.id] = true; @@ -5258,7 +5266,7 @@ merge(Compressor.prototype, { if (node instanceof AST_Call) calls_to_drop_args.push(node); if (scope !== self) return; if (node instanceof AST_Lambda) { - if (drop_funcs && node !== self && node instanceof AST_Defun) { + if (drop_funcs && node !== self && (node instanceof AST_AsyncDefun || node instanceof AST_Defun)) { var def = node.name.definition(); if (!(def.id in in_use_ids)) { log(node.name, "Dropping unused function {name}"); @@ -5266,7 +5274,7 @@ merge(Compressor.prototype, { return in_list ? List.skip : make_node(AST_EmptyStatement, node); } } - if (node instanceof AST_Function && node.name && drop_fn_name(node.name.definition())) { + if (is_function(node) && node.name && drop_fn_name(node.name.definition())) { unused_fn_names.push(node); } if (!(node instanceof AST_Accessor)) { @@ -7642,7 +7650,7 @@ merge(Compressor.prototype, { } } var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp; - var is_func = fn instanceof AST_Lambda; + var is_func = fn instanceof AST_Defun || fn instanceof AST_Function; var stat = is_func && fn.first_statement(); var can_inline = is_func && compressor.option("inline") @@ -8967,7 +8975,10 @@ merge(Compressor.prototype, { def.single_use = false; fixed._squeezed = true; fixed.single_use = true; - if (fixed instanceof AST_Defun) { + if (fixed instanceof AST_AsyncDefun) { + fixed = make_node(AST_AsyncFunction, fixed, fixed); + fixed.name = make_node(AST_SymbolLambda, fixed.name, fixed.name); + } else if (fixed instanceof AST_Defun) { fixed = make_node(AST_Function, fixed, fixed); fixed.name = make_node(AST_SymbolLambda, fixed.name, fixed.name); } |