aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMihai Bazon <mihai@bazon.net>2012-11-13 14:32:07 +0200
committerMihai Bazon <mihai@bazon.net>2012-11-13 14:32:07 +0200
commite02771a5f2e23f7d75242a70729dbc8216bb4bfb (patch)
tree73a2b6bc0a145aa9e4116f547b2cf0a74006af01 /lib
parenta9fa178f86b36959ea9b86b591e22f4406aa7ce3 (diff)
downloadtracifyjs-e02771a5f2e23f7d75242a70729dbc8216bb4bfb.tar.gz
tracifyjs-e02771a5f2e23f7d75242a70729dbc8216bb4bfb.zip
don't change order in binary expressions if both operands have side effects
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js32
1 files changed, 23 insertions, 9 deletions
diff --git a/lib/compress.js b/lib/compress.js
index a4a53f5a..dcd376ca 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -554,12 +554,24 @@ merge(Compressor.prototype, {
def(AST_UnaryPrefix, function(){
return this.operator == "typeof";
});
- def(AST_Binary, function(){
+ def(AST_Binary, function(compressor){
return this.operator == "+" &&
- (this.left.is_string() || this.right.is_string());
+ (this.left.is_string(compressor) || this.right.is_string(compressor));
});
- def(AST_Assign, function(){
- return this.operator == "=" && this.right.is_string();
+ def(AST_Assign, function(compressor){
+ return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
+ });
+ def(AST_Seq, function(compressor){
+ return this.cdr.is_string(compressor);
+ });
+ def(AST_Conditional, function(compressor){
+ return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
+ });
+ def(AST_Call, function(compressor){
+ return compressor.option("unsafe")
+ && this.expression instanceof AST_SymbolRef
+ && this.expression.name == "String"
+ && this.expression.undeclared();
});
})(function(node, func){
node.DEFMETHOD("is_string", func);
@@ -1549,10 +1561,12 @@ merge(Compressor.prototype, {
OPT(AST_Binary, function(self, compressor){
function reverse(op) {
- if (op) self.operator = op;
- var tmp = self.left;
- self.left = self.right;
- self.right = tmp;
+ if (!(self.left.has_side_effects() && self.right.has_side_effects())) {
+ if (op) self.operator = op;
+ var tmp = self.left;
+ self.left = self.right;
+ self.right = tmp;
+ }
};
if (commutativeOperators(self.operator)) {
if (self.right instanceof AST_Constant
@@ -1564,7 +1578,7 @@ merge(Compressor.prototype, {
if (compressor.option("comparisons")) switch (self.operator) {
case "===":
case "!==":
- if ((self.left.is_string() && self.right.is_string()) ||
+ if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
(self.left.is_boolean() && self.right.is_boolean())) {
self.operator = self.operator.substr(0, 2);
}