enhance reduce_vars (#1814)
- allow immediate assignment after declaration of variable - relax modification rule for immutable value - fix order of visit for TreeWalker - remove extraneous code
This commit is contained in:
@@ -119,7 +119,7 @@ merge(Compressor.prototype, {
|
||||
option: function(key) { return this.options[key] },
|
||||
compress: function(node) {
|
||||
if (this.option("expression")) {
|
||||
node = node.process_expression(true);
|
||||
node.process_expression(true);
|
||||
}
|
||||
var passes = +this.options.passes || 1;
|
||||
for (var pass = 0; pass < passes && pass < 3; ++pass) {
|
||||
@@ -128,7 +128,7 @@ merge(Compressor.prototype, {
|
||||
node = node.transform(this);
|
||||
}
|
||||
if (this.option("expression")) {
|
||||
node = node.process_expression(false);
|
||||
node.process_expression(false);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
@@ -200,7 +200,7 @@ merge(Compressor.prototype, {
|
||||
return this.TYPE == node.TYPE && this.print_to_string() == node.print_to_string();
|
||||
});
|
||||
|
||||
AST_Node.DEFMETHOD("process_expression", function(insert, compressor) {
|
||||
AST_Scope.DEFMETHOD("process_expression", function(insert, compressor) {
|
||||
var self = this;
|
||||
var tt = new TreeTransformer(function(node) {
|
||||
if (insert && node instanceof AST_SimpleStatement) {
|
||||
@@ -244,10 +244,10 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
return node;
|
||||
});
|
||||
return self.transform(tt);
|
||||
self.transform(tt);
|
||||
});
|
||||
|
||||
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan){
|
||||
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan) {
|
||||
var reduce_vars = rescan && compressor.option("reduce_vars");
|
||||
var toplevel = compressor.option("toplevel");
|
||||
var safe_ids = Object.create(null);
|
||||
@@ -258,7 +258,7 @@ merge(Compressor.prototype, {
|
||||
d.fixed = false;
|
||||
}
|
||||
});
|
||||
var tw = new TreeWalker(function(node, descend){
|
||||
var tw = new TreeWalker(function(node, descend) {
|
||||
node._squeezed = false;
|
||||
node._optimized = false;
|
||||
if (reduce_vars) {
|
||||
@@ -268,7 +268,7 @@ merge(Compressor.prototype, {
|
||||
var d = node.definition();
|
||||
d.references.push(node);
|
||||
if (d.fixed === undefined || !is_safe(d)
|
||||
|| is_modified(node, 0, node.fixed_value() instanceof AST_Lambda)) {
|
||||
|| is_modified(node, 0, is_immutable(node.fixed_value()))) {
|
||||
d.fixed = false;
|
||||
}
|
||||
}
|
||||
@@ -293,6 +293,20 @@ merge(Compressor.prototype, {
|
||||
d.fixed = false;
|
||||
}
|
||||
}
|
||||
if (node instanceof AST_Assign
|
||||
&& node.operator == "="
|
||||
&& node.left instanceof AST_SymbolRef) {
|
||||
var d = node.left.definition();
|
||||
if (HOP(safe_ids, d.id) && d.fixed == null) {
|
||||
d.fixed = function() {
|
||||
return node.right;
|
||||
};
|
||||
mark(d, false);
|
||||
node.right.walk(tw);
|
||||
mark(d, true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (node instanceof AST_Defun) {
|
||||
var d = node.name.definition();
|
||||
if (!toplevel && d.global || is_safe(d)) {
|
||||
@@ -309,21 +323,24 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
var iife;
|
||||
if (node instanceof AST_Function
|
||||
&& !node.name
|
||||
&& (iife = tw.parent()) instanceof AST_Call
|
||||
&& iife.expression === node) {
|
||||
// Virtually turn IIFE parameters into variable definitions:
|
||||
// (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
|
||||
// So existing transformation rules can work on them.
|
||||
node.argnames.forEach(function(arg, i) {
|
||||
var d = arg.definition();
|
||||
d.fixed = function() {
|
||||
return iife.args[i] || make_node(AST_Undefined, iife);
|
||||
};
|
||||
mark(d, true);
|
||||
});
|
||||
if (node.name) {
|
||||
node.name.definition().fixed = node;
|
||||
} else {
|
||||
// Virtually turn IIFE parameters into variable definitions:
|
||||
// (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
|
||||
// So existing transformation rules can work on them.
|
||||
node.argnames.forEach(function(arg, i) {
|
||||
var d = arg.definition();
|
||||
d.fixed = function() {
|
||||
return iife.args[i] || make_node(AST_Undefined, iife);
|
||||
};
|
||||
mark(d, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
if (node instanceof AST_If || node instanceof AST_DWLoop) {
|
||||
if (node instanceof AST_If) {
|
||||
node.condition.walk(tw);
|
||||
push();
|
||||
node.body.walk(tw);
|
||||
@@ -335,6 +352,13 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_DWLoop) {
|
||||
push();
|
||||
node.condition.walk(tw);
|
||||
node.body.walk(tw);
|
||||
pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_LabeledStatement) {
|
||||
push();
|
||||
node.body.walk(tw);
|
||||
@@ -401,13 +425,17 @@ merge(Compressor.prototype, {
|
||||
def.should_replace = undefined;
|
||||
}
|
||||
|
||||
function is_modified(node, level, func) {
|
||||
function is_immutable(value) {
|
||||
return value && value.is_constant() || value instanceof AST_Lambda;
|
||||
}
|
||||
|
||||
function is_modified(node, level, immutable) {
|
||||
var parent = tw.parent(level);
|
||||
if (is_lhs(node, parent)
|
||||
|| !func && parent instanceof AST_Call && parent.expression === node) {
|
||||
|| !immutable && parent instanceof AST_Call && parent.expression === node) {
|
||||
return true;
|
||||
} else if (parent instanceof AST_PropAccess && parent.expression === node) {
|
||||
return !func && is_modified(parent, level + 1);
|
||||
return !immutable && is_modified(parent, level + 1);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -2167,7 +2195,7 @@ merge(Compressor.prototype, {
|
||||
if (this.expression instanceof AST_Function
|
||||
&& (!this.expression.name || !this.expression.name.definition().references.length)) {
|
||||
var node = this.clone();
|
||||
node.expression = node.expression.process_expression(false, compressor);
|
||||
node.expression.process_expression(false, compressor);
|
||||
return node;
|
||||
}
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user