more small optimizations

(unlikely to help for hand-written code)
This commit is contained in:
Mihai Bazon
2012-10-17 21:57:08 +03:00
parent 6a099fba66
commit 253bd8559b

View File

@@ -162,6 +162,9 @@ merge(Compressor.prototype, {
if (val === null) { if (val === null) {
return make_node(AST_Null, orig).optimize(compressor); return make_node(AST_Null, orig).optimize(compressor);
} }
if (val instanceof RegExp) {
return make_node(AST_RegExp, orig).optimize(compressor);
}
throw new Error(string_template("Can't handle constant of type: {type}", { throw new Error(string_template("Can't handle constant of type: {type}", {
type: typeof val type: typeof val
})); }));
@@ -1109,7 +1112,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 }); return make_node(AST_BlockStatement, self, { body: a }).transform(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);
@@ -1117,7 +1120,7 @@ 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 }); return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);
} }
} }
} }
@@ -1134,7 +1137,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);
} }
if (self.body instanceof AST_SimpleStatement if (self.body instanceof AST_SimpleStatement
&& self.alternative instanceof AST_SimpleStatement) { && self.alternative instanceof AST_SimpleStatement) {
@@ -1144,7 +1147,7 @@ merge(Compressor.prototype, {
consequent : self.body.body, consequent : self.body.body,
alternative : self.alternative.body alternative : self.alternative.body
}) })
}); }).transform(compressor);
} }
if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) { if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) {
if (negated_is_best) return make_node(AST_SimpleStatement, self, { if (negated_is_best) return make_node(AST_SimpleStatement, self, {
@@ -1153,14 +1156,14 @@ merge(Compressor.prototype, {
left : negated, left : negated,
right : self.body.body right : self.body.body
}) })
}); }).transform(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 : self.body.body right : self.body.body
}) })
}); }).transform(compressor);
} }
if (self.body instanceof AST_EmptyStatement if (self.body instanceof AST_EmptyStatement
&& self.alternative && self.alternative
@@ -1171,7 +1174,7 @@ merge(Compressor.prototype, {
left : self.condition, left : self.condition,
right : self.alternative.body right : self.alternative.body
}) })
}); }).transform(compressor);
} }
if (self.body instanceof AST_Exit if (self.body instanceof AST_Exit
&& self.alternative instanceof AST_Exit && self.alternative instanceof AST_Exit
@@ -1182,7 +1185,7 @@ merge(Compressor.prototype, {
consequent : self.body.value, consequent : self.body.value,
alternative : self.alternative.value || make_node(AST_Undefined, self).optimize(compressor) alternative : self.alternative.value || make_node(AST_Undefined, self).optimize(compressor)
}) })
}); }).transform(compressor);
} }
if (self.body instanceof AST_If if (self.body instanceof AST_If
&& !self.body.alternative && !self.body.alternative
@@ -1200,7 +1203,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);
} }
} }
if (aborts(self.alternative)) { if (aborts(self.alternative)) {
@@ -1210,12 +1213,17 @@ 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);
} }
return self; return self;
}); });
OPT(AST_Switch, function(self, compressor){ OPT(AST_Switch, function(self, compressor){
if (self.body.length == 0 && compressor.option("conditionals")) {
return make_node(AST_SimpleStatement, self, {
body: self.expression
}).transform(compressor);
}
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
@@ -1303,14 +1311,14 @@ merge(Compressor.prototype, {
left: exp.expression, left: exp.expression,
operator: "+", operator: "+",
right: make_node(AST_String, self, { value: "" }) right: make_node(AST_String, self, { value: "" })
}); }).transform(compressor);
} }
} }
if (compressor.option("side_effects")) { if (compressor.option("side_effects")) {
if (self.expression instanceof AST_Function if (self.expression instanceof AST_Function
&& self.args.length == 0 && self.args.length == 0
&& !self.expression.has_side_effects()) { && !self.expression.has_side_effects()) {
return make_node(AST_Undefined, self); return make_node(AST_Undefined, self).transform(compressor);
} }
} }
return self; return self;
@@ -1614,4 +1622,14 @@ merge(Compressor.prototype, {
return self; return self;
}); });
function literals_in_boolean_context(self, compressor) {
if (compressor.option("booleans") && compressor.in_boolean_context()) {
return make_node(AST_True, self);
}
return self;
};
OPT(AST_Array, literals_in_boolean_context);
OPT(AST_Object, literals_in_boolean_context);
OPT(AST_RegExp, literals_in_boolean_context);
})(); })();