return make_node(AST_Infinity, self).optimize(compressor);
}
}
- if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
+ if (compressor.option("evaluate")
+ && compressor.option("reduce_vars")
+ && is_lhs(self, compressor.parent()) !== self) {
var d = self.definition();
var fixed = self.fixed_value();
if (fixed) {
var init = fixed.evaluate(compressor);
if (init !== fixed) {
init = make_node_from_constant(init, fixed);
- var value = best_of_expression(init.optimize(compressor), fixed).print_to_string().length;
+ var value = init.optimize(compressor).print_to_string().length;
+ var fn;
+ if (has_symbol_ref(fixed)) {
+ fn = function() {
+ var result = init.optimize(compressor);
+ return result === init ? result.clone(true) : result;
+ };
+ } else {
+ value = Math.min(value, fixed.print_to_string().length);
+ fn = function() {
+ var result = best_of_expression(init.optimize(compressor), fixed);
+ return result === init || result === fixed ? result.clone(true) : result;
+ };
+ }
var name = d.name.length;
- var freq = d.references.length;
- var overhead = d.global || !freq ? 0 : (name + 2 + value) / freq;
- d.should_replace = value <= name + overhead ? init : false;
+ var overhead = 0;
+ if (compressor.option("unused") && (!d.global || compressor.option("toplevel"))) {
+ overhead = (name + 2 + value) / d.references.length;
+ }
+ d.should_replace = value <= name + overhead ? fn : false;
} else {
d.should_replace = false;
}
}
if (d.should_replace) {
- return best_of_expression(d.should_replace.optimize(compressor), fixed).clone(true);
+ return d.should_replace();
}
}
}
return self;
+
+ function has_symbol_ref(value) {
+ var found;
+ value.walk(new TreeWalker(function(node) {
+ if (node instanceof AST_SymbolRef) found = true;
+ if (found) return true;
+ }));
+ return found;
+ }
});
function is_atomic(lhs, self) {
console.log(a, b);
}
expect_exact: [
- "var a = 100, b = 10;",
+ "var b = 10;",
"",
"!function() {",
- " for (;b = a, !1; ) ;",
- "}(), console.log(a, b);",
+ " for (;b = 100, !1; ) ;",
+ "}(), console.log(100, b);",
]
expect_stdout: true
}