fix usage of transform() vs optimize()
Given the current `OPT()` node: - `optimize(compressor)` works on the same level, and will only `descend()` if returned instance is different - `transform(compressor)` works on child nodes, and will always `descend()` Other fixes - `loopcontrol_target()` should compare against `compressor.self()` since the current node may have already been displaced - remove obsolete optimisation in `AST_Binary` after #1477 fixes #1592
This commit is contained in:
parent
919d5e3482
commit
7550c53fd6
219
lib/compress.js
219
lib/compress.js
|
|
@ -384,7 +384,7 @@ merge(Compressor.prototype, {
|
||||||
return new ctor(props);
|
return new ctor(props);
|
||||||
};
|
};
|
||||||
|
|
||||||
function make_node_from_constant(compressor, val, orig) {
|
function make_node_from_constant(val, orig) {
|
||||||
switch (typeof val) {
|
switch (typeof val) {
|
||||||
case "string":
|
case "string":
|
||||||
return make_node(AST_String, orig, {
|
return make_node(AST_String, orig, {
|
||||||
|
|
@ -404,9 +404,9 @@ merge(Compressor.prototype, {
|
||||||
|
|
||||||
return make_node(AST_Number, orig, { value: val });
|
return make_node(AST_Number, orig, { value: val });
|
||||||
case "boolean":
|
case "boolean":
|
||||||
return make_node(val ? AST_True : AST_False, orig).optimize(compressor);
|
return make_node(val ? AST_True : AST_False, orig);
|
||||||
case "undefined":
|
case "undefined":
|
||||||
return make_node(AST_Undefined, orig).transform(compressor);
|
return make_node(AST_Undefined, orig);
|
||||||
default:
|
default:
|
||||||
if (val === null) {
|
if (val === null) {
|
||||||
return make_node(AST_Null, orig, { value: null });
|
return make_node(AST_Null, orig, { value: null });
|
||||||
|
|
@ -1217,7 +1217,7 @@ merge(Compressor.prototype, {
|
||||||
properties: props
|
properties: props
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return make_node_from_constant(compressor, value, orig);
|
return make_node_from_constant(value, orig);
|
||||||
}
|
}
|
||||||
def(AST_Node, noop);
|
def(AST_Node, noop);
|
||||||
def(AST_Dot, function(compressor, suffix){
|
def(AST_Dot, function(compressor, suffix){
|
||||||
|
|
@ -1243,20 +1243,24 @@ merge(Compressor.prototype, {
|
||||||
node.DEFMETHOD("_find_defs", func);
|
node.DEFMETHOD("_find_defs", func);
|
||||||
});
|
});
|
||||||
|
|
||||||
function best_of(ast1, ast2) {
|
function best_of_expression(ast1, ast2) {
|
||||||
return ast1.print_to_string().length >
|
return ast1.print_to_string().length >
|
||||||
ast2.print_to_string().length
|
ast2.print_to_string().length
|
||||||
? ast2 : ast1;
|
? ast2 : ast1;
|
||||||
}
|
}
|
||||||
|
|
||||||
function best_of_statement(ast1, ast2) {
|
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
|
body: ast1
|
||||||
}), make_node(AST_SimpleStatement, ast2, {
|
}), make_node(AST_SimpleStatement, ast2, {
|
||||||
body: ast2
|
body: ast2
|
||||||
})).body;
|
})).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
|
// methods to evaluate a constant expression
|
||||||
(function (def){
|
(function (def){
|
||||||
// The evaluate method returns an array with one or two
|
// The evaluate method returns an array with one or two
|
||||||
|
|
@ -1277,11 +1281,18 @@ merge(Compressor.prototype, {
|
||||||
}
|
}
|
||||||
var node;
|
var node;
|
||||||
try {
|
try {
|
||||||
node = make_node_from_constant(compressor, val, this);
|
node = make_node_from_constant(val, this);
|
||||||
} catch(ex) {
|
} catch(ex) {
|
||||||
return [ this ];
|
return [ this ];
|
||||||
}
|
}
|
||||||
return [ best_of(node, this), val ];
|
return [ node, val ];
|
||||||
|
});
|
||||||
|
AST_Node.DEFMETHOD("evaluate_self", function(compressor){
|
||||||
|
var ev = this.evaluate(compressor);
|
||||||
|
if (ev.length > 1 && !ev[0].equivalent_to(this)) {
|
||||||
|
return best_of(compressor, ev[0].optimize(compressor), this);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
});
|
});
|
||||||
var unaryPrefix = makePredicate("! ~ - +");
|
var unaryPrefix = makePredicate("! ~ - +");
|
||||||
AST_Node.DEFMETHOD("is_constant", function(){
|
AST_Node.DEFMETHOD("is_constant", function(){
|
||||||
|
|
@ -1480,9 +1491,9 @@ merge(Compressor.prototype, {
|
||||||
var stat = make_node(AST_SimpleStatement, alt, {
|
var stat = make_node(AST_SimpleStatement, alt, {
|
||||||
body: 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(){
|
def(AST_Node, function(){
|
||||||
return basic_negation(this);
|
return basic_negation(this);
|
||||||
|
|
@ -1678,7 +1689,7 @@ merge(Compressor.prototype, {
|
||||||
|
|
||||||
OPT(AST_LabeledStatement, function(self, compressor){
|
OPT(AST_LabeledStatement, function(self, compressor){
|
||||||
if (self.body instanceof AST_Break
|
if (self.body instanceof AST_Break
|
||||||
&& compressor.loopcontrol_target(self.body.label) === self.body) {
|
&& compressor.loopcontrol_target(self.body.label) === compressor.self().body) {
|
||||||
return make_node(AST_EmptyStatement, self);
|
return make_node(AST_EmptyStatement, self);
|
||||||
}
|
}
|
||||||
return self.label.references.length == 0 ? self.body : self;
|
return self.label.references.length == 0 ? self.body : self;
|
||||||
|
|
@ -2242,7 +2253,9 @@ merge(Compressor.prototype, {
|
||||||
|
|
||||||
OPT(AST_DWLoop, function(self, compressor){
|
OPT(AST_DWLoop, function(self, compressor){
|
||||||
var cond = self.condition.evaluate(compressor);
|
var cond = self.condition.evaluate(compressor);
|
||||||
self.condition = cond[0];
|
if (cond.length > 1) {
|
||||||
|
self.condition = best_of_expression(cond[0].transform(compressor), self.condition);
|
||||||
|
}
|
||||||
if (!compressor.option("loops")) return self;
|
if (!compressor.option("loops")) return self;
|
||||||
if (cond.length > 1) {
|
if (cond.length > 1) {
|
||||||
if (cond[1]) {
|
if (cond[1]) {
|
||||||
|
|
@ -2261,7 +2274,7 @@ merge(Compressor.prototype, {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (self instanceof AST_While) {
|
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;
|
return self;
|
||||||
});
|
});
|
||||||
|
|
@ -2283,7 +2296,7 @@ merge(Compressor.prototype, {
|
||||||
var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body;
|
var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body;
|
||||||
if (first instanceof AST_If) {
|
if (first instanceof AST_If) {
|
||||||
if (first.body instanceof AST_Break
|
if (first.body instanceof AST_Break
|
||||||
&& compressor.loopcontrol_target(first.body.label) === self) {
|
&& compressor.loopcontrol_target(first.body.label) === compressor.self()) {
|
||||||
if (self.condition) {
|
if (self.condition) {
|
||||||
self.condition = make_node(AST_Binary, self.condition, {
|
self.condition = make_node(AST_Binary, self.condition, {
|
||||||
left: self.condition,
|
left: self.condition,
|
||||||
|
|
@ -2296,7 +2309,7 @@ merge(Compressor.prototype, {
|
||||||
drop_it(first.alternative);
|
drop_it(first.alternative);
|
||||||
}
|
}
|
||||||
else if (first.alternative instanceof AST_Break
|
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) {
|
if (self.condition) {
|
||||||
self.condition = make_node(AST_Binary, self.condition, {
|
self.condition = make_node(AST_Binary, self.condition, {
|
||||||
left: self.condition,
|
left: self.condition,
|
||||||
|
|
@ -2315,7 +2328,9 @@ merge(Compressor.prototype, {
|
||||||
var cond = self.condition;
|
var cond = self.condition;
|
||||||
if (cond) {
|
if (cond) {
|
||||||
cond = cond.evaluate(compressor);
|
cond = cond.evaluate(compressor);
|
||||||
self.condition = cond[0];
|
if (cond.length > 1) {
|
||||||
|
self.condition = best_of_expression(cond[0].transform(compressor), self.condition);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!compressor.option("loops")) return self;
|
if (!compressor.option("loops")) return self;
|
||||||
if (cond) {
|
if (cond) {
|
||||||
|
|
@ -2348,7 +2363,6 @@ merge(Compressor.prototype, {
|
||||||
// “has no side effects”; also it doesn't work for cases like
|
// “has no side effects”; also it doesn't work for cases like
|
||||||
// `x && true`, though it probably should.
|
// `x && true`, though it probably should.
|
||||||
var cond = self.condition.evaluate(compressor);
|
var cond = self.condition.evaluate(compressor);
|
||||||
self.condition = cond[0];
|
|
||||||
if (cond.length > 1) {
|
if (cond.length > 1) {
|
||||||
if (cond[1]) {
|
if (cond[1]) {
|
||||||
compressor.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
|
compressor.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
|
||||||
|
|
@ -2358,7 +2372,7 @@ merge(Compressor.prototype, {
|
||||||
extract_declarations_from_unreachable_code(compressor, self.alternative, a);
|
extract_declarations_from_unreachable_code(compressor, self.alternative, a);
|
||||||
}
|
}
|
||||||
a.push(self.body);
|
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 {
|
} else {
|
||||||
compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
|
compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
|
||||||
|
|
@ -2366,9 +2380,10 @@ merge(Compressor.prototype, {
|
||||||
var a = [];
|
var a = [];
|
||||||
extract_declarations_from_unreachable_code(compressor, self.body, a);
|
extract_declarations_from_unreachable_code(compressor, self.body, a);
|
||||||
if (self.alternative) a.push(self.alternative);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.condition = best_of_expression(cond[0].transform(compressor), self.condition);
|
||||||
}
|
}
|
||||||
var negated = self.condition.negate(compressor);
|
var negated = self.condition.negate(compressor);
|
||||||
var self_condition_length = self.condition.print_to_string().length;
|
var self_condition_length = self.condition.print_to_string().length;
|
||||||
|
|
@ -2386,7 +2401,7 @@ merge(Compressor.prototype, {
|
||||||
if (is_empty(self.body) && is_empty(self.alternative)) {
|
if (is_empty(self.body) && is_empty(self.alternative)) {
|
||||||
return make_node(AST_SimpleStatement, self.condition, {
|
return make_node(AST_SimpleStatement, self.condition, {
|
||||||
body: self.condition
|
body: self.condition
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (self.body instanceof AST_SimpleStatement
|
if (self.body instanceof AST_SimpleStatement
|
||||||
&& self.alternative instanceof AST_SimpleStatement) {
|
&& self.alternative instanceof AST_SimpleStatement) {
|
||||||
|
|
@ -2396,7 +2411,7 @@ merge(Compressor.prototype, {
|
||||||
consequent : statement_to_expression(self.body),
|
consequent : statement_to_expression(self.body),
|
||||||
alternative : statement_to_expression(self.alternative)
|
alternative : statement_to_expression(self.alternative)
|
||||||
})
|
})
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) {
|
if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) {
|
||||||
if (self_condition_length === negated_length && !negated_is_best
|
if (self_condition_length === negated_length && !negated_is_best
|
||||||
|
|
@ -2412,14 +2427,14 @@ merge(Compressor.prototype, {
|
||||||
left : negated,
|
left : negated,
|
||||||
right : statement_to_expression(self.body)
|
right : statement_to_expression(self.body)
|
||||||
})
|
})
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
return make_node(AST_SimpleStatement, self, {
|
return make_node(AST_SimpleStatement, self, {
|
||||||
body: make_node(AST_Binary, self, {
|
body: make_node(AST_Binary, self, {
|
||||||
operator : "&&",
|
operator : "&&",
|
||||||
left : self.condition,
|
left : self.condition,
|
||||||
right : statement_to_expression(self.body)
|
right : statement_to_expression(self.body)
|
||||||
})
|
})
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (self.body instanceof AST_EmptyStatement
|
if (self.body instanceof AST_EmptyStatement
|
||||||
&& self.alternative
|
&& self.alternative
|
||||||
|
|
@ -2430,7 +2445,7 @@ merge(Compressor.prototype, {
|
||||||
left : self.condition,
|
left : self.condition,
|
||||||
right : statement_to_expression(self.alternative)
|
right : statement_to_expression(self.alternative)
|
||||||
})
|
})
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (self.body instanceof AST_Exit
|
if (self.body instanceof AST_Exit
|
||||||
&& self.alternative instanceof AST_Exit
|
&& self.alternative instanceof AST_Exit
|
||||||
|
|
@ -2441,7 +2456,7 @@ merge(Compressor.prototype, {
|
||||||
consequent : self.body.value || make_node(AST_Undefined, self.body),
|
consequent : self.body.value || make_node(AST_Undefined, self.body),
|
||||||
alternative : self.alternative.value || make_node(AST_Undefined, self.alternative)
|
alternative : self.alternative.value || make_node(AST_Undefined, self.alternative)
|
||||||
})
|
})
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (self.body instanceof AST_If
|
if (self.body instanceof AST_If
|
||||||
&& !self.body.alternative
|
&& !self.body.alternative
|
||||||
|
|
@ -2459,7 +2474,7 @@ merge(Compressor.prototype, {
|
||||||
self.alternative = null;
|
self.alternative = null;
|
||||||
return make_node(AST_BlockStatement, self, {
|
return make_node(AST_BlockStatement, self, {
|
||||||
body: [ self, alt ]
|
body: [ self, alt ]
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (aborts(self.alternative)) {
|
if (aborts(self.alternative)) {
|
||||||
|
|
@ -2469,7 +2484,7 @@ merge(Compressor.prototype, {
|
||||||
self.alternative = null;
|
self.alternative = null;
|
||||||
return make_node(AST_BlockStatement, self, {
|
return make_node(AST_BlockStatement, self, {
|
||||||
body: [ self, body ]
|
body: [ self, body ]
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
});
|
});
|
||||||
|
|
@ -2478,13 +2493,13 @@ merge(Compressor.prototype, {
|
||||||
if (self.body.length == 0 && compressor.option("conditionals")) {
|
if (self.body.length == 0 && compressor.option("conditionals")) {
|
||||||
return make_node(AST_SimpleStatement, self, {
|
return make_node(AST_SimpleStatement, self, {
|
||||||
body: self.expression
|
body: self.expression
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
for(;;) {
|
for(;;) {
|
||||||
var last_branch = self.body[self.body.length - 1];
|
var last_branch = self.body[self.body.length - 1];
|
||||||
if (last_branch) {
|
if (last_branch) {
|
||||||
var stat = last_branch.body[last_branch.body.length - 1]; // last statement
|
var stat = last_branch.body[last_branch.body.length - 1]; // last statement
|
||||||
if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self)
|
if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === compressor.self())
|
||||||
last_branch.body.pop();
|
last_branch.body.pop();
|
||||||
if (last_branch instanceof AST_Default && last_branch.body.length == 0) {
|
if (last_branch instanceof AST_Default && last_branch.body.length == 0) {
|
||||||
self.body.pop();
|
self.body.pop();
|
||||||
|
|
@ -2496,7 +2511,7 @@ merge(Compressor.prototype, {
|
||||||
var exp = self.expression.evaluate(compressor);
|
var exp = self.expression.evaluate(compressor);
|
||||||
out: if (exp.length == 2) try {
|
out: if (exp.length == 2) try {
|
||||||
// constant expression
|
// constant expression
|
||||||
self.expression = exp[0];
|
self.expression = best_of_expression(exp[0].transform(compressor), self.expression);
|
||||||
if (!compressor.option("dead_code")) break out;
|
if (!compressor.option("dead_code")) break out;
|
||||||
var value = exp[1];
|
var value = exp[1];
|
||||||
var in_if = false;
|
var in_if = false;
|
||||||
|
|
@ -2509,14 +2524,14 @@ merge(Compressor.prototype, {
|
||||||
// no need to descend these node types
|
// no need to descend these node types
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_Switch && node === self) {
|
else if (node === self) {
|
||||||
node = node.clone();
|
node = node.clone();
|
||||||
descend(node, this);
|
descend(node, this);
|
||||||
return ruined ? node : make_node(AST_BlockStatement, node, {
|
return ruined ? node : make_node(AST_BlockStatement, node, {
|
||||||
body: node.body.reduce(function(a, branch){
|
body: node.body.reduce(function(a, branch){
|
||||||
return a.concat(branch.body);
|
return a.concat(branch.body);
|
||||||
}, [])
|
}, [])
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_If || node instanceof AST_Try) {
|
else if (node instanceof AST_If || node instanceof AST_Try) {
|
||||||
var save = in_if;
|
var save = in_if;
|
||||||
|
|
@ -2532,7 +2547,7 @@ merge(Compressor.prototype, {
|
||||||
in_block = save;
|
in_block = save;
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_Break && this.loopcontrol_target(node.label) === self) {
|
else if (node instanceof AST_Break && this.loopcontrol_target(node.label) === compressor.self()) {
|
||||||
if (in_if) {
|
if (in_if) {
|
||||||
ruined = true;
|
ruined = true;
|
||||||
return node;
|
return node;
|
||||||
|
|
@ -2656,7 +2671,7 @@ merge(Compressor.prototype, {
|
||||||
if (self.args.length != 1) {
|
if (self.args.length != 1) {
|
||||||
return make_node(AST_Array, self, {
|
return make_node(AST_Array, self, {
|
||||||
elements: self.args
|
elements: self.args
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "Object":
|
case "Object":
|
||||||
|
|
@ -2674,7 +2689,7 @@ merge(Compressor.prototype, {
|
||||||
left: self.args[0],
|
left: self.args[0],
|
||||||
operator: "+",
|
operator: "+",
|
||||||
right: make_node(AST_String, self, { value: "" })
|
right: make_node(AST_String, self, { value: "" })
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
break;
|
break;
|
||||||
case "Number":
|
case "Number":
|
||||||
if (self.args.length == 0) return make_node(AST_Number, self, {
|
if (self.args.length == 0) return make_node(AST_Number, self, {
|
||||||
|
|
@ -2683,7 +2698,7 @@ merge(Compressor.prototype, {
|
||||||
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
|
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
|
||||||
expression: self.args[0],
|
expression: self.args[0],
|
||||||
operator: "+"
|
operator: "+"
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
case "Boolean":
|
case "Boolean":
|
||||||
if (self.args.length == 0) return make_node(AST_False, self);
|
if (self.args.length == 0) return make_node(AST_False, self);
|
||||||
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
|
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
|
||||||
|
|
@ -2692,7 +2707,7 @@ merge(Compressor.prototype, {
|
||||||
operator: "!"
|
operator: "!"
|
||||||
}),
|
}),
|
||||||
operator: "!"
|
operator: "!"
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
break;
|
break;
|
||||||
case "Function":
|
case "Function":
|
||||||
// new Function() => function(){}
|
// new Function() => function(){}
|
||||||
|
|
@ -2757,7 +2772,7 @@ merge(Compressor.prototype, {
|
||||||
left: make_node(AST_String, self, { value: "" }),
|
left: make_node(AST_String, self, { value: "" }),
|
||||||
operator: "+",
|
operator: "+",
|
||||||
right: exp.expression
|
right: exp.expression
|
||||||
}).transform(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
else if (exp instanceof AST_Dot && exp.expression instanceof AST_Array && exp.property == "join") EXIT: {
|
else if (exp instanceof AST_Dot && exp.expression instanceof AST_Array && exp.property == "join") EXIT: {
|
||||||
var separator;
|
var separator;
|
||||||
|
|
@ -2769,9 +2784,9 @@ merge(Compressor.prototype, {
|
||||||
var elements = [];
|
var elements = [];
|
||||||
var consts = [];
|
var consts = [];
|
||||||
exp.expression.elements.forEach(function(el) {
|
exp.expression.elements.forEach(function(el) {
|
||||||
el = el.evaluate(compressor);
|
var ev = el.evaluate(compressor);
|
||||||
if (el.length > 1) {
|
if (ev.length > 1) {
|
||||||
consts.push(el[1]);
|
consts.push(ev[1]);
|
||||||
} else {
|
} else {
|
||||||
if (consts.length > 0) {
|
if (consts.length > 0) {
|
||||||
elements.push(make_node(AST_String, self, {
|
elements.push(make_node(AST_String, self, {
|
||||||
|
|
@ -2779,7 +2794,7 @@ merge(Compressor.prototype, {
|
||||||
}));
|
}));
|
||||||
consts.length = 0;
|
consts.length = 0;
|
||||||
}
|
}
|
||||||
elements.push(el[0]);
|
elements.push(el);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (consts.length > 0) {
|
if (consts.length > 0) {
|
||||||
|
|
@ -2812,15 +2827,15 @@ merge(Compressor.prototype, {
|
||||||
left : prev,
|
left : prev,
|
||||||
right : el
|
right : el
|
||||||
});
|
});
|
||||||
}, first).transform(compressor);
|
}, first).optimize(compressor);
|
||||||
}
|
}
|
||||||
// need this awkward cloning to not affect original element
|
// need this awkward cloning to not affect original element
|
||||||
// best_of will decide which one to get through.
|
// best_of_expression will decide which one to get through.
|
||||||
var node = self.clone();
|
var node = self.clone();
|
||||||
node.expression = node.expression.clone();
|
node.expression = node.expression.clone();
|
||||||
node.expression.expression = node.expression.expression.clone();
|
node.expression.expression = node.expression.expression.clone();
|
||||||
node.expression.expression.elements = elements;
|
node.expression.expression.elements = elements;
|
||||||
return best_of(self, node);
|
return best_of_expression(self, node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (exp instanceof AST_Function) {
|
if (exp instanceof AST_Function) {
|
||||||
|
|
@ -2828,13 +2843,13 @@ merge(Compressor.prototype, {
|
||||||
var value = exp.body[0].value;
|
var value = exp.body[0].value;
|
||||||
if (!value || value.is_constant()) {
|
if (!value || value.is_constant()) {
|
||||||
var args = self.args.concat(value || make_node(AST_Undefined, self));
|
var args = self.args.concat(value || make_node(AST_Undefined, self));
|
||||||
return AST_Seq.from_array(args).transform(compressor);
|
return AST_Seq.from_array(args).optimize(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (compressor.option("side_effects")) {
|
if (compressor.option("side_effects")) {
|
||||||
if (!AST_Block.prototype.has_side_effects.call(exp, compressor)) {
|
if (!AST_Block.prototype.has_side_effects.call(exp, compressor)) {
|
||||||
var args = self.args.concat(make_node(AST_Undefined, self));
|
var args = self.args.concat(make_node(AST_Undefined, self));
|
||||||
return AST_Seq.from_array(args).transform(compressor);
|
return AST_Seq.from_array(args).optimize(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2847,7 +2862,7 @@ merge(Compressor.prototype, {
|
||||||
if (name instanceof AST_SymbolRef
|
if (name instanceof AST_SymbolRef
|
||||||
&& name.name == "console"
|
&& name.name == "console"
|
||||||
&& name.undeclared()) {
|
&& name.undeclared()) {
|
||||||
return make_node(AST_Undefined, self).transform(compressor);
|
return make_node(AST_Undefined, self).optimize(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2869,7 +2884,7 @@ merge(Compressor.prototype, {
|
||||||
case "Function":
|
case "Function":
|
||||||
case "Error":
|
case "Error":
|
||||||
case "Array":
|
case "Array":
|
||||||
return make_node(AST_Call, self, self).transform(compressor);
|
return make_node(AST_Call, self, self).optimize(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2932,7 +2947,7 @@ merge(Compressor.prototype, {
|
||||||
var x = seq.to_array();
|
var x = seq.to_array();
|
||||||
this.expression = x.pop();
|
this.expression = x.pop();
|
||||||
x.push(this);
|
x.push(this);
|
||||||
seq = AST_Seq.from_array(x).transform(compressor);
|
seq = AST_Seq.from_array(x);
|
||||||
return seq;
|
return seq;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2940,13 +2955,14 @@ merge(Compressor.prototype, {
|
||||||
});
|
});
|
||||||
|
|
||||||
OPT(AST_UnaryPostfix, function(self, compressor){
|
OPT(AST_UnaryPostfix, function(self, compressor){
|
||||||
return self.lift_sequences(compressor);
|
var seq = self.lift_sequences(compressor);
|
||||||
|
return seq !== self ? seq.optimize(compressor) : self;
|
||||||
});
|
});
|
||||||
|
|
||||||
OPT(AST_UnaryPrefix, function(self, compressor){
|
OPT(AST_UnaryPrefix, function(self, compressor){
|
||||||
var seq = self.lift_sequences(compressor);
|
var seq = self.lift_sequences(compressor);
|
||||||
if (seq !== self) {
|
if (seq !== self) {
|
||||||
return seq;
|
return seq.optimize(compressor);
|
||||||
}
|
}
|
||||||
var e = self.expression;
|
var e = self.expression;
|
||||||
if (compressor.option("side_effects") && self.operator == "void") {
|
if (compressor.option("side_effects") && self.operator == "void") {
|
||||||
|
|
@ -2955,7 +2971,7 @@ merge(Compressor.prototype, {
|
||||||
self.expression = e;
|
self.expression = e;
|
||||||
return self;
|
return self;
|
||||||
} else {
|
} else {
|
||||||
return make_node(AST_Undefined, self).transform(compressor);
|
return make_node(AST_Undefined, self).optimize(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (compressor.option("booleans") && compressor.in_boolean_context()) {
|
if (compressor.option("booleans") && compressor.in_boolean_context()) {
|
||||||
|
|
@ -2966,8 +2982,7 @@ merge(Compressor.prototype, {
|
||||||
return e.expression;
|
return e.expression;
|
||||||
}
|
}
|
||||||
if (e instanceof AST_Binary) {
|
if (e instanceof AST_Binary) {
|
||||||
var statement = first_in_statement(compressor);
|
self = best_of(compressor, self, e.negate(compressor, first_in_statement(compressor)));
|
||||||
self = (statement ? best_of_statement : best_of)(self, e.negate(compressor, statement));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "typeof":
|
case "typeof":
|
||||||
|
|
@ -2980,7 +2995,7 @@ merge(Compressor.prototype, {
|
||||||
}).optimize(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return self.evaluate(compressor)[0];
|
return self.evaluate_self(compressor);
|
||||||
});
|
});
|
||||||
|
|
||||||
function has_side_effects_or_prop_access(node, compressor) {
|
function has_side_effects_or_prop_access(node, compressor) {
|
||||||
|
|
@ -2998,7 +3013,7 @@ merge(Compressor.prototype, {
|
||||||
var x = seq.to_array();
|
var x = seq.to_array();
|
||||||
this.left = x.pop();
|
this.left = x.pop();
|
||||||
x.push(this);
|
x.push(this);
|
||||||
seq = AST_Seq.from_array(x).transform(compressor);
|
seq = AST_Seq.from_array(x);
|
||||||
return seq;
|
return seq;
|
||||||
}
|
}
|
||||||
if (this.right instanceof AST_Seq
|
if (this.right instanceof AST_Seq
|
||||||
|
|
@ -3008,7 +3023,7 @@ merge(Compressor.prototype, {
|
||||||
var x = seq.to_array();
|
var x = seq.to_array();
|
||||||
this.right = x.pop();
|
this.right = x.pop();
|
||||||
x.push(this);
|
x.push(this);
|
||||||
seq = AST_Seq.from_array(x).transform(compressor);
|
seq = AST_Seq.from_array(x);
|
||||||
return seq;
|
return seq;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3018,16 +3033,6 @@ merge(Compressor.prototype, {
|
||||||
var commutativeOperators = makePredicate("== === != !== * & | ^");
|
var commutativeOperators = makePredicate("== === != !== * & | ^");
|
||||||
|
|
||||||
OPT(AST_Binary, function(self, compressor){
|
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() {
|
function reversible() {
|
||||||
return self.left instanceof AST_Constant
|
return self.left instanceof AST_Constant
|
||||||
|| self.right instanceof AST_Constant
|
|| self.right instanceof AST_Constant
|
||||||
|
|
@ -3081,7 +3086,10 @@ merge(Compressor.prototype, {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self = self.lift_sequences(compressor);
|
var seq = self.lift_sequences(compressor);
|
||||||
|
if (seq !== self) {
|
||||||
|
return seq.optimize(compressor);
|
||||||
|
}
|
||||||
if (compressor.option("comparisons")) switch (self.operator) {
|
if (compressor.option("comparisons")) switch (self.operator) {
|
||||||
case "===":
|
case "===":
|
||||||
case "!==":
|
case "!==":
|
||||||
|
|
@ -3102,7 +3110,7 @@ merge(Compressor.prototype, {
|
||||||
if (expr instanceof AST_SymbolRef ? !expr.undeclared()
|
if (expr instanceof AST_SymbolRef ? !expr.undeclared()
|
||||||
: !(expr instanceof AST_PropAccess) || compressor.option("screw_ie8")) {
|
: !(expr instanceof AST_PropAccess) || compressor.option("screw_ie8")) {
|
||||||
self.right = expr;
|
self.right = expr;
|
||||||
self.left = make_node(AST_Undefined, self.left).optimize(compressor);
|
self.left = make_node(AST_Undefined, self.left).transform(compressor);
|
||||||
if (self.operator.length == 2) self.operator += "=";
|
if (self.operator.length == 2) self.operator += "=";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3115,15 +3123,15 @@ merge(Compressor.prototype, {
|
||||||
if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) {
|
if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) {
|
||||||
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
|
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
|
||||||
return make_node(AST_Seq, self, {
|
return make_node(AST_Seq, self, {
|
||||||
car: self.left,
|
car: self.left.transform(compressor),
|
||||||
cdr: make_node(AST_False, self)
|
cdr: make_node(AST_False, self).transform(compressor)
|
||||||
}).optimize(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (ll.length > 1 && ll[1]) {
|
if (ll.length > 1 && ll[1]) {
|
||||||
return rr[0];
|
return best_of(compressor, rr[0].optimize(compressor), self.right);
|
||||||
}
|
}
|
||||||
if (rr.length > 1 && rr[1]) {
|
if (rr.length > 1 && rr[1]) {
|
||||||
return ll[0];
|
return best_of(compressor, ll[0].optimize(compressor), self.left);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "||":
|
case "||":
|
||||||
|
|
@ -3132,15 +3140,15 @@ merge(Compressor.prototype, {
|
||||||
if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) {
|
if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) {
|
||||||
compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
|
compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
|
||||||
return make_node(AST_Seq, self, {
|
return make_node(AST_Seq, self, {
|
||||||
car: self.left,
|
car: self.left.transform(compressor),
|
||||||
cdr: make_node(AST_True, self)
|
cdr: make_node(AST_True, self).transform(compressor)
|
||||||
}).optimize(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (ll.length > 1 && !ll[1]) {
|
if (ll.length > 1 && !ll[1]) {
|
||||||
return rr[0];
|
return best_of(compressor, rr[0].optimize(compressor), self.right);
|
||||||
}
|
}
|
||||||
if (rr.length > 1 && !rr[1]) {
|
if (rr.length > 1 && !rr[1]) {
|
||||||
return ll[0];
|
return best_of(compressor, ll[0].optimize(compressor), self.left);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "+":
|
case "+":
|
||||||
|
|
@ -3149,15 +3157,15 @@ merge(Compressor.prototype, {
|
||||||
if (ll.length > 1 && ll[0] instanceof AST_String && ll[1]) {
|
if (ll.length > 1 && ll[0] instanceof AST_String && ll[1]) {
|
||||||
compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
|
compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
|
||||||
return make_node(AST_Seq, self, {
|
return make_node(AST_Seq, self, {
|
||||||
car: self.right,
|
car: self.right.transform(compressor),
|
||||||
cdr: make_node(AST_True, self)
|
cdr: make_node(AST_True, self).transform(compressor)
|
||||||
}).optimize(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (rr.length > 1 && rr[0] instanceof AST_String && rr[1]) {
|
if (rr.length > 1 && rr[0] instanceof AST_String && rr[1]) {
|
||||||
compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
|
compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
|
||||||
return make_node(AST_Seq, self, {
|
return make_node(AST_Seq, self, {
|
||||||
car: self.left,
|
car: self.left.transform(compressor),
|
||||||
cdr: make_node(AST_True, self)
|
cdr: make_node(AST_True, self).transform(compressor)
|
||||||
}).optimize(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -3165,12 +3173,11 @@ merge(Compressor.prototype, {
|
||||||
if (compressor.option("comparisons") && self.is_boolean()) {
|
if (compressor.option("comparisons") && self.is_boolean()) {
|
||||||
if (!(compressor.parent() instanceof AST_Binary)
|
if (!(compressor.parent() instanceof AST_Binary)
|
||||||
|| compressor.parent() instanceof AST_Assign) {
|
|| compressor.parent() instanceof AST_Assign) {
|
||||||
var statement = first_in_statement(compressor);
|
|
||||||
var negated = make_node(AST_UnaryPrefix, self, {
|
var negated = make_node(AST_UnaryPrefix, self, {
|
||||||
operator: "!",
|
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")) {
|
if (compressor.option("unsafe_comps")) {
|
||||||
switch (self.operator) {
|
switch (self.operator) {
|
||||||
|
|
@ -3196,7 +3203,7 @@ merge(Compressor.prototype, {
|
||||||
&& self.left.left.getValue() == ""
|
&& self.left.left.getValue() == ""
|
||||||
&& self.right.is_string(compressor)) {
|
&& self.right.is_string(compressor)) {
|
||||||
self.left = self.left.right;
|
self.left = self.left.right;
|
||||||
return self.transform(compressor);
|
return self.optimize(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (compressor.option("evaluate")) {
|
if (compressor.option("evaluate")) {
|
||||||
|
|
@ -3322,9 +3329,9 @@ merge(Compressor.prototype, {
|
||||||
});
|
});
|
||||||
if (self.right instanceof AST_Constant
|
if (self.right instanceof AST_Constant
|
||||||
&& !(self.left instanceof AST_Constant)) {
|
&& !(self.left instanceof AST_Constant)) {
|
||||||
self = best_of(reversed, self);
|
self = best_of(compressor, reversed, self);
|
||||||
} else {
|
} else {
|
||||||
self = best_of(self, reversed);
|
self = best_of(compressor, self, reversed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (associative && self.is_number(compressor)) {
|
if (associative && self.is_number(compressor)) {
|
||||||
|
|
@ -3417,11 +3424,10 @@ merge(Compressor.prototype, {
|
||||||
operator : self.operator,
|
operator : self.operator,
|
||||||
left : self.left,
|
left : self.left,
|
||||||
right : self.right.left
|
right : self.right.left
|
||||||
});
|
}).transform(compressor);
|
||||||
self.right = self.right.right;
|
self.right = self.right.right.transform(compressor);
|
||||||
return self.transform(compressor);
|
|
||||||
}
|
}
|
||||||
return self.evaluate(compressor)[0];
|
return self.evaluate_self(compressor);
|
||||||
});
|
});
|
||||||
|
|
||||||
OPT(AST_SymbolRef, function(self, compressor){
|
OPT(AST_SymbolRef, function(self, compressor){
|
||||||
|
|
@ -3436,11 +3442,11 @@ merge(Compressor.prototype, {
|
||||||
&& (!self.scope.uses_with || !compressor.find_parent(AST_With))) {
|
&& (!self.scope.uses_with || !compressor.find_parent(AST_With))) {
|
||||||
switch (self.name) {
|
switch (self.name) {
|
||||||
case "undefined":
|
case "undefined":
|
||||||
return make_node(AST_Undefined, self).transform(compressor);
|
return make_node(AST_Undefined, self).optimize(compressor);
|
||||||
case "NaN":
|
case "NaN":
|
||||||
return make_node(AST_NaN, self).transform(compressor);
|
return make_node(AST_NaN, self).optimize(compressor);
|
||||||
case "Infinity":
|
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")) {
|
if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
|
||||||
|
|
@ -3449,11 +3455,12 @@ merge(Compressor.prototype, {
|
||||||
if (d.should_replace === undefined) {
|
if (d.should_replace === undefined) {
|
||||||
var init = d.fixed.evaluate(compressor);
|
var init = d.fixed.evaluate(compressor);
|
||||||
if (init.length > 1) {
|
if (init.length > 1) {
|
||||||
var value = init[0].print_to_string().length;
|
init = best_of(compressor, init[0].optimize(compressor), d.fixed);
|
||||||
|
var value = init.print_to_string().length;
|
||||||
var name = d.name.length;
|
var name = d.name.length;
|
||||||
var freq = d.references.length;
|
var freq = d.references.length;
|
||||||
var overhead = d.global || !freq ? 0 : (name + 2 + value) / freq;
|
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 {
|
} else {
|
||||||
d.should_replace = false;
|
d.should_replace = false;
|
||||||
}
|
}
|
||||||
|
|
@ -3494,7 +3501,10 @@ merge(Compressor.prototype, {
|
||||||
var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];
|
var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];
|
||||||
var ASSIGN_OPS_COMMUTATIVE = [ '*', '|', '^', '&' ];
|
var ASSIGN_OPS_COMMUTATIVE = [ '*', '|', '^', '&' ];
|
||||||
OPT(AST_Assign, function(self, compressor){
|
OPT(AST_Assign, function(self, compressor){
|
||||||
self = self.lift_sequences(compressor);
|
var seq = self.lift_sequences(compressor);
|
||||||
|
if (seq !== self) {
|
||||||
|
return seq.optimize(compressor);
|
||||||
|
}
|
||||||
if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
|
if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
|
||||||
// x = expr1 OP expr2
|
// x = expr1 OP expr2
|
||||||
if (self.right.left instanceof AST_SymbolRef
|
if (self.right.left instanceof AST_SymbolRef
|
||||||
|
|
@ -3532,10 +3542,10 @@ merge(Compressor.prototype, {
|
||||||
compressor.warn("Condition always false [{file}:{line},{col}]", self.start);
|
compressor.warn("Condition always false [{file}:{line},{col}]", self.start);
|
||||||
return maintain_this_binding(compressor.parent(), self, self.alternative);
|
return maintain_this_binding(compressor.parent(), self, self.alternative);
|
||||||
}
|
}
|
||||||
|
self.condition = best_of(compressor, cond[0].transform(compressor), self.condition);
|
||||||
}
|
}
|
||||||
var statement = first_in_statement(compressor);
|
var negated = self.condition.negate(compressor, first_in_statement(compressor));
|
||||||
var negated = cond[0].negate(compressor, statement);
|
if (best_of(compressor, self.condition, negated) === negated) {
|
||||||
if ((statement ? best_of_statement : best_of)(cond[0], negated) === negated) {
|
|
||||||
self = make_node(AST_Conditional, self, {
|
self = make_node(AST_Conditional, self, {
|
||||||
condition: negated,
|
condition: negated,
|
||||||
consequent: self.alternative,
|
consequent: self.alternative,
|
||||||
|
|
@ -3715,7 +3725,7 @@ merge(Compressor.prototype, {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return self.evaluate(compressor)[0];
|
return self.evaluate_self(compressor);
|
||||||
});
|
});
|
||||||
|
|
||||||
OPT(AST_Dot, function(self, compressor){
|
OPT(AST_Dot, function(self, compressor){
|
||||||
|
|
@ -3754,13 +3764,12 @@ merge(Compressor.prototype, {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return self.evaluate(compressor)[0];
|
return self.evaluate_self(compressor);
|
||||||
});
|
});
|
||||||
|
|
||||||
function literals_in_boolean_context(self, compressor) {
|
function literals_in_boolean_context(self, compressor) {
|
||||||
if (compressor.option("booleans") && compressor.in_boolean_context()) {
|
if (compressor.option("booleans") && compressor.in_boolean_context()) {
|
||||||
var best = first_in_statement(compressor) ? best_of_statement : best_of;
|
return best_of(compressor, self, make_node(AST_Seq, self, {
|
||||||
return best(self, make_node(AST_Seq, self, {
|
|
||||||
car: self,
|
car: self,
|
||||||
cdr: make_node(AST_True, self)
|
cdr: make_node(AST_True, self)
|
||||||
}).optimize(compressor));
|
}).optimize(compressor));
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ iifes_returning_constants_keep_fargs_true: {
|
||||||
console.log(void 0);
|
console.log(void 0);
|
||||||
console.log(2);
|
console.log(2);
|
||||||
console.log(6);
|
console.log(6);
|
||||||
console.log((a(), b(), 6));
|
console.log((0, a(), b(), 6));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
68
test/compress/transform-optimize.js
Normal file
68
test/compress/transform-optimize.js
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
booleans_evaluate: {
|
||||||
|
options = {
|
||||||
|
booleans: true,
|
||||||
|
evaluate: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
console.log(typeof void 0 != "undefined");
|
||||||
|
console.log(1 == 1, 1 === 1)
|
||||||
|
console.log(1 != 1, 1 !== 1)
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(!1);
|
||||||
|
console.log(!0, !0);
|
||||||
|
console.log(!1, !1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
booleans_global_defs: {
|
||||||
|
options = {
|
||||||
|
booleans: true,
|
||||||
|
evaluate: true,
|
||||||
|
global_defs: {
|
||||||
|
A: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
console.log(A == 1);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(!0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
condition_evaluate: {
|
||||||
|
options = {
|
||||||
|
booleans: true,
|
||||||
|
dead_code: false,
|
||||||
|
evaluate: true,
|
||||||
|
loops: false,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
while (1 === 2);
|
||||||
|
for (; 1 == true;);
|
||||||
|
if (void 0 == null);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
while (!1);
|
||||||
|
for (; !0;);
|
||||||
|
if (!0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while_if_break: {
|
||||||
|
options = {
|
||||||
|
conditionals: true,
|
||||||
|
loops: true,
|
||||||
|
sequences: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
while (a) {
|
||||||
|
if (b) if(c) d;
|
||||||
|
if (e) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
for(; a && (b && c && d, !e););
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user