retrofit AST_BlockStatement as block-scoped (#4177)

This commit is contained in:
Alex Lam S.L
2020-10-04 18:58:50 +01:00
committed by GitHub
parent 58ac5b9bd5
commit f9946767c9
6 changed files with 104 additions and 59 deletions

View File

@@ -368,10 +368,10 @@ merge(Compressor.prototype, {
&& !(def.init instanceof AST_Function && def.init !== def.scope)
&& def.init;
if (def.fixed instanceof AST_Defun && !all(def.references, function(ref) {
var scope = ref.scope;
var scope = ref.scope.resolve();
do {
if (def.scope === scope) return true;
} while (scope instanceof AST_Function && (scope = scope.parent_scope));
} while (scope instanceof AST_Function && (scope = scope.parent_scope.resolve()));
})) {
tw.defun_ids[def.id] = false;
}
@@ -854,7 +854,7 @@ merge(Compressor.prototype, {
&& !value.pinned()
&& (!d.in_loop || tw.parent() instanceof AST_Call)
|| !d.in_loop
&& d.scope === this.scope
&& d.scope === this.scope.resolve()
&& value.is_constant_expression();
} else {
d.single_use = false;
@@ -2585,7 +2585,7 @@ merge(Compressor.prototype, {
var lhs = expr.left;
if (!(lhs instanceof AST_SymbolRef)) break;
if (is_undeclared_ref(lhs)) break;
if (lhs.scope !== scope) break;
if (lhs.scope.resolve() !== scope) break;
var def = lhs.definition();
if (def.scope !== scope) break;
if (def.orig.length > def.eliminated + 1) break;
@@ -5179,12 +5179,12 @@ merge(Compressor.prototype, {
if (!(node_def.id in in_use_ids)) {
in_use_ids[node_def.id] = true;
in_use.push(node_def);
if (node.scope !== node_def.scope) {
var redef = node_def.redefined();
if (redef && !(redef.id in in_use_ids)) {
in_use_ids[redef.id] = true;
in_use.push(redef);
}
}
if (node.scope !== node_def.scope) {
var redef = node_def.redefined();
if (redef && !(redef.id in in_use_ids)) {
in_use_ids[redef.id] = true;
in_use.push(redef);
}
}
if (track_assigns(node_def, node)) add_assigns(node_def, node);
@@ -7031,6 +7031,9 @@ merge(Compressor.prototype, {
var stat = fn.body[i];
if (stat instanceof AST_Defun) {
if (!safe_to_inject || var_exists(used, stat.name.name)) return false;
if (!all(stat.enclosed, function(def) {
return def.scope === stat || !catches[def.name];
})) return false;
continue;
}
if (!(stat instanceof AST_Var)) continue;
@@ -7067,7 +7070,7 @@ merge(Compressor.prototype, {
}
} while (!(scope instanceof AST_Scope));
var safe_to_inject = (!(scope instanceof AST_Toplevel) || compressor.toplevel.vars)
&& (exp !== fn || fn.parent_scope === compressor.find_parent(AST_Scope));
&& (exp !== fn || fn.parent_scope.resolve() === compressor.find_parent(AST_Scope));
var inline = compressor.option("inline");
var used = Object.create(catches);
if (!can_inject_args(catches, used, inline >= 2 && safe_to_inject)) return false;
@@ -8087,7 +8090,7 @@ merge(Compressor.prototype, {
if (!compressor.option("ie8")
&& is_undeclared_ref(self)
// testing against `self.scope.uses_with` is an optimization
&& !(self.scope.uses_with && compressor.find_parent(AST_With))) {
&& !(self.scope.resolve().uses_with && compressor.find_parent(AST_With))) {
switch (self.name) {
case "undefined":
return make_node(AST_Undefined, self).optimize(compressor);
@@ -8104,16 +8107,14 @@ merge(Compressor.prototype, {
var single_use = def.single_use && !(parent instanceof AST_Call && parent.is_expr_pure(compressor));
if (single_use) {
if (fixed instanceof AST_Lambda) {
if ((def.scope !== self.scope || def.in_loop)
if ((def.scope !== self.scope.resolve() || def.in_loop)
&& (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) {
single_use = false;
} else if (recursive_ref(compressor, def)) {
single_use = false;
} else if (fixed.name && fixed.name.definition() !== def) {
single_use = false;
} else if (fixed.parent_scope !== self.scope
|| !(self.scope instanceof AST_Scope)
|| def.orig[0] instanceof AST_SymbolFunarg) {
} else if (fixed.parent_scope !== self.scope.resolve() || def.orig[0] instanceof AST_SymbolFunarg) {
single_use = fixed.is_constant_expression(self.scope);
if (single_use == "f") {
var scope = self.scope;
@@ -8894,7 +8895,7 @@ merge(Compressor.prototype, {
&& expr instanceof AST_SymbolRef
&& is_arguments(def = expr.definition())
&& prop instanceof AST_Number
&& (fn = expr.scope) === find_lambda()) {
&& (fn = expr.scope.resolve()) === find_lambda()) {
var index = prop.value;
if (parent instanceof AST_UnaryPrefix && parent.operator == "delete") {
if (!def.deleted) def.deleted = [];