enhance collapse_vars (#5555)

This commit is contained in:
Alex Lam S.L
2022-07-11 23:10:40 +08:00
committed by GitHub
parent 4778cf88e2
commit 154edf0427
4 changed files with 218 additions and 20 deletions

View File

@@ -1665,6 +1665,18 @@ Compressor.prototype.compress = function(node) {
}
}
function find_try(compressor, level, node, scope, may_throw, sync) {
for (var parent; parent = compressor.parent(level++); node = parent) {
if (parent === scope) return false;
if (sync && parent instanceof AST_Lambda) {
if (parent.name || is_async(parent) || is_generator(parent)) return true;
} else if (parent instanceof AST_Try) {
if (parent.bfinally && parent.bfinally !== node) return true;
if (may_throw && parent.bcatch && parent.bcatch !== node) return true;
}
}
}
var identifier_atom = makePredicate("Infinity NaN undefined");
function is_lhs_read_only(lhs, compressor) {
if (lhs instanceof AST_ObjectIdentity) return true;
@@ -2585,7 +2597,7 @@ Compressor.prototype.compress = function(node) {
});
}
if (node instanceof AST_DestructuredObject) {
if (!value.is_defined(compressor)) return true;
if (value.may_throw_on_access(compressor)) return true;
return !all(node.properties, function(prop) {
if (prop.key instanceof AST_Node && reject(prop.key)) return false;
return !may_throw_arg(reject, prop.value);
@@ -2608,11 +2620,18 @@ Compressor.prototype.compress = function(node) {
})) {
var fn_strict = fn.in_strict_mode(compressor)
&& !fn.parent_scope.resolve(true).in_strict_mode(compressor);
var has_await = is_async(fn) ? function(node) {
return node instanceof AST_Symbol && node.name == "await";
} : function(node) {
return node instanceof AST_Await && !tw.find_parent(AST_Scope);
};
var check_arg = false, has_await;
if (is_async(fn)) {
check_arg = true;
has_await = function(node) {
return node instanceof AST_Symbol && node.name == "await";
};
} else {
check_arg = find_try(compressor, 1, iife, null, true, true);
has_await = function(node) {
return node instanceof AST_Await && !tw.find_parent(AST_Scope);
};
}
var arg_scope = null;
var tw = new TreeWalker(function(node, descend) {
if (!arg) return true;
@@ -2645,18 +2664,21 @@ Compressor.prototype.compress = function(node) {
for (var i = fn.argnames.length; --i >= 0;) {
var sym = fn.argnames[i];
var arg = args[i];
var value;
var value = null;
if (sym instanceof AST_DefaultValue) {
value = sym.value;
sym = sym.name;
args[len + i] = value;
}
if (sym instanceof AST_Destructured) {
if (!may_throw_arg(function(node) {
if (check_arg && may_throw_arg(function(node) {
return node.has_side_effects(compressor);
}, sym, arg)) continue;
candidates.length = 0;
break;
}, sym, arg)) {
candidates.length = 0;
break;
}
args[len + i] = fn.argnames[i];
continue;
}
if (names.has(sym.name)) continue;
names.set(sym.name, true);
@@ -12656,15 +12678,7 @@ Compressor.prototype.compress = function(node) {
self.right = make_node(AST_Null, right);
var may_throw = node.may_throw(compressor);
self.right = right;
for (var parent; parent = compressor.parent(level++); node = parent) {
if (parent === scope) return false;
if (sync && parent instanceof AST_Lambda) {
if (parent.name || is_async(parent) || is_generator(parent)) return true;
} else if (parent instanceof AST_Try) {
if (parent.bfinally && parent.bfinally !== node) return true;
if (may_throw && parent.bcatch && parent.bcatch !== node) return true;
}
}
return find_try(compressor, level, node, scope, may_throw, sync);
}
function strip_assignment(def) {