Merge branch 'master' into harmony-2.8.0
This commit is contained in:
154
lib/compress.js
154
lib/compress.js
@@ -1407,9 +1407,7 @@ merge(Compressor.prototype, {
|
||||
&& (comments = this.start.comments_before)
|
||||
&& comments.length
|
||||
&& /[@#]__PURE__/.test((last_comment = comments[comments.length - 1]).value)) {
|
||||
compressor.warn("Dropping __PURE__ call [{file}:{line},{col}]", this.start);
|
||||
last_comment.value = last_comment.value.replace(/[@#]__PURE__/g, ' ');
|
||||
pure = true;
|
||||
pure = last_comment;
|
||||
}
|
||||
return this.pure = pure;
|
||||
});
|
||||
@@ -1776,8 +1774,7 @@ merge(Compressor.prototype, {
|
||||
line : def.name.start.line,
|
||||
col : def.name.start.col
|
||||
};
|
||||
if (def.value && def.value.has_side_effects(compressor)) {
|
||||
def._unused_side_effects = true;
|
||||
if (def.value && (def._unused_side_effects = def.value.drop_side_effect_free(compressor))) {
|
||||
compressor.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", w);
|
||||
return true;
|
||||
}
|
||||
@@ -1797,7 +1794,7 @@ merge(Compressor.prototype, {
|
||||
for (var i = 0; i < def.length;) {
|
||||
var x = def[i];
|
||||
if (x._unused_side_effects) {
|
||||
side_effects.push(x.value);
|
||||
side_effects.push(x._unused_side_effects);
|
||||
def.splice(i, 1);
|
||||
} else {
|
||||
if (side_effects.length > 0) {
|
||||
@@ -2040,6 +2037,10 @@ merge(Compressor.prototype, {
|
||||
def(AST_This, return_null);
|
||||
def(AST_Call, function(compressor, first_in_statement){
|
||||
if (!this.has_pure_annotation(compressor) && compressor.pure_funcs(this)) return this;
|
||||
if (this.pure) {
|
||||
compressor.warn("Dropping __PURE__ call [{file}:{line},{col}]", this.start);
|
||||
this.pure.value = this.pure.value.replace(/[@#]__PURE__/g, ' ');
|
||||
}
|
||||
var args = trim(this.args, compressor, first_in_statement);
|
||||
return args && AST_Seq.from_array(args);
|
||||
});
|
||||
@@ -2091,8 +2092,17 @@ merge(Compressor.prototype, {
|
||||
case "typeof":
|
||||
if (this.expression instanceof AST_SymbolRef) return null;
|
||||
default:
|
||||
if (first_in_statement && is_iife_call(this.expression)) return this;
|
||||
return this.expression.drop_side_effect_free(compressor, first_in_statement);
|
||||
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
|
||||
if (first_in_statement
|
||||
&& this instanceof AST_UnaryPrefix
|
||||
&& is_iife_call(expression)) {
|
||||
if (expression === this.expression && this.operator.length === 1) return this;
|
||||
return make_node(AST_UnaryPrefix, this, {
|
||||
operator: this.operator.length === 1 ? this.operator : "!",
|
||||
expression: expression
|
||||
});
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
});
|
||||
def(AST_SymbolRef, function() {
|
||||
@@ -2140,10 +2150,6 @@ merge(Compressor.prototype, {
|
||||
OPT(AST_SimpleStatement, function(self, compressor){
|
||||
if (compressor.option("side_effects")) {
|
||||
var body = self.body;
|
||||
if (!body.has_side_effects(compressor)) {
|
||||
compressor.warn("Dropping side-effect-free statement [{file}:{line},{col}]", self.start);
|
||||
return make_node(AST_EmptyStatement, self);
|
||||
}
|
||||
var node = body.drop_side_effect_free(compressor, true);
|
||||
if (!node) {
|
||||
compressor.warn("Dropping side-effect-free statement [{file}:{line},{col}]", self.start);
|
||||
@@ -2780,17 +2786,10 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
if (!self.car.has_side_effects(compressor)
|
||||
&& !self.cdr.has_side_effects(compressor)
|
||||
&& self.car.equivalent_to(self.cdr)) {
|
||||
return self.car;
|
||||
}
|
||||
}
|
||||
if (self.cdr instanceof AST_UnaryPrefix
|
||||
&& self.cdr.operator == "void"
|
||||
&& !self.cdr.expression.has_side_effects(compressor)) {
|
||||
self.cdr.expression = self.car;
|
||||
return self.cdr;
|
||||
}
|
||||
if (self.cdr instanceof AST_Undefined) {
|
||||
return make_node(AST_UnaryPrefix, self, {
|
||||
operator : "void",
|
||||
@@ -2819,8 +2818,20 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
OPT(AST_UnaryPrefix, function(self, compressor){
|
||||
self = self.lift_sequences(compressor);
|
||||
var seq = self.lift_sequences(compressor);
|
||||
if (seq !== self) {
|
||||
return seq;
|
||||
}
|
||||
var e = self.expression;
|
||||
if (compressor.option("side_effects") && self.operator == "void") {
|
||||
e = e.drop_side_effect_free(compressor);
|
||||
if (e) {
|
||||
self.expression = e;
|
||||
return self;
|
||||
} else {
|
||||
return make_node(AST_Undefined, self);
|
||||
}
|
||||
}
|
||||
if (compressor.option("booleans") && compressor.in_boolean_context()) {
|
||||
switch (self.operator) {
|
||||
case "!":
|
||||
@@ -2837,13 +2848,10 @@ merge(Compressor.prototype, {
|
||||
// typeof always returns a non-empty string, thus it's
|
||||
// always true in booleans
|
||||
compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
|
||||
if (self.expression.has_side_effects(compressor)) {
|
||||
return make_node(AST_Seq, self, {
|
||||
car: self.expression,
|
||||
cdr: make_node(AST_True, self)
|
||||
});
|
||||
}
|
||||
return make_node(AST_True, self);
|
||||
return make_node(AST_Seq, self, {
|
||||
car: e,
|
||||
cdr: make_node(AST_True, self)
|
||||
}).optimize(compressor);
|
||||
}
|
||||
}
|
||||
return self.evaluate(compressor)[0];
|
||||
@@ -2973,13 +2981,10 @@ merge(Compressor.prototype, {
|
||||
var rr = self.right.evaluate(compressor);
|
||||
if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) {
|
||||
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
|
||||
if (self.left.has_side_effects(compressor)) {
|
||||
return make_node(AST_Seq, self, {
|
||||
car: self.left,
|
||||
cdr: make_node(AST_False)
|
||||
}).optimize(compressor);
|
||||
}
|
||||
return make_node(AST_False, self);
|
||||
return make_node(AST_Seq, self, {
|
||||
car: self.left,
|
||||
cdr: make_node(AST_False)
|
||||
}).optimize(compressor);
|
||||
}
|
||||
if (ll.length > 1 && ll[1]) {
|
||||
return rr[0];
|
||||
@@ -2993,13 +2998,10 @@ merge(Compressor.prototype, {
|
||||
var rr = self.right.evaluate(compressor);
|
||||
if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) {
|
||||
compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
|
||||
if (self.left.has_side_effects(compressor)) {
|
||||
return make_node(AST_Seq, self, {
|
||||
car: self.left,
|
||||
cdr: make_node(AST_True)
|
||||
}).optimize(compressor);
|
||||
}
|
||||
return make_node(AST_True, self);
|
||||
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];
|
||||
@@ -3011,10 +3013,19 @@ merge(Compressor.prototype, {
|
||||
case "+":
|
||||
var ll = self.left.evaluate(compressor);
|
||||
var rr = self.right.evaluate(compressor);
|
||||
if ((ll.length > 1 && ll[0] instanceof AST_String && ll[1] && !self.right.has_side_effects(compressor)) ||
|
||||
(rr.length > 1 && rr[0] instanceof AST_String && rr[1] && !self.left.has_side_effects(compressor))) {
|
||||
if (ll.length > 1 && ll[0] instanceof AST_String && ll[1]) {
|
||||
compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
|
||||
return make_node(AST_True, self);
|
||||
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]) {
|
||||
compressor.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
|
||||
return make_node(AST_Seq, self, {
|
||||
car: self.left,
|
||||
cdr: make_node(AST_True, self)
|
||||
}).optimize(compressor);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -3280,7 +3291,8 @@ merge(Compressor.prototype, {
|
||||
&& alternative instanceof AST_Assign
|
||||
&& consequent.operator == alternative.operator
|
||||
&& consequent.left.equivalent_to(alternative.left)
|
||||
&& !consequent.left.has_side_effects(compressor)
|
||||
&& (!consequent.left.has_side_effects(compressor)
|
||||
|| !self.condition.has_side_effects(compressor))
|
||||
) {
|
||||
/*
|
||||
* Stuff like this:
|
||||
@@ -3298,25 +3310,19 @@ merge(Compressor.prototype, {
|
||||
})
|
||||
});
|
||||
}
|
||||
// x ? y(a) : y(b) --> y(x ? a : b)
|
||||
if (consequent instanceof AST_Call
|
||||
&& alternative.TYPE === consequent.TYPE
|
||||
&& consequent.args.length == alternative.args.length
|
||||
&& !consequent.expression.has_side_effects(compressor)
|
||||
&& consequent.expression.equivalent_to(alternative.expression)) {
|
||||
if (consequent.args.length == 0) {
|
||||
return make_node(AST_Seq, self, {
|
||||
car: self.condition,
|
||||
cdr: consequent
|
||||
});
|
||||
}
|
||||
if (consequent.args.length == 1) {
|
||||
consequent.args[0] = make_node(AST_Conditional, self, {
|
||||
condition: self.condition,
|
||||
consequent: consequent.args[0],
|
||||
alternative: alternative.args[0]
|
||||
});
|
||||
return consequent;
|
||||
}
|
||||
&& consequent.args.length == 1
|
||||
&& alternative.args.length == 1
|
||||
&& consequent.expression.equivalent_to(alternative.expression)
|
||||
&& !consequent.expression.has_side_effects(compressor)) {
|
||||
consequent.args[0] = make_node(AST_Conditional, self, {
|
||||
condition: self.condition,
|
||||
consequent: consequent.args[0],
|
||||
alternative: alternative.args[0]
|
||||
});
|
||||
return consequent;
|
||||
}
|
||||
// x?y?z:a:a --> x&&y?z:a
|
||||
if (consequent instanceof AST_Conditional
|
||||
@@ -3331,16 +3337,12 @@ merge(Compressor.prototype, {
|
||||
alternative: alternative
|
||||
});
|
||||
}
|
||||
// y?1:1 --> 1
|
||||
if (consequent.is_constant()
|
||||
&& alternative.is_constant()
|
||||
&& consequent.equivalent_to(alternative)) {
|
||||
var consequent_value = consequent.evaluate(compressor)[0];
|
||||
if (self.condition.has_side_effects(compressor)) {
|
||||
return AST_Seq.from_array([self.condition, consequent_value]);
|
||||
} else {
|
||||
return consequent_value;
|
||||
}
|
||||
// x ? y : y --> x, y
|
||||
if (consequent.equivalent_to(alternative)) {
|
||||
return make_node(AST_Seq, self, {
|
||||
car: self.condition,
|
||||
cdr: consequent
|
||||
}).optimize(compressor);
|
||||
}
|
||||
|
||||
if (is_true(self.consequent)) {
|
||||
@@ -3499,8 +3501,12 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
function literals_in_boolean_context(self, compressor) {
|
||||
if (compressor.option("booleans") && compressor.in_boolean_context() && !self.has_side_effects(compressor)) {
|
||||
return make_node(AST_True, self);
|
||||
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, {
|
||||
car: self,
|
||||
cdr: make_node(AST_True, self)
|
||||
}).optimize(compressor));
|
||||
}
|
||||
return self;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user