Merge branch 'master' into harmony
This commit is contained in:
234
lib/compress.js
234
lib/compress.js
@@ -156,7 +156,9 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
var passes = +this.options.passes || 1;
|
||||
var last_count = 1 / 0;
|
||||
var mangle = { ie8: this.option("ie8") };
|
||||
for (var pass = 0; pass < passes; pass++) {
|
||||
node.figure_out_scope(mangle);
|
||||
if (pass > 0 || this.option("reduce_vars"))
|
||||
node.reset_opt_flags(this);
|
||||
node = node.transform(this);
|
||||
@@ -322,7 +324,7 @@ merge(Compressor.prototype, {
|
||||
d.fixed = false;
|
||||
} else if (d.fixed) {
|
||||
var value = node.fixed_value();
|
||||
if (unused && !compressor.exposed(d) && value && d.references.length == 1) {
|
||||
if (value && ref_once(d) && !compressor.exposed(d)) {
|
||||
if (value instanceof AST_Lambda) {
|
||||
d.single_use = d.scope === node.scope
|
||||
&& !(d.orig[0] instanceof AST_SymbolFunarg)
|
||||
@@ -396,7 +398,7 @@ merge(Compressor.prototype, {
|
||||
} else {
|
||||
d.fixed = node;
|
||||
mark(d, true);
|
||||
if (unused && d.references.length == 1) {
|
||||
if (ref_once(d)) {
|
||||
d.single_use = d.scope === d.references[0].scope
|
||||
|| node.is_constant_expression(d.references[0].scope);
|
||||
}
|
||||
@@ -578,7 +580,7 @@ merge(Compressor.prototype, {
|
||||
function reset_def(def) {
|
||||
def.direct_access = false;
|
||||
def.escaped = false;
|
||||
if (def.scope.uses_eval) {
|
||||
if (def.scope.uses_eval || def.scope.uses_with) {
|
||||
def.fixed = false;
|
||||
} else if (def.orig[0] instanceof AST_SymbolConst || !compressor.exposed(def)) {
|
||||
def.fixed = undefined;
|
||||
@@ -590,6 +592,10 @@ merge(Compressor.prototype, {
|
||||
def.single_use = undefined;
|
||||
}
|
||||
|
||||
function ref_once(def) {
|
||||
return unused && !def.scope.uses_eval && !def.scope.uses_with && def.references.length == 1;
|
||||
}
|
||||
|
||||
function is_immutable(value) {
|
||||
if (!value) return false;
|
||||
return value.is_constant()
|
||||
@@ -829,6 +835,14 @@ merge(Compressor.prototype, {
|
||||
|| compressor.option("unsafe") && global_names(this.name);
|
||||
});
|
||||
|
||||
function drop_decl(def) {
|
||||
def._eliminiated = (def._eliminiated || 0) + 1;
|
||||
if (def.orig.length == def._eliminiated) {
|
||||
def.scope.functions.del(def.name);
|
||||
def.scope.variables.del(def.name);
|
||||
}
|
||||
}
|
||||
|
||||
function tighten_body(statements, compressor) {
|
||||
var CHANGED, max_iter = 10;
|
||||
do {
|
||||
@@ -1070,7 +1084,8 @@ merge(Compressor.prototype, {
|
||||
function get_lhs(expr) {
|
||||
if (expr instanceof AST_VarDef && expr.name instanceof AST_SymbolDeclaration) {
|
||||
var def = expr.name.definition();
|
||||
if (def.orig.length > 1 && !(expr.name instanceof AST_SymbolFunarg)
|
||||
if (def.orig.length - (def._eliminiated || 0) > 1
|
||||
&& !(expr.name instanceof AST_SymbolFunarg)
|
||||
|| def.references.length == 1 && !compressor.exposed(def)) {
|
||||
return make_node(AST_SymbolRef, expr.name, expr.name);
|
||||
}
|
||||
@@ -1080,6 +1095,10 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
function get_rvalue(expr) {
|
||||
return expr[expr instanceof AST_Assign ? "right" : "value"];
|
||||
}
|
||||
|
||||
function get_lvalues(expr) {
|
||||
var lvalues = Object.create(null);
|
||||
if (expr instanceof AST_Unary) return lvalues;
|
||||
@@ -1090,7 +1109,7 @@ merge(Compressor.prototype, {
|
||||
lvalues[sym.name] = lvalues[sym.name] || is_lhs(node, tw.parent());
|
||||
}
|
||||
});
|
||||
expr[expr instanceof AST_Assign ? "right" : "value"].walk(tw);
|
||||
get_rvalue(expr).walk(tw);
|
||||
return lvalues;
|
||||
}
|
||||
|
||||
@@ -1119,7 +1138,9 @@ merge(Compressor.prototype, {
|
||||
if (node === expr) {
|
||||
found = true;
|
||||
if (node instanceof AST_VarDef) {
|
||||
remove(node.name.definition().orig, node.name);
|
||||
drop_decl(node.name.definition());
|
||||
node.value = null;
|
||||
return node;
|
||||
}
|
||||
return in_list ? MAP.skip : null;
|
||||
}
|
||||
@@ -1128,16 +1149,13 @@ merge(Compressor.prototype, {
|
||||
case 0: return null;
|
||||
case 1: return node.expressions[0];
|
||||
}
|
||||
if (node instanceof AST_Definitions && node.definitions.length == 0
|
||||
|| node instanceof AST_SimpleStatement && !node.body) {
|
||||
return null;
|
||||
}
|
||||
if (node instanceof AST_SimpleStatement && !node.body) return null;
|
||||
}));
|
||||
}
|
||||
|
||||
function value_has_side_effects(expr) {
|
||||
if (expr instanceof AST_Unary) return false;
|
||||
return expr[expr instanceof AST_Assign ? "right" : "value"].has_side_effects(compressor);
|
||||
return get_rvalue(expr).has_side_effects(compressor);
|
||||
}
|
||||
|
||||
function references_in_scope(def) {
|
||||
@@ -2390,7 +2408,7 @@ merge(Compressor.prototype, {
|
||||
|| can_be_evicted_from_block(self.body[0])) {
|
||||
return self.body[0];
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case 0: return make_node(AST_EmptyStatement, self);
|
||||
}
|
||||
return self;
|
||||
@@ -2426,78 +2444,80 @@ merge(Compressor.prototype, {
|
||||
// this scope (not in nested scopes).
|
||||
var scope = this;
|
||||
var tw = new TreeWalker(function(node, descend){
|
||||
if (node !== self) {
|
||||
if (node instanceof AST_Defun || node instanceof AST_DefClass) {
|
||||
var in_export = tw.parent() instanceof AST_Export;
|
||||
if (in_export || !drop_funcs && scope === self) {
|
||||
var node_def = node.name.definition();
|
||||
if (node_def.global && !(node_def.id in in_use_ids)) {
|
||||
in_use_ids[node_def.id] = true;
|
||||
in_use.push(node_def);
|
||||
}
|
||||
}
|
||||
initializations.add(node.name.name, node);
|
||||
return true; // don't go in nested scopes
|
||||
}
|
||||
if (node instanceof AST_Definitions && scope === self) {
|
||||
var in_export = tw.parent() instanceof AST_Export;
|
||||
node.definitions.forEach(function(def){
|
||||
if (def.name instanceof AST_SymbolVar) {
|
||||
var_defs_by_id.add(def.name.definition().id, def);
|
||||
}
|
||||
if (in_export || !drop_vars) {
|
||||
def.name.walk(new TreeWalker(function(node) {
|
||||
if (node instanceof AST_SymbolDeclaration) {
|
||||
var def = node.definition();
|
||||
if ((in_export || def.global) && !(def.id in in_use_ids)) {
|
||||
in_use_ids[def.id] = true;
|
||||
in_use.push(def);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
if (def.value) {
|
||||
if (def.name instanceof AST_Destructuring) {
|
||||
var destructuring_cache = destructuring_value;
|
||||
destructuring_value = def.value;
|
||||
def.walk(tw);
|
||||
destructuring_value = destructuring_cache;
|
||||
} else {
|
||||
initializations.add(def.name.name, def.value);
|
||||
}
|
||||
if (def.value.has_side_effects(compressor)) {
|
||||
def.value.walk(tw);
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
var sym;
|
||||
if (scope === self
|
||||
&& (sym = assign_as_unused(node)) instanceof AST_SymbolRef
|
||||
&& !is_ref_of(node.left, AST_SymbolBlockDeclaration)
|
||||
&& self.variables.get(sym.name) === sym.definition()) {
|
||||
if (node instanceof AST_Assign) node.right.walk(tw);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
var node_def = node.definition();
|
||||
if (!(node_def.id in in_use_ids)) {
|
||||
if (node === self) return;
|
||||
if (node instanceof AST_Defun || node instanceof AST_DefClass) {
|
||||
var in_export = tw.parent() instanceof AST_Export;
|
||||
if (in_export || !drop_funcs && scope === self) {
|
||||
var node_def = node.name.definition();
|
||||
if (node_def.global && !(node_def.id in in_use_ids)) {
|
||||
in_use_ids[node_def.id] = true;
|
||||
in_use.push(node_def);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Scope) {
|
||||
var save_scope = scope;
|
||||
scope = node;
|
||||
descend();
|
||||
scope = save_scope;
|
||||
return true;
|
||||
}
|
||||
if (node.destructuring && destructuring_value) {
|
||||
initializations.add(node.name, destructuring_value);
|
||||
initializations.add(node.name.name, node);
|
||||
return true; // don't go in nested scopes
|
||||
}
|
||||
if (node instanceof AST_SymbolFunarg && scope === self) {
|
||||
var_defs_by_id.add(node.definition().id, node);
|
||||
}
|
||||
if (node instanceof AST_Definitions && scope === self) {
|
||||
var in_export = tw.parent() instanceof AST_Export;
|
||||
node.definitions.forEach(function(def){
|
||||
if (def.name instanceof AST_SymbolVar) {
|
||||
var_defs_by_id.add(def.name.definition().id, def);
|
||||
}
|
||||
if (in_export || !drop_vars) {
|
||||
def.name.walk(new TreeWalker(function(node) {
|
||||
if (node instanceof AST_SymbolDeclaration) {
|
||||
var def = node.definition();
|
||||
if ((in_export || def.global) && !(def.id in in_use_ids)) {
|
||||
in_use_ids[def.id] = true;
|
||||
in_use.push(def);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
if (def.value) {
|
||||
if (def.name instanceof AST_Destructuring) {
|
||||
var destructuring_cache = destructuring_value;
|
||||
destructuring_value = def.value;
|
||||
def.walk(tw);
|
||||
destructuring_value = destructuring_cache;
|
||||
} else {
|
||||
initializations.add(def.name.name, def.value);
|
||||
}
|
||||
if (def.value.has_side_effects(compressor)) {
|
||||
def.value.walk(tw);
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
var sym;
|
||||
if (scope === self
|
||||
&& (sym = assign_as_unused(node)) instanceof AST_SymbolRef
|
||||
&& !is_ref_of(node.left, AST_SymbolBlockDeclaration)
|
||||
&& self.variables.get(sym.name) === sym.definition()) {
|
||||
if (node instanceof AST_Assign) node.right.walk(tw);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
var node_def = node.definition();
|
||||
if (!(node_def.id in in_use_ids)) {
|
||||
in_use_ids[node_def.id] = true;
|
||||
in_use.push(node_def);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Scope) {
|
||||
var save_scope = scope;
|
||||
scope = node;
|
||||
descend();
|
||||
scope = save_scope;
|
||||
return true;
|
||||
}
|
||||
if (node.destructuring && destructuring_value) {
|
||||
initializations.add(node.name, destructuring_value);
|
||||
}
|
||||
});
|
||||
self.walk(tw);
|
||||
@@ -2566,7 +2586,7 @@ merge(Compressor.prototype, {
|
||||
var keep = (def.id in in_use_ids) || !drop_funcs && def.global;
|
||||
if (!keep) {
|
||||
compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
|
||||
drop_decl(def, node.name);
|
||||
drop_decl(def);
|
||||
return make_node(AST_EmptyStatement, node);
|
||||
}
|
||||
return node;
|
||||
@@ -2591,7 +2611,7 @@ merge(Compressor.prototype, {
|
||||
if (var_defs.length > 1 && !def.value) {
|
||||
compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
|
||||
remove(var_defs, def);
|
||||
drop_decl(sym, def.name);
|
||||
drop_decl(sym);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -2624,7 +2644,7 @@ merge(Compressor.prototype, {
|
||||
} else {
|
||||
compressor[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name));
|
||||
}
|
||||
drop_decl(sym, def.name);
|
||||
drop_decl(sym);
|
||||
}
|
||||
});
|
||||
if (head.length == 0 && tail.length == 1 && tail[0].name instanceof AST_SymbolVar) {
|
||||
@@ -2633,7 +2653,7 @@ merge(Compressor.prototype, {
|
||||
var def = tail.pop();
|
||||
compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", template(def.name));
|
||||
remove(var_defs, def);
|
||||
drop_decl(def.name.definition(), def.name);
|
||||
drop_decl(def.name.definition());
|
||||
side_effects.unshift(make_node(AST_Assign, def, {
|
||||
operator: "=",
|
||||
left: make_node(AST_SymbolRef, def.name, def.name),
|
||||
@@ -2722,14 +2742,6 @@ merge(Compressor.prototype, {
|
||||
col : sym.start.col
|
||||
};
|
||||
}
|
||||
|
||||
function drop_decl(def, decl) {
|
||||
remove(def.orig, decl);
|
||||
if (!def.orig.length) {
|
||||
def.scope.functions.del(def.name);
|
||||
def.scope.variables.del(def.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
self.transform(tt);
|
||||
@@ -3489,7 +3501,7 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
a.push(var_);
|
||||
}
|
||||
remove(def.name.definition().orig, def.name);
|
||||
drop_decl(def.name.definition());
|
||||
return a;
|
||||
}, []);
|
||||
if (assignments.length == 0) return null;
|
||||
@@ -3772,6 +3784,7 @@ merge(Compressor.prototype, {
|
||||
&& !exp.uses_arguments
|
||||
&& !exp.uses_eval
|
||||
&& (exp.body instanceof AST_Node || exp.body.length == 1)
|
||||
&& !exp.contains_this()
|
||||
&& all(exp.argnames, function(arg) {
|
||||
if (arg instanceof AST_Expansion) return arg.expression.__unused;
|
||||
return arg.__unused;
|
||||
@@ -3786,23 +3799,6 @@ merge(Compressor.prototype, {
|
||||
expression: stat.body
|
||||
});
|
||||
}
|
||||
if (value) {
|
||||
var tw = new TreeWalker(function(node) {
|
||||
if (!value) return true;
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
var ref = node.scope.find_variable(node);
|
||||
if (ref && ref.scope.parent_scope === fn.parent_scope) {
|
||||
value = null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (node instanceof AST_This && !tw.find_parent(AST_Scope)) {
|
||||
value = null;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
value.walk(tw);
|
||||
}
|
||||
if (value) {
|
||||
var args = self.args.concat(value);
|
||||
return make_sequence(self, args).optimize(compressor);
|
||||
@@ -4498,12 +4494,20 @@ merge(Compressor.prototype, {
|
||||
if (fixed instanceof AST_Defun) {
|
||||
d.fixed = fixed = make_node(AST_Function, fixed, fixed);
|
||||
}
|
||||
if (compressor.option("unused")
|
||||
&& fixed
|
||||
&& d.references.length == 1
|
||||
&& d.single_use) {
|
||||
var value = fixed.optimize(compressor);
|
||||
return value === fixed ? fixed.clone(true) : value;
|
||||
if (fixed && d.single_use) {
|
||||
var recurse;
|
||||
if (fixed instanceof AST_Function) {
|
||||
for (var i = 0; recurse = compressor.parent(i); i++) {
|
||||
if (recurse instanceof AST_Lambda) {
|
||||
var name = recurse.name;
|
||||
if (name && name.definition() === d) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!recurse) {
|
||||
var value = fixed.optimize(compressor);
|
||||
return value === fixed ? fixed.clone(true) : value;
|
||||
}
|
||||
}
|
||||
if (fixed && d.should_replace === undefined) {
|
||||
var init;
|
||||
|
||||
Reference in New Issue
Block a user