return true;
return false;
},
+ in_boolean_context: function() {
+ if (!this.option("booleans")) return false;
+ var self = this.self();
+ for (var i = 0, p; p = this.parent(i); i++) {
+ if (p instanceof AST_SimpleStatement
+ || p instanceof AST_Conditional && p.condition === self
+ || p instanceof AST_DWLoop && p.condition === self
+ || p instanceof AST_For && p.condition === self
+ || p instanceof AST_If && p.condition === self
+ || p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self) {
+ return true;
+ }
+ if (p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")
+ || p.tail_node() === self) {
+ self = p;
+ } else {
+ return false;
+ }
+ }
+ },
compress: function(node) {
if (this.option("expression")) {
node.process_expression(true);
|| this.alternative._dot_throw(compressor);
})
def(AST_Sequence, function(compressor) {
- return this.expressions[this.expressions.length - 1]._dot_throw(compressor);
+ return this.tail_node()._dot_throw(compressor);
});
def(AST_SymbolRef, function(compressor) {
if (this.is_undefined) return true;
return this.operator == "=" && this.right.is_boolean();
});
def(AST_Sequence, function(){
- return this.expressions[this.expressions.length - 1].is_boolean();
+ return this.tail_node().is_boolean();
});
def(AST_True, return_true);
def(AST_False, return_true);
|| this.operator == "=" && this.right.is_number(compressor);
});
def(AST_Sequence, function(compressor){
- return this.expressions[this.expressions.length - 1].is_number(compressor);
+ return this.tail_node().is_number(compressor);
});
def(AST_Conditional, function(compressor){
return this.consequent.is_number(compressor) && this.alternative.is_number(compressor);
return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
});
def(AST_Sequence, function(compressor){
- return this.expressions[this.expressions.length - 1].is_string(compressor);
+ return this.tail_node().is_string(compressor);
});
def(AST_Conditional, function(compressor){
return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
return make_sequence(this, [ expression, property ]);
});
def(AST_Sequence, function(compressor){
- var last = this.expressions[this.expressions.length - 1];
+ var last = this.tail_node();
var expr = last.drop_side_effect_free(compressor);
if (expr === last) return this;
var expressions = this.expressions.slice(0, -1);
return make_node(AST_Undefined, self).optimize(compressor);
}
}
- if (compressor.option("booleans") && compressor.in_boolean_context()) {
+ if (compressor.in_boolean_context()) {
switch (self.operator) {
case "!":
if (e instanceof AST_UnaryPrefix && e.operator == "!") {
}
break;
}
- if (compressor.option("booleans") && self.operator == "+" && compressor.in_boolean_context()) {
+ if (self.operator == "+" && compressor.in_boolean_context()) {
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if (ll && typeof ll == "string") {
compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), compressor.self(), self.right).optimize(compressor);
}
- if (compressor.option("booleans") && compressor.in_boolean_context()) {
+ if (compressor.in_boolean_context()) {
var rr = self.right.evaluate(compressor);
if (!rr) {
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start);
return maintain_this_binding(compressor.parent(), compressor.self(), self.left).optimize(compressor);
}
- if (compressor.option("booleans") && compressor.in_boolean_context()) {
+ if (compressor.in_boolean_context()) {
var rr = self.right.evaluate(compressor);
if (!rr) {
compressor.warn("Dropping side-effect-free || in boolean context [{file}:{line},{col}]", self.start);
});
function literals_in_boolean_context(self, compressor) {
- if (compressor.option("booleans") && compressor.in_boolean_context()) {
+ if (compressor.in_boolean_context()) {
return best_of(compressor, self, make_sequence(self, [
self,
make_node(AST_True, self)