support generator functions (#4620)

This commit is contained in:
Alex Lam S.L
2021-02-07 22:44:20 +00:00
committed by GitHub
parent c44b6399c3
commit fd4caf7a9c
12 changed files with 1186 additions and 182 deletions

View File

@@ -104,6 +104,7 @@ function Compressor(options, false_by_default) {
unsafe_undefined: false,
unused : !false_by_default,
varify : !false_by_default,
yields : !false_by_default,
}, true);
var evaluate = this.options["evaluate"];
this.eval_threshold = /eager/.test(evaluate) ? 1 / 0 : +evaluate;
@@ -344,7 +345,7 @@ merge(Compressor.prototype, {
return !immutable
&& parent.expression === node
&& !parent.is_expr_pure(compressor)
&& (!is_function(value) || !(parent instanceof AST_New) && value.contains_this());
&& (!(value instanceof AST_LambdaExpression) || !(parent instanceof AST_New) && value.contains_this());
}
if (parent instanceof AST_ForIn) return parent.init === node;
if (parent instanceof AST_ObjectKeyVal) {
@@ -396,13 +397,13 @@ merge(Compressor.prototype, {
def.fixed = !def.const_redefs
&& !def.scope.pinned()
&& !compressor.exposed(def)
&& !(is_function(def.init) && def.init !== def.scope)
&& !(def.init instanceof AST_LambdaExpression && def.init !== def.scope)
&& def.init;
if (is_defun(def.fixed) && !all(def.references, function(ref) {
if (def.fixed instanceof AST_LambdaDefinition && !all(def.references, function(ref) {
var scope = ref.scope.resolve();
do {
if (def.scope === scope) return true;
} while (is_function(scope) && (scope = scope.parent_scope.resolve()));
} while (scope instanceof AST_LambdaExpression && (scope = scope.parent_scope.resolve()));
})) {
tw.defun_ids[def.id] = false;
}
@@ -427,7 +428,7 @@ merge(Compressor.prototype, {
scope.may_call_this = noop;
if (!scope.contains_this()) return;
scope.functions.each(function(def) {
if (is_defun(def.init) && !(def.id in tw.defun_ids)) {
if (def.init instanceof AST_LambdaDefinition && !(def.id in tw.defun_ids)) {
tw.defun_ids[def.id] = false;
}
});
@@ -467,7 +468,7 @@ merge(Compressor.prototype, {
function walk_defuns(tw, scope) {
scope.functions.each(function(def) {
if (is_defun(def.init) && !tw.defun_visited[def.id]) {
if (def.init instanceof AST_LambdaDefinition && !tw.defun_visited[def.id]) {
tw.defun_ids[def.id] = tw.safe_ids;
def.init.walk(tw);
}
@@ -505,7 +506,7 @@ merge(Compressor.prototype, {
}
return !safe.assign || safe.assign === tw.safe_ids;
}
return is_defun(def.fixed);
return def.fixed instanceof AST_LambdaDefinition;
}
function safe_to_assign(tw, def, declare) {
@@ -702,25 +703,11 @@ merge(Compressor.prototype, {
lhs.walk(scanner);
}
function reduce_defun(tw, descend, compressor) {
var id = this.name.definition().id;
if (tw.defun_visited[id]) return true;
if (tw.defun_ids[id] !== tw.safe_ids) return true;
tw.defun_visited[id] = true;
this.inlined = false;
push(tw);
reset_variables(tw, compressor, this);
descend();
pop(tw);
walk_defuns(tw, this);
return true;
}
function reduce_iife(tw, descend, compressor) {
var fn = this;
fn.inlined = false;
var iife = tw.parent();
var hit = is_async(fn);
var hit = is_async(fn) || is_generator(fn);
var aborts = false;
fn.walk(new TreeWalker(function(node) {
if (hit) return aborts = true;
@@ -848,7 +835,6 @@ merge(Compressor.prototype, {
}
}
});
def(AST_AsyncDefun, reduce_defun);
def(AST_Binary, function(tw) {
if (!lazy_op[this.operator]) return;
this.left.walk(tw);
@@ -865,7 +851,7 @@ merge(Compressor.prototype, {
def(AST_Call, function(tw, descend) {
tw.find_parent(AST_Scope).may_call_this();
var exp = this.expression;
if (is_function(exp)) {
if (exp instanceof AST_LambdaExpression) {
var iife = !exp.name;
this.args.forEach(function(arg) {
arg.walk(tw);
@@ -878,7 +864,7 @@ merge(Compressor.prototype, {
} else if (exp instanceof AST_SymbolRef) {
var def = exp.definition();
if (this.TYPE == "Call" && tw.in_boolean_context()) def.bool_fn++;
if (!is_defun(def.fixed)) return;
if (!(def.fixed instanceof AST_LambdaDefinition)) return;
var defun = mark_defun(tw, def);
if (!defun) return;
descend();
@@ -909,7 +895,6 @@ merge(Compressor.prototype, {
pop(tw);
return true;
});
def(AST_Defun, reduce_defun);
def(AST_Do, function(tw) {
var saved_loop = tw.in_loop;
tw.in_loop = this;
@@ -1008,6 +993,19 @@ merge(Compressor.prototype, {
walk_defuns(tw, fn);
return true;
});
def(AST_LambdaDefinition, function(tw, descend, compressor) {
var id = this.name.definition().id;
if (tw.defun_visited[id]) return true;
if (tw.defun_ids[id] !== tw.safe_ids) return true;
tw.defun_visited[id] = true;
this.inlined = false;
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);
@@ -1079,7 +1077,8 @@ merge(Compressor.prototype, {
}
if (!this.fixed) this.fixed = d.fixed;
var parent;
if (is_defun(d.fixed) && !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) {
if (d.fixed instanceof AST_LambdaDefinition
&& !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) {
var defun = mark_defun(tw, d);
if (defun) defun.walk(tw);
}
@@ -1455,8 +1454,10 @@ merge(Compressor.prototype, {
function is_iife_call(node) {
if (node.TYPE != "Call") return false;
var exp = node.expression;
return exp instanceof AST_AsyncFunction || exp instanceof AST_Function || is_iife_call(exp);
do {
node = node.expression;
} while (node instanceof AST_PropAccess);
return node instanceof AST_LambdaExpression ? !is_arrow(node) : is_iife_call(node);
}
function is_undeclared_ref(node) {
@@ -1843,7 +1844,7 @@ merge(Compressor.prototype, {
if (node instanceof AST_Call) {
if (!(lhs instanceof AST_PropAccess)) return false;
if (!lhs.equivalent_to(node.expression)) return false;
return !(is_function(rvalue) && !rvalue.contains_this());
return !(rvalue instanceof AST_LambdaExpression && !rvalue.contains_this());
}
if (node instanceof AST_Debugger) return true;
if (node instanceof AST_Defun) return funarg && lhs.name === node.name.name;
@@ -1951,6 +1952,7 @@ merge(Compressor.prototype, {
&& (lvalues.has(node.name) || side_effects && may_modify(node));
}, true);
}
if (node instanceof AST_Yield) return true;
var sym = is_lhs(node.left, node);
if (!sym) return false;
if (sym instanceof AST_PropAccess) return true;
@@ -1988,7 +1990,8 @@ merge(Compressor.prototype, {
function extract_args() {
var iife, fn = compressor.self();
if (is_function(fn)
if (fn instanceof AST_LambdaExpression
&& !is_generator(fn)
&& !fn.name
&& !fn.uses_arguments
&& !fn.pinned()
@@ -2482,7 +2485,7 @@ merge(Compressor.prototype, {
if (modify_toplevel) return;
var exp = node.expression;
if (exp instanceof AST_PropAccess) return;
if (is_function(exp) && !exp.contains_this()) return;
if (exp instanceof AST_LambdaExpression && !exp.contains_this()) return;
modify_toplevel = true;
} else if (node instanceof AST_PropAccess && may_be_global(node.expression)) {
if (node === lhs && !(expr instanceof AST_Unary)) {
@@ -3249,7 +3252,7 @@ merge(Compressor.prototype, {
}
function extract_declarations_from_unreachable_code(compressor, stat, target) {
if (!(stat instanceof AST_Definitions || is_defun(stat))) {
if (!(stat instanceof AST_Definitions || stat instanceof AST_LambdaDefinition)) {
AST_Node.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
}
var block;
@@ -3265,7 +3268,7 @@ merge(Compressor.prototype, {
}
return true;
}
if (is_defun(node)) {
if (node instanceof AST_LambdaDefinition) {
push(node);
return true;
}
@@ -5114,7 +5117,7 @@ merge(Compressor.prototype, {
if (node instanceof AST_Call) {
var exp = node.expression;
var tail = exp.tail_node();
if (!is_function(tail)) return;
if (!(tail instanceof AST_LambdaExpression)) return;
if (exp !== tail) exp.expressions.slice(0, -1).forEach(function(node) {
node.walk(tw);
});
@@ -5534,7 +5537,7 @@ merge(Compressor.prototype, {
}
if (node === self) return;
if (scope === self) {
if (is_defun(node)) {
if (node instanceof AST_LambdaDefinition) {
var def = node.name.definition();
if (!drop_funcs && !(def.id in in_use_ids)) {
in_use_ids[def.id] = true;
@@ -5735,7 +5738,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 && is_defun(node)) {
if (drop_funcs && node !== self && node instanceof AST_LambdaDefinition) {
var def = node.name.definition();
if (!(def.id in in_use_ids)) {
log(node.name, "Dropping unused function {name}");
@@ -5743,7 +5746,7 @@ merge(Compressor.prototype, {
return in_list ? List.skip : make_node(AST_EmptyStatement, node);
}
}
if (is_function(node) && node.name && drop_fn_name(node.name.definition())) {
if (node instanceof AST_LambdaExpression && node.name && drop_fn_name(node.name.definition())) {
unused_fn_names.push(node);
}
if (!(node instanceof AST_Accessor)) {
@@ -5853,12 +5856,28 @@ merge(Compressor.prototype, {
&& node instanceof AST_Var
&& var_defs[sym.id] == 1
&& sym.assignments == 0
&& (value instanceof AST_AsyncFunction || value instanceof AST_Function)
&& value instanceof AST_LambdaExpression
&& !is_arrow(value)
&& assigned_once(value, sym.references)
&& can_declare_defun()
&& can_rename(value, def.name.name)) {
AST_Node.warn("Declaring {name} as function [{file}:{line},{col}]", template(def.name));
var defun = make_node(value instanceof AST_Function ? AST_Defun : AST_AsyncDefun, def, value);
var ctor;
switch (value.CTOR) {
case AST_AsyncFunction:
ctor = AST_AsyncDefun;
break;
case AST_AsyncGeneratorFunction:
ctor = AST_AsyncGeneratorDefun;
break;
case AST_Function:
ctor = AST_Defun;
break;
case AST_GeneratorFunction:
ctor = AST_GeneratorDefun;
break;
}
var defun = make_node(ctor, def, value);
defun.name = make_node(AST_SymbolDefun, def.name, def.name);
var name_def = def.name.scope.resolve().def_function(defun.name);
if (old_def) old_def.forEach(function(node) {
@@ -5916,6 +5935,7 @@ merge(Compressor.prototype, {
if (old_def.assignments > 0) return false;
if (old_def.name == name) return true;
if (name == "await" && is_async(fn)) return false;
if (name == "yield" && is_generator(fn)) return false;
return all(old_def.references, function(ref) {
return ref.scope.find_variable(name) === sym;
});
@@ -6838,7 +6858,6 @@ merge(Compressor.prototype, {
})) return this;
return make_sequence(this, values.map(convert_spread));
});
def(AST_Arrow, return_null);
def(AST_Assign, function(compressor) {
var left = this.left;
if (left instanceof AST_PropAccess) {
@@ -6855,8 +6874,6 @@ merge(Compressor.prototype, {
}
return this;
});
def(AST_AsyncArrow, return_null);
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);
@@ -6991,6 +7008,7 @@ merge(Compressor.prototype, {
def(AST_Function, function(compressor) {
return fn_name_unused(this, compressor) ? null : this;
});
def(AST_LambdaExpression, return_null);
def(AST_Object, function(compressor, first_in_statement) {
var exprs = [];
this.properties.forEach(function(prop) {
@@ -7553,7 +7571,7 @@ merge(Compressor.prototype, {
var exprs = [];
for (var i = 0; i < stat.body.length; i++) {
var line = stat.body[i];
if (is_defun(line)) {
if (line instanceof AST_LambdaDefinition) {
defuns.push(line);
} else if (line instanceof AST_EmptyStatement) {
continue;
@@ -7570,7 +7588,7 @@ merge(Compressor.prototype, {
}
return exprs;
}
if (is_defun(stat)) {
if (stat instanceof AST_LambdaDefinition) {
defuns.push(stat);
return [];
}
@@ -8274,8 +8292,10 @@ merge(Compressor.prototype, {
}
}
var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp;
var is_func = fn instanceof AST_Lambda && (!is_async(fn)
|| compressor.option("awaits") && compressor.parent() instanceof AST_Await);
var parent = compressor.parent(), current = compressor.self();
var is_func = fn instanceof AST_Lambda
&& (!is_async(fn) || compressor.option("awaits") && parent instanceof AST_Await)
&& (!is_generator(fn) || compressor.option("yields") && current instanceof AST_Yield && current.nested);
var stat = is_func && fn.first_statement();
var has_default = 0, has_destructured = false;
var has_spread = !all(self.args, function(arg) {
@@ -8308,7 +8328,7 @@ merge(Compressor.prototype, {
if (can_inline
&& !fn.uses_arguments
&& !fn.pinned()
&& !(fn.name && is_function(fn))
&& !(fn.name && fn instanceof AST_LambdaExpression)
&& (exp === fn || !recursive_ref(compressor, def = exp.definition())
&& fn.is_constant_expression(find_scope(compressor)))
&& !has_spread
@@ -8339,7 +8359,7 @@ merge(Compressor.prototype, {
return arg;
})).optimize(compressor);
fn.inlined = save_inlined;
node = maintain_this_binding(compressor, compressor.parent(), compressor.self(), node);
node = maintain_this_binding(compressor, parent, current, node);
if (replacing || best_of_expression(node, self) === node) {
refs.forEach(function(ref) {
var def = ref.definition();
@@ -8358,7 +8378,7 @@ merge(Compressor.prototype, {
fn._squeezed = true;
if (exp !== fn) fn.parent_scope = exp.scope;
var node = make_sequence(self, flatten_fn()).optimize(compressor);
return maintain_this_binding(compressor, compressor.parent(), compressor.self(), node);
return maintain_this_binding(compressor, parent, current, node);
}
}
if (compressor.option("side_effects")
@@ -8380,9 +8400,7 @@ merge(Compressor.prototype, {
}
}
}
if (compressor.option("negate_iife")
&& compressor.parent() instanceof AST_SimpleStatement
&& is_iife_call(self)) {
if (compressor.option("negate_iife") && parent instanceof AST_SimpleStatement && is_iife_call(current)) {
return self.negate(compressor, true);
}
return try_evaluate(compressor, self);
@@ -8480,7 +8498,7 @@ merge(Compressor.prototype, {
if (node === fn) return;
if (is_arrow(node)) {
for (var i = 0; safe && i < node.argnames.length; i++) node.argnames[i].walk(tw);
} else if (is_defun(node) && node.name.name == "await") {
} else if (node instanceof AST_LambdaDefinition && node.name.name == "await") {
safe = false;
}
return true;
@@ -8566,7 +8584,7 @@ merge(Compressor.prototype, {
in_order = null;
return;
}
if (is_defun(def.init)) return abort = true;
if (def.init instanceof AST_LambdaDefinition) return abort = true;
if (is_lhs(node, this.parent())) return abort = true;
var index = resolve_index(def);
if (!(begin < index)) begin = index;
@@ -8619,7 +8637,7 @@ merge(Compressor.prototype, {
function can_inject_vars(defined, used, safe_to_inject) {
for (var i = 0; i < fn.body.length; i++) {
var stat = fn.body[i];
if (is_defun(stat)) {
if (stat instanceof AST_LambdaDefinition) {
if (!safe_to_inject || var_exists(used, stat.name.name)) return false;
if (!all(stat.enclosed, function(def) {
return def.scope === stat || !defined[def.name];
@@ -8640,7 +8658,7 @@ merge(Compressor.prototype, {
function can_inject_symbols() {
var defined = Object.create(null);
var level = 0, child;
scope = compressor.self();
scope = current;
do {
if (scope.variables) scope.variables.each(function(def) {
defined[def.name] = true;
@@ -8806,7 +8824,7 @@ merge(Compressor.prototype, {
flatten_vars(decls, expressions);
expressions.push(value);
var args = fn.body.filter(function(stat) {
if (is_defun(stat)) {
if (stat instanceof AST_LambdaDefinition) {
var def = stat.name.definition();
scope.functions.set(def.name, def);
scope.variables.set(def.name, def);
@@ -9040,6 +9058,20 @@ merge(Compressor.prototype, {
return self;
});
OPT(AST_Yield, function(self, compressor) {
if (!compressor.option("yields")) return self;
if (compressor.option("sequences")) {
var seq = lift_sequence_in_expression(self, compressor);
if (seq !== self) return seq.optimize(compressor);
}
var exp = self.expression;
if (self.nested && exp.TYPE == "Call") {
var inlined = exp.clone().optimize(compressor);
if (inlined.TYPE != "Call") return inlined;
}
return self;
});
AST_Binary.DEFMETHOD("lift_sequences", function(compressor) {
if (this.left instanceof AST_PropAccess) {
if (!(this.left.expression instanceof AST_Sequence)) return this;
@@ -9802,7 +9834,9 @@ merge(Compressor.prototype, {
if (single_use == "f") {
var scope = self.scope;
do {
if (is_defun(scope) || is_function(scope)) scope.inlined = true;
if (scope instanceof AST_LambdaDefinition || scope instanceof AST_LambdaExpression) {
scope.inlined = true;
}
} while (scope = scope.parent_scope);
}
} else if (fixed.name && fixed.name.name == "await" && is_async(fixed)) {
@@ -9817,11 +9851,23 @@ merge(Compressor.prototype, {
def.single_use = false;
fixed._squeezed = true;
fixed.single_use = true;
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);
if (fixed instanceof AST_LambdaDefinition) {
var ctor;
switch (fixed.CTOR) {
case AST_AsyncDefun:
ctor = AST_AsyncFunction;
break;
case AST_AsyncGeneratorDefun:
ctor = AST_AsyncGeneratorFunction;
break;
case AST_Defun:
ctor = AST_Function;
break;
case AST_GeneratorDefun:
ctor = AST_GeneratorFunction;
break;
}
fixed = make_node(ctor, fixed, fixed);
fixed.name = make_node(AST_SymbolLambda, fixed.name, fixed.name);
}
if (fixed instanceof AST_Lambda) {