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 instanceof AST_Conditional
- || p.tail_node() === self) {
- self = p;
- } else {
- return false;
- }
- }
- },
compress: function(node) {
if (this.option("expression")) {
node.process_expression(true);
var args;
var candidates = [];
var stat_index = statements.length;
- var scanner = new TreeTransformer(function(node, descend) {
+ var scanner = new TreeTransformer(function(node) {
if (abort) return node;
// Skip nodes before `candidate` as quickly as possible
if (!hit) {
if (can_replace
&& !(node instanceof AST_SymbolDeclaration)
&& (scan_lhs && (hit_lhs = lhs.equivalent_to(node))
- || scan_rhs && (hit_rhs = rhs.equivalent_to(node)))) {
+ || scan_rhs && (hit_rhs = scan_rhs(node, this)))) {
if (stop_if_hit && (hit_rhs || !lhs_local || !replace_all)) {
abort = true;
return node;
}
function foldable(expr) {
- if (expr.is_constant()) return true;
- if (expr instanceof AST_Array) return false;
- if (expr instanceof AST_Function) return false;
- if (expr instanceof AST_Object) return false;
- if (expr instanceof AST_RegExp) return false;
- if (expr instanceof AST_Symbol) return true;
+ if (expr instanceof AST_SymbolRef) {
+ var value = expr.evaluate(compressor);
+ if (value === expr) return rhs_exact_match;
+ return rhs_fuzzy_match(value, rhs_exact_match);
+ }
+ if (expr instanceof AST_This) return rhs_exact_match;
+ if (expr.is_truthy()) return rhs_fuzzy_match(true, return_false);
+ if (expr.is_constant()) {
+ return rhs_fuzzy_match(expr.evaluate(compressor), rhs_exact_match);
+ }
if (!(lhs instanceof AST_SymbolRef)) return false;
if (expr.has_side_effects(compressor)) return false;
var circular;
circular = true;
}
}));
- return !circular;
+ return !circular && rhs_exact_match;
+ }
+
+ function rhs_exact_match(node) {
+ return rhs.equivalent_to(node);
+ }
+
+ function rhs_fuzzy_match(value, fallback) {
+ return function(node, tw) {
+ if (tw.in_boolean_context()) {
+ if (value && node.is_truthy() && !node.has_side_effects(compressor)) {
+ return true;
+ }
+ if (node.is_constant()) {
+ return !node.evaluate(compressor) == !value;
+ }
+ }
+ return fallback(node);
+ };
}
function get_lvalues(expr) {
}
break;
}
- if (self.operator == "+" && compressor.in_boolean_context()) {
+ if (compressor.option("booleans") && self.operator == "+" && compressor.in_boolean_context()) {
var ll = self.left.evaluate(compressor);
var rr = self.right.evaluate(compressor);
if (ll && typeof ll == "string") {
}
var rr = self.right.evaluate(compressor);
if (!rr) {
- if (compressor.in_boolean_context()) {
+ if (compressor.option("booleans") && compressor.in_boolean_context()) {
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
return make_sequence(self, [
self.left,
} else self.falsy = true;
} else if (!(rr instanceof AST_Node)) {
var parent = compressor.parent();
- if (parent.operator == "&&" && parent.left === compressor.self() || compressor.in_boolean_context()) {
+ if (parent.operator == "&&" && parent.left === compressor.self()
+ || compressor.option("booleans") && compressor.in_boolean_context()) {
compressor.warn("Dropping side-effect-free && [{file}:{line},{col}]", self.start);
return self.left.optimize(compressor);
}
var rr = self.right.evaluate(compressor);
if (!rr) {
var parent = compressor.parent();
- if (parent.operator == "||" && parent.left === compressor.self() || compressor.in_boolean_context()) {
+ if (parent.operator == "||" && parent.left === compressor.self()
+ || compressor.option("booleans") && compressor.in_boolean_context()) {
compressor.warn("Dropping side-effect-free || [{file}:{line},{col}]", self.start);
return self.left.optimize(compressor);
}
} else if (!(rr instanceof AST_Node)) {
- if (compressor.in_boolean_context()) {
+ if (compressor.option("booleans") && compressor.in_boolean_context()) {
compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
return make_sequence(self, [
self.left,
right: alternative
}).optimize(compressor);
}
- var in_bool = compressor.in_boolean_context();
+ var in_bool = compressor.option("booleans") && compressor.in_boolean_context();
if (is_true(self.consequent)) {
if (is_false(self.alternative)) {
// c ? true : false ---> !!c
});
OPT(AST_Boolean, function(self, compressor){
+ if (!compressor.option("booleans")) return self;
if (compressor.in_boolean_context()) return make_node(AST_Number, self, {
value: +self.value
});
- if (compressor.option("booleans")) {
- var p = compressor.parent();
- if (p instanceof AST_Binary && (p.operator == "=="
- || p.operator == "!=")) {
- compressor.warn("Non-strict equality against boolean: {operator} {value} [{file}:{line},{col}]", {
- operator : p.operator,
- value : self.value,
- file : p.start.file,
- line : p.start.line,
- col : p.start.col,
- });
- return make_node(AST_Number, self, {
- value: +self.value
- });
- }
- return make_node(AST_UnaryPrefix, self, {
- operator: "!",
- expression: make_node(AST_Number, self, {
- value: 1 - self.value
- })
+ var p = compressor.parent();
+ if (p instanceof AST_Binary && (p.operator == "==" || p.operator == "!=")) {
+ compressor.warn("Non-strict equality against boolean: {operator} {value} [{file}:{line},{col}]", {
+ operator : p.operator,
+ value : self.value,
+ file : p.start.file,
+ line : p.start.line,
+ col : p.start.col,
+ });
+ return make_node(AST_Number, self, {
+ value: +self.value
});
}
- return self;
+ return make_node(AST_UnaryPrefix, self, {
+ operator: "!",
+ expression: make_node(AST_Number, self, {
+ value: 1 - self.value
+ })
+ });
});
OPT(AST_Sub, function(self, compressor){