enhance "functions" (#4791)

This commit is contained in:
Alex Lam S.L
2021-03-16 21:38:33 +00:00
committed by GitHub
parent b872ffee01
commit b244b4ec21
4 changed files with 156 additions and 37 deletions

View File

@@ -6203,8 +6203,8 @@ merge(Compressor.prototype, {
&& value instanceof AST_LambdaExpression
&& !is_arrow(value)
&& assigned_once(value, sym.references)
&& can_declare_defun()
&& can_rename(value, def.name.name)) {
&& can_declare_defun(value)
&& (old_def = rename_def(value, def.name.name)) !== false) {
AST_Node.warn("Declaring {name} as function [{file}:{line},{col}]", template(def.name));
var ctor;
switch (value.CTOR) {
@@ -6269,27 +6269,26 @@ merge(Compressor.prototype, {
});
}
function can_declare_defun() {
if (compressor.has_directive("use strict")) return parent instanceof AST_Scope;
function can_declare_defun(fn) {
if (compressor.has_directive("use strict") || !(fn instanceof AST_Function)) {
return parent instanceof AST_Scope;
}
return parent instanceof AST_Block
|| parent instanceof AST_For && parent.init === node
|| parent instanceof AST_If;
}
function can_rename(fn, name) {
if (!fn.name) return !fn.variables.get(name);
old_def = fn.name.definition();
if (old_def.orig.length > 1) {
old_def = null;
} else {
if (old_def.assignments > 0) return false;
if (old_def.name == name) return true;
}
function rename_def(fn, name) {
if (!fn.name) return null;
var def = fn.name.definition();
if (def.orig.length > 1) return null;
if (def.assignments > 0) return false;
if (def.name == name) return def;
if (name == "await" && is_async(fn)) return false;
if (name == "yield" && is_generator(fn)) return false;
return !old_def || all(old_def.references, function(ref) {
return all(def.references, function(ref) {
return ref.scope.find_variable(name) === sym;
});
}) && def;
}
function is_catch(node) {