enhance varify (#5737)

This commit is contained in:
Alex Lam S.L
2022-11-21 03:30:02 +00:00
committed by GitHub
parent 1d400f1a25
commit 21aff996a5
3 changed files with 78 additions and 6 deletions

View File

@@ -98,7 +98,7 @@ function Compressor(options, false_by_default) {
switches : !false_by_default,
templates : !false_by_default,
top_retain : null,
toplevel : !!(options && (options["module"] || options["top_retain"])),
toplevel : !!(options && !options["expression"] && (options["module"] || options["top_retain"])),
typeofs : !false_by_default,
unsafe : false,
unsafe_comps : false,
@@ -9357,7 +9357,7 @@ Compressor.prototype.compress = function(node) {
OPT(AST_ForEnumeration, function(self, compressor) {
if (compressor.option("varify") && is_lexical_definition(self.init)) {
var name = self.init.definitions[0].name;
if ((name instanceof AST_Destructured || name instanceof AST_SymbolLet)
if ((compressor.option("module") || name instanceof AST_Destructured || name instanceof AST_SymbolLet)
&& !name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) {
var def = node.definition();
@@ -9365,6 +9365,8 @@ Compressor.prototype.compress = function(node) {
}
}, true)) {
self.init = to_var(self.init, self.resolve());
} else if (can_letify(self.init, compressor, 1)) {
self.init = to_let(self.init);
}
}
return self;
@@ -10178,6 +10180,20 @@ Compressor.prototype.compress = function(node) {
}
}
function to_let(stat) {
return make_node(AST_Let, stat, {
definitions: stat.definitions.map(function(defn) {
return make_node(AST_VarDef, defn, {
name: defn.name.convert_symbol(AST_SymbolLet, function(name, node) {
var def = name.definition();
def.orig[def.orig.indexOf(node)] = name;
}),
value: defn.value,
});
}),
});
}
function to_var(stat, scope) {
return make_node(AST_Var, stat, {
definitions: stat.definitions.map(function(defn) {
@@ -10197,6 +10213,18 @@ Compressor.prototype.compress = function(node) {
});
}
function can_letify(stat, compressor, assigned) {
if (!(stat instanceof AST_Const)) return false;
if (!compressor.option("module") && all(stat.definitions, function(defn) {
return defn.name instanceof AST_SymbolConst;
})) return false;
return all(stat.definitions, function(defn) {
return !defn.name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) return node.definition().assignments != assigned;
}, true);
});
}
function can_varify(compressor, sym) {
var def = sym.definition();
return (def.fixed || def.fixed === 0)
@@ -10206,15 +10234,23 @@ Compressor.prototype.compress = function(node) {
}
function varify(self, compressor) {
return compressor.option("varify") && all(self.definitions, function(defn) {
return all(self.definitions, function(defn) {
return !defn.name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) return !can_varify(compressor, node);
}, true);
}) ? to_var(self, compressor.find_parent(AST_Scope)) : self;
}
OPT(AST_Const, varify);
OPT(AST_Let, varify);
OPT(AST_Const, function(self, compressor) {
if (!compressor.option("varify")) return self;
if (can_letify(self, compressor, 0)) return to_let(self);
return varify(self, compressor);
});
OPT(AST_Let, function(self, compressor) {
if (!compressor.option("varify")) return self;
return varify(self, compressor);
});
function trim_optional_chain(node, compressor) {
if (!compressor.option("optional_chains")) return;