enhance varify (#4279)

This commit is contained in:
Alex Lam S.L
2020-11-17 04:35:00 +00:00
committed by GitHub
parent e5f80afc53
commit 6dbacb5e3f
3 changed files with 156 additions and 17 deletions

View File

@@ -6551,6 +6551,19 @@ merge(Compressor.prototype, {
return if_break_in_loop(self, compressor);
});
OPT(AST_ForIn, function(self, compressor) {
if (compressor.option("varify") && (self.init instanceof AST_Const || self.init instanceof AST_Let)) {
var name = self.init.definitions[0].name;
if ((name instanceof AST_Destructured || name instanceof AST_SymbolLet)
&& !name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) return may_overlap(compressor, node.definition());
})) {
self.init = to_var(self.init);
}
}
return self;
});
function mark_locally_defined(condition, consequent, alternative) {
if (!(condition instanceof AST_Binary)) return;
if (!(condition.left instanceof AST_String)) {
@@ -7051,21 +7064,18 @@ merge(Compressor.prototype, {
return make_sequence(this, assignments);
});
function varify(self, compressor) {
return compressor.option("varify") && all(self.definitions, function(defn) {
return !defn.name.match_symbol(function(node) {
if (!(node instanceof AST_SymbolDeclaration)) return;
if (!node.fixed_value()) return true;
var def = node.definition();
if (compressor.exposed(def)) return true;
var scope = def.scope.resolve();
for (var s = def.scope; s !== scope;) {
s = s.parent_scope;
if (s.var_names()[node.name]) return true;
}
});
}) ? make_node(AST_Var, self, {
definitions: self.definitions.map(function(defn) {
function may_overlap(compressor, def) {
if (compressor.exposed(def)) return true;
var scope = def.scope.resolve();
for (var s = def.scope; s !== scope;) {
s = s.parent_scope;
if (s.var_names()[def.name]) return true;
}
}
function to_var(stat) {
return make_node(AST_Var, stat, {
definitions: stat.definitions.map(function(defn) {
return make_node(AST_VarDef, defn, {
name: defn.name.convert_symbol(AST_SymbolVar, function(name, node) {
var def = name.definition();
@@ -7076,7 +7086,17 @@ merge(Compressor.prototype, {
value: defn.value
});
})
}) : self;
});
}
function varify(self, compressor) {
return compressor.option("varify") && all(self.definitions, function(defn) {
return !defn.name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) {
return !node.fixed_value() || may_overlap(compressor, node.definition());
}
});
}) ? to_var(self) : self;
}
OPT(AST_Const, varify);