improve compress (#3814)

- avoid identifier overflow through consecutive API calls
- simplify `reduce_vars`
- enhance `unsafe` `evaluate`
This commit is contained in:
Alex Lam S.L
2020-04-23 23:50:53 +01:00
committed by GitHub
parent 99cf3a38c5
commit 3ee13cae02
3 changed files with 43 additions and 22 deletions

View File

@@ -460,7 +460,7 @@ merge(Compressor.prototype, {
return def.fixed instanceof AST_Defun;
}
function safe_to_assign(tw, def, scope, value) {
function safe_to_assign(tw, def, value) {
if (def.fixed === undefined) return true;
if (def.fixed === null && def.safe_ids) {
def.safe_ids[def.id] = false;
@@ -471,11 +471,8 @@ merge(Compressor.prototype, {
if (!safe_to_read(tw, def)) return false;
if (def.fixed === false) return false;
if (def.fixed != null && (!value || def.references.length > def.assignments)) return false;
if (def.fixed instanceof AST_Defun) {
return value instanceof AST_Node && def.fixed.parent_scope === scope;
}
return all(def.orig, function(sym) {
return !(sym instanceof AST_SymbolDefun || sym instanceof AST_SymbolLambda);
return !(sym instanceof AST_SymbolLambda);
});
}
@@ -557,7 +554,7 @@ merge(Compressor.prototype, {
}
if (sym.fixed) delete sym.fixed;
var d = sym.definition();
var safe = safe_to_assign(tw, d, sym.scope, node.right);
var safe = safe_to_assign(tw, d, node.right);
d.assignments++;
var fixed = d.fixed;
if (!fixed && node.operator != "=") return;
@@ -820,7 +817,7 @@ merge(Compressor.prototype, {
}
if (exp.fixed) delete exp.fixed;
var d = exp.definition();
var safe = safe_to_assign(tw, d, exp.scope, true);
var safe = safe_to_assign(tw, d, true);
d.assignments++;
var fixed = d.fixed;
if (!fixed) return;
@@ -846,7 +843,7 @@ merge(Compressor.prototype, {
var node = this;
var d = node.name.definition();
if (node.value) {
if (safe_to_assign(tw, d, node.name.scope, node.value)) {
if (safe_to_assign(tw, d, node.value)) {
d.fixed = function() {
return node.value;
};
@@ -3195,7 +3192,7 @@ merge(Compressor.prototype, {
def(AST_Statement, function() {
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
});
def(AST_Lambda, return_this);
def(AST_Accessor, return_this);
def(AST_Node, return_this);
def(AST_Constant, function() {
return this.value;
@@ -3213,7 +3210,7 @@ merge(Compressor.prototype, {
var value = node._eval(compressor, ignore_side_effects, cached, depth);
return value === node ? this : value;
});
def(AST_Function, function(compressor) {
def(AST_Lambda, function(compressor) {
if (compressor.option("unsafe")) {
var fn = function() {};
fn.node = this;