fix corner cases with await (#4350)

fixes #4349
This commit is contained in:
Alex Lam S.L
2020-12-08 03:26:03 +00:00
committed by GitHub
parent 5fba98608c
commit 4733159782
4 changed files with 88 additions and 19 deletions

View File

@@ -927,6 +927,7 @@ merge(Compressor.prototype, {
return true;
});
def(AST_Lambda, function(tw, descend, compressor) {
this.inlined = false;
push(tw);
reset_variables(tw, compressor, this);
descend();
@@ -1630,7 +1631,7 @@ merge(Compressor.prototype, {
var assign_used = false;
var can_replace = !args || !hit;
if (!can_replace) {
for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; !abort && j < args.length; j++) {
for (var j = scope.argnames.lastIndexOf(candidate.name) + 1; !abort && j < args.length; j++) {
args[j].transform(scanner);
}
can_replace = true;
@@ -1819,7 +1820,7 @@ merge(Compressor.prototype, {
function extract_args() {
var iife, fn = compressor.self();
if (fn instanceof AST_Function
if (is_function(fn)
&& !fn.name
&& !fn.uses_arguments
&& !fn.pinned()
@@ -1830,6 +1831,29 @@ merge(Compressor.prototype, {
})) {
var fn_strict = compressor.has_directive("use strict");
if (fn_strict && !member(fn_strict, fn.body)) fn_strict = false;
var has_await = fn instanceof AST_AsyncFunction ? function(node) {
return node instanceof AST_Symbol && node.name == "await";
} : function(node) {
return node instanceof AST_Await && !tw.find_parent(AST_Scope);
};
var tw = new TreeWalker(function(node) {
if (!arg) return true;
if (has_await(node)) {
arg = null;
return true;
}
if (node instanceof AST_SymbolRef && fn.variables.has(node.name)) {
var s = node.definition().scope;
if (s !== scope) while (s = s.parent_scope) {
if (s === scope) return true;
}
arg = null;
}
if (node instanceof AST_This && (fn_strict || !tw.find_parent(AST_Scope))) {
arg = null;
return true;
}
});
var len = fn.argnames.length;
args = iife.args.slice(len);
var names = Object.create(null);
@@ -1852,20 +1876,7 @@ merge(Compressor.prototype, {
} else if (arg instanceof AST_Lambda && arg.pinned()) {
arg = null;
} else {
arg.walk(new TreeWalker(function(node) {
if (!arg) return true;
if (node instanceof AST_SymbolRef && fn.variables.has(node.name)) {
var s = node.definition().scope;
if (s !== scope) while (s = s.parent_scope) {
if (s === scope) return true;
}
arg = null;
}
if (node instanceof AST_This && (fn_strict || !this.find_parent(AST_Scope))) {
arg = null;
return true;
}
}));
arg.walk(tw);
}
if (arg) candidates.unshift([ make_node(AST_VarDef, sym, {
name: sym,
@@ -8982,7 +8993,7 @@ merge(Compressor.prototype, {
single_use = fixed.is_constant_expression(self.scope);
if (single_use == "f") {
var scope = self.scope;
do if (scope instanceof AST_Defun || scope instanceof AST_Function) {
do if (is_defun(scope) || is_function(scope)) {
scope.inlined = true;
} while (scope = scope.parent_scope);
}