Merge branch 'master' into harmony-v2.8.13

This commit is contained in:
alexlamsl
2017-03-18 02:52:45 +08:00
19 changed files with 689 additions and 246 deletions

View File

@@ -99,10 +99,10 @@ function Compressor(options, false_by_default) {
this.top_retain = function(def) {
return top_retain.test(def.name);
};
} else if (typeof top_retain === "function") {
} else if (typeof top_retain == "function") {
this.top_retain = top_retain;
} else if (top_retain) {
if (typeof top_retain === "string") {
if (typeof top_retain == "string") {
top_retain = top_retain.split(/,/);
}
this.top_retain = function(def) {
@@ -152,14 +152,25 @@ merge(Compressor.prototype, {
node = node.hoist_declarations(this);
was_scope = true;
}
// Before https://github.com/mishoo/UglifyJS2/pull/1602 AST_Node.optimize()
// would call AST_Node.transform() if a different instance of AST_Node is
// produced after OPT().
// This corrupts TreeWalker.stack, which cause AST look-ups to malfunction.
// Migrate and defer all children's AST_Node.transform() to below, which
// will now happen after this parent AST_Node has been properly substituted
// thus gives a consistent AST snapshot.
descend(node, this);
node = node.optimize(this);
if (was_scope && node instanceof AST_Scope) {
node.drop_unused(this);
descend(node, this);
// Existing code relies on how AST_Node.optimize() worked, and omitting the
// following replacement call would result in degraded efficiency of both
// output and performance.
descend(node, this);
var opt = node.optimize(this);
if (was_scope && opt instanceof AST_Scope) {
opt.drop_unused(this);
descend(opt, this);
}
node._squeezed = true;
return node;
if (opt === node) opt._squeezed = true;
return opt;
}
});
@@ -172,8 +183,7 @@ merge(Compressor.prototype, {
if (compressor.has_directive("use asm")) return self;
var opt = optimizer(self, compressor);
opt._optimized = true;
if (opt === self) return opt;
return opt.transform(compressor);
return opt;
});
};
@@ -389,7 +399,7 @@ merge(Compressor.prototype, {
return new ctor(props);
};
function make_node_from_constant(compressor, val, orig) {
function make_node_from_constant(val, orig) {
switch (typeof val) {
case "string":
return make_node(AST_String, orig, {
@@ -409,9 +419,9 @@ merge(Compressor.prototype, {
return make_node(AST_Number, orig, { value: val });
case "boolean":
return make_node(val ? AST_True : AST_False, orig).optimize(compressor);
return make_node(val ? AST_True : AST_False, orig);
case "undefined":
return make_node(AST_Undefined, orig).transform(compressor);
return make_node(AST_Undefined, orig);
default:
if (val === null) {
return make_node(AST_Null, orig, { value: null });
@@ -524,6 +534,7 @@ merge(Compressor.prototype, {
var self = compressor.self();
var var_defs_removed = false;
var toplevel = compressor.option("toplevel");
for (var stat_index = statements.length; --stat_index >= 0;) {
var stat = statements[stat_index];
if (stat instanceof AST_Definitions) continue;
@@ -561,7 +572,8 @@ merge(Compressor.prototype, {
// Only interested in cases with just one reference to the variable.
var def = self.find_variable && self.find_variable(var_name);
if (!def || !def.references || def.references.length !== 1 || var_name == "arguments") {
if (!def || !def.references || def.references.length !== 1
|| var_name == "arguments" || (!toplevel && def.global)) {
side_effects_encountered = true;
continue;
}
@@ -937,7 +949,7 @@ merge(Compressor.prototype, {
if (stat instanceof AST_LoopControl) {
var lct = compressor.loopcontrol_target(stat.label);
if ((stat instanceof AST_Break
&& lct instanceof AST_BlockStatement
&& !(lct instanceof AST_IterationStatement)
&& loop_body(lct) === self) || (stat instanceof AST_Continue
&& loop_body(lct) === self)) {
if (stat.label) {
@@ -1224,11 +1236,11 @@ merge(Compressor.prototype, {
}
}
});
function to_node(compressor, value, orig) {
function to_node(value, orig) {
if (value instanceof AST_Node) return make_node(value.CTOR, orig, value);
if (Array.isArray(value)) return make_node(AST_Array, orig, {
elements: value.map(function(value) {
return to_node(compressor, value, orig);
return to_node(value, orig);
})
});
if (value && typeof value == "object") {
@@ -1236,14 +1248,14 @@ merge(Compressor.prototype, {
for (var key in value) {
props.push(make_node(AST_ObjectKeyVal, orig, {
key: key,
value: to_node(compressor, value[key], orig)
value: to_node(value[key], orig)
}));
}
return make_node(AST_Object, orig, {
properties: props
});
}
return make_node_from_constant(compressor, value, orig);
return make_node_from_constant(value, orig);
}
def(AST_Node, noop);
def(AST_Dot, function(compressor, suffix){
@@ -1254,7 +1266,7 @@ merge(Compressor.prototype, {
var name;
var defines = compressor.option("global_defs");
if (defines && HOP(defines, (name = this.name + suffix))) {
var node = to_node(compressor, defines[name], this);
var node = to_node(defines[name], this);
var top = compressor.find_parent(AST_Toplevel);
node.walk(new TreeWalker(function(node) {
if (node instanceof AST_SymbolRef) {
@@ -1269,45 +1281,40 @@ merge(Compressor.prototype, {
node.DEFMETHOD("_find_defs", func);
});
function best_of(ast1, ast2) {
function best_of_expression(ast1, ast2) {
return ast1.print_to_string().length >
ast2.print_to_string().length
? ast2 : ast1;
}
function best_of_statement(ast1, ast2) {
return best_of(make_node(AST_SimpleStatement, ast1, {
return best_of_expression(make_node(AST_SimpleStatement, ast1, {
body: ast1
}), make_node(AST_SimpleStatement, ast2, {
body: ast2
})).body;
}
function best_of(compressor, ast1, ast2) {
return (first_in_statement(compressor) ? best_of_statement : best_of_expression)(ast1, ast2);
}
// methods to evaluate a constant expression
(function (def){
// The evaluate method returns an array with one or two
// elements. If the node has been successfully reduced to a
// constant, then the second element tells us the value;
// otherwise the second element is missing. The first element
// of the array is always an AST_Node descendant; if
// evaluation was successful it's a node that represents the
// constant; otherwise it's the original or a replacement node.
// If the node has been successfully reduced to a constant,
// then its value is returned; otherwise the element itself
// is returned.
// They can be distinguished as constant value is never a
// descendant of AST_Node.
AST_Node.DEFMETHOD("evaluate", function(compressor){
if (!compressor.option("evaluate")) return [ this ];
var val;
if (!compressor.option("evaluate")) return this;
try {
val = this._eval(compressor);
var val = this._eval(compressor);
return !val || val instanceof RegExp || typeof val != "object" ? val : this;
} catch(ex) {
if (ex !== def) throw ex;
return [ this ];
return this;
}
var node;
try {
node = make_node_from_constant(compressor, val, this);
} catch(ex) {
return [ this ];
}
return [ best_of(node, this), val ];
});
var unaryPrefix = makePredicate("! ~ - +");
AST_Node.DEFMETHOD("is_constant", function(){
@@ -1345,8 +1352,8 @@ merge(Compressor.prototype, {
}));
}
var result = this.evaluate(compressor);
if (result.length > 1) {
return result[1];
if (result !== this) {
return result;
}
throw new Error(string_template("Cannot evaluate constant [{file}:{line},{col}]", this.start));
});
@@ -1518,9 +1525,9 @@ merge(Compressor.prototype, {
var stat = make_node(AST_SimpleStatement, alt, {
body: alt
});
return best_of(negated, stat) === stat ? alt : negated;
return best_of_expression(negated, stat) === stat ? alt : negated;
}
return best_of(negated, alt);
return best_of_expression(negated, alt);
}
def(AST_Node, function(){
return basic_negation(this);
@@ -1687,8 +1694,8 @@ merge(Compressor.prototype, {
return thing && thing.aborts();
};
(function(def){
def(AST_Statement, function(){ return null });
def(AST_Jump, function(){ return this });
def(AST_Statement, return_null);
def(AST_Jump, return_this);
function block_aborts(){
var n = this.body.length;
return n > 0 && aborts(this.body[n - 1]);
@@ -1706,7 +1713,7 @@ merge(Compressor.prototype, {
/* -----[ optimizers ]----- */
OPT(AST_Directive, function(self, compressor){
if (compressor.has_directive(self.value) === "up") {
if (compressor.has_directive(self.value) !== self) {
return make_node(AST_EmptyStatement, self);
}
return self;
@@ -2102,7 +2109,7 @@ merge(Compressor.prototype, {
vars.set(def.name.name, def);
++vars_found;
});
var seq = node.to_assignments();
var seq = node.to_assignments(compressor);
var p = tt.parent();
if (p instanceof AST_ForIn && p.init === node) {
if (seq == null) {
@@ -2198,14 +2205,6 @@ merge(Compressor.prototype, {
// drop_side_effect_free()
// remove side-effect-free parts which only affects return value
(function(def){
function return_this() {
return this;
}
function return_null() {
return null;
}
// Drop side-effect-free elements from an array of expressions.
// Returns an array of expressions with side-effects or null
// if all elements were dropped. Note: original array may be
@@ -2362,27 +2361,24 @@ merge(Compressor.prototype, {
});
OPT(AST_DWLoop, function(self, compressor){
var cond = self.condition.evaluate(compressor);
self.condition = cond[0];
if (!compressor.option("loops")) return self;
if (cond.length > 1) {
if (cond[1]) {
var cond = self.condition.evaluate(compressor);
if (cond !== self.condition) {
if (cond) {
return make_node(AST_For, self, {
body: self.body
});
} else if (self instanceof AST_While) {
if (compressor.option("dead_code")) {
var a = [];
extract_declarations_from_unreachable_code(compressor, self.body, a);
return make_node(AST_BlockStatement, self, { body: a });
}
} else if (compressor.option("dead_code") && self instanceof AST_While) {
var a = [];
extract_declarations_from_unreachable_code(compressor, self.body, a);
return make_node(AST_BlockStatement, self, { body: a });
} else {
// self instanceof AST_Do
return self;
cond = make_node_from_constant(cond, self.condition).transform(compressor);
self.condition = best_of_expression(cond, self.condition);
}
}
if (self instanceof AST_While) {
return make_node(AST_For, self, self).transform(compressor);
return make_node(AST_For, self, self).optimize(compressor);
}
return self;
});
@@ -2404,7 +2400,7 @@ merge(Compressor.prototype, {
var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body;
if (first instanceof AST_If) {
if (first.body instanceof AST_Break
&& compressor.loopcontrol_target(first.body.label) === self) {
&& compressor.loopcontrol_target(first.body.label) === compressor.self()) {
if (self.condition) {
self.condition = make_node(AST_Binary, self.condition, {
left: self.condition,
@@ -2417,7 +2413,7 @@ merge(Compressor.prototype, {
drop_it(first.alternative);
}
else if (first.alternative instanceof AST_Break
&& compressor.loopcontrol_target(first.alternative.label) === self) {
&& compressor.loopcontrol_target(first.alternative.label) === compressor.self()) {
if (self.condition) {
self.condition = make_node(AST_Binary, self.condition, {
left: self.condition,
@@ -2433,27 +2429,25 @@ merge(Compressor.prototype, {
};
OPT(AST_For, function(self, compressor){
var cond = self.condition;
if (cond) {
cond = cond.evaluate(compressor);
self.condition = cond[0];
}
if (!compressor.option("loops")) return self;
if (cond) {
if (cond.length > 1 && !cond[1]) {
if (compressor.option("dead_code")) {
var a = [];
if (self.init instanceof AST_Statement) {
a.push(self.init);
}
else if (self.init) {
a.push(make_node(AST_SimpleStatement, self.init, {
body: self.init
}));
}
extract_declarations_from_unreachable_code(compressor, self.body, a);
return make_node(AST_BlockStatement, self, { body: a });
if (self.condition) {
var cond = self.condition.evaluate(compressor);
if (compressor.option("dead_code") && !cond) {
var a = [];
if (self.init instanceof AST_Statement) {
a.push(self.init);
}
else if (self.init) {
a.push(make_node(AST_SimpleStatement, self.init, {
body: self.init
}));
}
extract_declarations_from_unreachable_code(compressor, self.body, a);
return make_node(AST_BlockStatement, self, { body: a });
}
if (cond !== self.condition) {
cond = make_node_from_constant(cond, self.condition).transform(compressor);
self.condition = best_of_expression(cond, self.condition);
}
}
if_break_in_loop(self, compressor);
@@ -2469,9 +2463,8 @@ merge(Compressor.prototype, {
// “has no side effects”; also it doesn't work for cases like
// `x && true`, though it probably should.
var cond = self.condition.evaluate(compressor);
self.condition = cond[0];
if (cond.length > 1) {
if (cond[1]) {
if (cond !== self.condition) {
if (cond) {
compressor.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
if (compressor.option("dead_code")) {
var a = [];
@@ -2479,7 +2472,7 @@ merge(Compressor.prototype, {
extract_declarations_from_unreachable_code(compressor, self.alternative, a);
}
a.push(self.body);
return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);
return make_node(AST_BlockStatement, self, { body: a }).optimize(compressor);
}
} else {
compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
@@ -2487,9 +2480,11 @@ merge(Compressor.prototype, {
var a = [];
extract_declarations_from_unreachable_code(compressor, self.body, a);
if (self.alternative) a.push(self.alternative);
return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);
return make_node(AST_BlockStatement, self, { body: a }).optimize(compressor);
}
}
cond = make_node_from_constant(cond, self.condition).transform(compressor);
self.condition = best_of_expression(cond, self.condition);
}
var negated = self.condition.negate(compressor);
var self_condition_length = self.condition.print_to_string().length;
@@ -2506,8 +2501,8 @@ merge(Compressor.prototype, {
}
if (is_empty(self.body) && is_empty(self.alternative)) {
return make_node(AST_SimpleStatement, self.condition, {
body: self.condition
}).transform(compressor);
body: self.condition.clone()
}).optimize(compressor);
}
if (self.body instanceof AST_SimpleStatement
&& self.alternative instanceof AST_SimpleStatement) {
@@ -2517,7 +2512,7 @@ merge(Compressor.prototype, {
consequent : statement_to_expression(self.body),
alternative : statement_to_expression(self.alternative)
})
}).transform(compressor);
}).optimize(compressor);
}
if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) {
if (self_condition_length === negated_length && !negated_is_best
@@ -2533,14 +2528,14 @@ merge(Compressor.prototype, {
left : negated,
right : statement_to_expression(self.body)
})
}).transform(compressor);
}).optimize(compressor);
return make_node(AST_SimpleStatement, self, {
body: make_node(AST_Binary, self, {
operator : "&&",
left : self.condition,
right : statement_to_expression(self.body)
})
}).transform(compressor);
}).optimize(compressor);
}
if (self.body instanceof AST_EmptyStatement
&& self.alternative
@@ -2551,7 +2546,7 @@ merge(Compressor.prototype, {
left : self.condition,
right : statement_to_expression(self.alternative)
})
}).transform(compressor);
}).optimize(compressor);
}
if (self.body instanceof AST_Exit
&& self.alternative instanceof AST_Exit
@@ -2561,18 +2556,21 @@ merge(Compressor.prototype, {
condition : self.condition,
consequent : self.body.value || make_node(AST_Undefined, self.body),
alternative : self.alternative.value || make_node(AST_Undefined, self.alternative)
})
}).transform(compressor);
}).transform(compressor)
}).optimize(compressor);
}
if (self.body instanceof AST_If
&& !self.body.alternative
&& !self.alternative) {
self.condition = make_node(AST_Binary, self.condition, {
operator: "&&",
left: self.condition,
right: self.body.condition
}).transform(compressor);
self.body = self.body.body;
self = make_node(AST_If, self, {
condition: make_node(AST_Binary, self.condition, {
operator: "&&",
left: self.condition,
right: self.body.condition
}),
body: self.body.body,
alternative: null
});
}
if (aborts(self.body)) {
if (self.alternative) {
@@ -2580,7 +2578,7 @@ merge(Compressor.prototype, {
self.alternative = null;
return make_node(AST_BlockStatement, self, {
body: [ self, alt ]
}).transform(compressor);
}).optimize(compressor);
}
}
if (aborts(self.alternative)) {
@@ -2590,7 +2588,7 @@ merge(Compressor.prototype, {
self.alternative = null;
return make_node(AST_BlockStatement, self, {
body: [ self, body ]
}).transform(compressor);
}).optimize(compressor);
}
return self;
});
@@ -2614,12 +2612,12 @@ merge(Compressor.prototype, {
}
break;
}
var exp = self.expression.evaluate(compressor);
out: if (exp.length == 2) try {
var value = self.expression.evaluate(compressor);
out: if (value !== self.expression) try {
// constant expression
self.expression = exp[0];
var expression = make_node_from_constant(value, self.expression);
self.expression = best_of_expression(expression, self.expression);
if (!compressor.option("dead_code")) break out;
var value = exp[1];
var in_if = false;
var in_block = false;
var started = false;
@@ -2666,11 +2664,11 @@ merge(Compressor.prototype, {
if (stopped) return MAP.skip;
if (node instanceof AST_Case) {
var exp = node.expression.evaluate(compressor);
if (exp.length < 2) {
if (exp === node.expression) {
// got a case with non-constant expression, baling out
throw self;
}
if (exp[1] === value || started) {
if (exp === value || started) {
started = true;
if (aborts(node)) stopped = true;
descend(node, this);
@@ -2704,7 +2702,8 @@ merge(Compressor.prototype, {
this.definitions.forEach(function(def){ def.value = null });
});
AST_Definitions.DEFMETHOD("to_assignments", function(){
AST_Definitions.DEFMETHOD("to_assignments", function(compressor){
var reduce_vars = compressor.option("reduce_vars");
var assignments = this.definitions.reduce(function(a, def){
if (def.value && !(def.name instanceof AST_Destructuring)) {
var name = make_node(AST_SymbolRef, def.name, def.name);
@@ -2713,6 +2712,7 @@ merge(Compressor.prototype, {
left : name,
right : def.value
}));
if (reduce_vars) name.definition().fixed = false;
} else if (def.value) {
// Because it's a destructuring, do not turn into an assignment.
var varDef = make_node(AST_VarDef, def, {
@@ -2903,15 +2903,14 @@ merge(Compressor.prototype, {
var separator;
if (self.args.length > 0) {
separator = self.args[0].evaluate(compressor);
if (separator.length < 2) break EXIT; // not a constant
separator = separator[1];
if (separator === self.args[0]) break EXIT; // not a constant
}
var elements = [];
var consts = [];
exp.expression.elements.forEach(function(el) {
el = el.evaluate(compressor);
if (el.length > 1) {
consts.push(el[1]);
var value = el.evaluate(compressor);
if (value !== el) {
consts.push(value);
} else {
if (consts.length > 0) {
elements.push(make_node(AST_String, self, {
@@ -2919,7 +2918,7 @@ merge(Compressor.prototype, {
}));
consts.length = 0;
}
elements.push(el[0]);
elements.push(el);
}
});
if (consts.length > 0) {
@@ -2960,7 +2959,7 @@ merge(Compressor.prototype, {
node.expression = node.expression.clone();
node.expression.expression = node.expression.expression.clone();
node.expression.expression.elements = elements;
return best_of(self, node);
return best_of(compressor, self, node);
}
}
if (exp instanceof AST_Function && !self.expression.is_generator) {
@@ -3106,8 +3105,7 @@ merge(Compressor.prototype, {
return e.expression;
}
if (e instanceof AST_Binary) {
var statement = first_in_statement(compressor);
self = (statement ? best_of_statement : best_of)(self, e.negate(compressor, statement));
self = best_of(compressor, self, e.negate(compressor, first_in_statement(compressor)));
}
break;
case "typeof":
@@ -3120,7 +3118,15 @@ merge(Compressor.prototype, {
}).optimize(compressor);
}
}
return self.evaluate(compressor)[0];
// avoids infinite recursion of numerals
if (self.operator != "-" || !(self.expression instanceof AST_Number)) {
var ev = self.evaluate(compressor);
if (ev !== self) {
ev = make_node_from_constant(ev, self).optimize(compressor);
return best_of(compressor, ev, self);
}
}
return self;
});
function has_side_effects_or_prop_access(node, compressor) {
@@ -3158,16 +3164,6 @@ merge(Compressor.prototype, {
var commutativeOperators = makePredicate("== === != !== * & | ^");
OPT(AST_Binary, function(self, compressor){
var lhs = self.left.evaluate(compressor);
var rhs = self.right.evaluate(compressor);
if (lhs.length > 1 && lhs[0].is_constant() !== self.left.is_constant()
|| rhs.length > 1 && rhs[0].is_constant() !== self.right.is_constant()) {
return make_node(AST_Binary, self, {
operator: self.operator,
left: lhs[0],
right: rhs[0]
}).optimize(compressor);
}
function reversible() {
return self.left instanceof AST_Constant
|| self.right instanceof AST_Constant
@@ -3252,48 +3248,48 @@ merge(Compressor.prototype, {
case "&&":
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) {
if (!ll || !rr) {
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
return make_node(AST_Seq, self, {
car: self.left,
cdr: make_node(AST_False, self)
}).optimize(compressor);
}
if (ll.length > 1 && ll[1]) {
return rr[0];
if (ll !== self.left && ll) {
return self.right.optimize(compressor);
}
if (rr.length > 1 && rr[1]) {
return ll[0];
if (rr !== self.right && rr) {
return self.left.optimize(compressor);
}
break;
case "||":
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) {
if (ll !== self.left && ll || rr !== self.right && rr) {
compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
return make_node(AST_Seq, self, {
car: self.left,
cdr: make_node(AST_True, self)
}).optimize(compressor);
}
if (ll.length > 1 && !ll[1]) {
return rr[0];
if (!ll) {
return self.right.optimize(compressor);
}
if (rr.length > 1 && !rr[1]) {
return ll[0];
if (!rr) {
return self.left.optimize(compressor);
}
break;
case "+":
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if (ll.length > 1 && ll[0] instanceof AST_String && ll[1]) {
if (ll && typeof ll == "string") {
compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
return make_node(AST_Seq, self, {
car: self.right,
cdr: make_node(AST_True, self)
}).optimize(compressor);
}
if (rr.length > 1 && rr[0] instanceof AST_String && rr[1]) {
if (rr && typeof rr == "string") {
compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
return make_node(AST_Seq, self, {
car: self.left,
@@ -3305,12 +3301,11 @@ merge(Compressor.prototype, {
if (compressor.option("comparisons") && self.is_boolean()) {
if (!(compressor.parent() instanceof AST_Binary)
|| compressor.parent() instanceof AST_Assign) {
var statement = first_in_statement(compressor);
var negated = make_node(AST_UnaryPrefix, self, {
operator: "!",
expression: self.negate(compressor, statement)
expression: self.negate(compressor, first_in_statement(compressor))
});
self = (statement ? best_of_statement : best_of)(self, negated);
self = best_of(compressor, self, negated);
}
if (compressor.option("unsafe_comps")) {
switch (self.operator) {
@@ -3462,9 +3457,9 @@ merge(Compressor.prototype, {
});
if (self.right instanceof AST_Constant
&& !(self.left instanceof AST_Constant)) {
self = best_of(reversed, self);
self = best_of(compressor, reversed, self);
} else {
self = best_of(self, reversed);
self = best_of(compressor, self, reversed);
}
}
if (associative && self.is_number(compressor)) {
@@ -3561,7 +3556,12 @@ merge(Compressor.prototype, {
self.right = self.right.right;
return self.transform(compressor);
}
return self.evaluate(compressor)[0];
var ev = self.evaluate(compressor);
if (ev !== self) {
ev = make_node_from_constant(ev, self).optimize(compressor);
return best_of(compressor, ev, self);
}
return self;
});
OPT(AST_SymbolRef, function(self, compressor){
@@ -3576,11 +3576,11 @@ merge(Compressor.prototype, {
&& (!self.scope.uses_with || !compressor.find_parent(AST_With))) {
switch (self.name) {
case "undefined":
return make_node(AST_Undefined, self).transform(compressor);
return make_node(AST_Undefined, self).optimize(compressor);
case "NaN":
return make_node(AST_NaN, self).transform(compressor);
return make_node(AST_NaN, self).optimize(compressor);
case "Infinity":
return make_node(AST_Infinity, self).transform(compressor);
return make_node(AST_Infinity, self).optimize(compressor);
}
}
if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
@@ -3588,18 +3588,20 @@ merge(Compressor.prototype, {
if (d.fixed) {
if (d.should_replace === undefined) {
var init = d.fixed.evaluate(compressor);
if (init.length > 1) {
var value = init[0].print_to_string().length;
if (init !== d.fixed) {
init = make_node_from_constant(init, d.fixed).optimize(compressor);
init = best_of_expression(init, d.fixed);
var value = init.print_to_string().length;
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[0] : false;
d.should_replace = value <= name + overhead ? init : false;
} else {
d.should_replace = false;
}
}
if (d.should_replace) {
return d.should_replace;
return d.should_replace.clone(true);
}
}
}
@@ -3663,10 +3665,11 @@ merge(Compressor.prototype, {
var evaluateRight = self.right.evaluate(compressor);
// `[x = undefined] = foo` ---> `[x] = foo`
if (evaluateRight.length > 1 && evaluateRight[1] === undefined) {
if (evaluateRight === undefined) {
self = self.left;
} else {
self.right = evaluateRight[0];
} else if (evaluateRight !== self.right) {
evaluateRight = make_node_from_constant(evaluateRight, self.right);
self.right = best_of_expression(evaluateRight, self.right);
}
return self;
@@ -3680,8 +3683,8 @@ merge(Compressor.prototype, {
return AST_Seq.cons(car, self);
}
var cond = self.condition.evaluate(compressor);
if (cond.length > 1) {
if (cond[1]) {
if (cond !== self.condition) {
if (cond) {
compressor.warn("Condition always true [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), self, self.consequent);
} else {
@@ -3689,9 +3692,8 @@ merge(Compressor.prototype, {
return maintain_this_binding(compressor.parent(), self, self.alternative);
}
}
var statement = first_in_statement(compressor);
var negated = cond[0].negate(compressor, statement);
if ((statement ? best_of_statement : best_of)(cond[0], negated) === negated) {
var negated = cond.negate(compressor, first_in_statement(compressor));
if (best_of(compressor, cond, negated) === negated) {
self = make_node(AST_Conditional, self, {
condition: negated,
consequent: self.alternative,
@@ -3871,7 +3873,12 @@ merge(Compressor.prototype, {
});
}
}
return self.evaluate(compressor)[0];
var ev = self.evaluate(compressor);
if (ev !== self) {
ev = make_node_from_constant(ev, self).optimize(compressor);
return best_of(compressor, ev, self);
}
return self;
});
OPT(AST_Dot, function(self, compressor){
@@ -3910,13 +3917,17 @@ merge(Compressor.prototype, {
break;
}
}
return self.evaluate(compressor)[0];
var ev = self.evaluate(compressor);
if (ev !== self) {
ev = make_node_from_constant(ev, self).optimize(compressor);
return best_of(compressor, ev, self);
}
return self;
});
function literals_in_boolean_context(self, compressor) {
if (compressor.option("booleans") && compressor.in_boolean_context()) {
var best = first_in_statement(compressor) ? best_of_statement : best_of;
return best(self, make_node(AST_Seq, self, {
return best_of(compressor, self, make_node(AST_Seq, self, {
car: self,
cdr: make_node(AST_True, self)
}).optimize(compressor));
@@ -3962,23 +3973,18 @@ merge(Compressor.prototype, {
var segments = [];
for (var i = 0; i < self.segments.length; i++) {
if (self.segments[i] instanceof AST_Node) {
var result = self.segments[i].evaluate(compressor);
// No result[1] means nothing to stringify
if (result.length === 1) {
segments.push(result[0]);
var segment = self.segments[i];
if (segment instanceof AST_Node) {
var result = segment.evaluate(compressor);
// Evaluate to constant value
// Constant value shorter than ${segment}
if (result !== segment && (result + "").length <= segment.print_to_string().length + "${}".length) {
// There should always be a previous and next segment if segment is a node
segments[segments.length - 1].value = segments[segments.length - 1].value + result + self.segments[++i].value;
continue;
}
// Evaluate length
if (result[0].print_to_string().length + 3 /* ${} */ < (result[1]+"").length) {
segments.push(result[0]);
continue;
}
// There should always be a previous and next segment if segment is a node
segments[segments.length - 1].value = segments[segments.length - 1].value + result[1] + self.segments[++i].value;
} else {
segments.push(self.segments[i]);
}
segments.push(segment);
}
self.segments = segments;