diff options
author | Mihai Bazon <mihai@bazon.net> | 2012-10-17 21:57:08 +0300 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2012-10-17 21:57:08 +0300 |
commit | 253bd8559bdc3b681fb665704718bc024f5d316e (patch) | |
tree | 29ac1e067555f77ebc7f963eee399091fb91a508 /lib | |
parent | 6a099fba66eba896755f5a723a9d08034faee615 (diff) | |
download | tracifyjs-253bd8559bdc3b681fb665704718bc024f5d316e.tar.gz tracifyjs-253bd8559bdc3b681fb665704718bc024f5d316e.zip |
more small optimizations
(unlikely to help for hand-written code)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/lib/compress.js b/lib/compress.js index d2e3ffc7..1af4a2d6 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -162,6 +162,9 @@ merge(Compressor.prototype, { if (val === null) { 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}", { type: typeof val })); @@ -1109,7 +1112,7 @@ merge(Compressor.prototype, { extract_declarations_from_unreachable_code(compressor, self.alternative, a); } a.push(self.body); - return make_node(AST_BlockStatement, self, { body: a }); + return make_node(AST_BlockStatement, self, { body: a }).transform(compressor); } } else { compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start); @@ -1117,7 +1120,7 @@ merge(Compressor.prototype, { var a = []; extract_declarations_from_unreachable_code(compressor, self.body, a); 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)) { return make_node(AST_SimpleStatement, self.condition, { body: self.condition - }); + }).transform(compressor); } if (self.body instanceof AST_SimpleStatement && self.alternative instanceof AST_SimpleStatement) { @@ -1144,7 +1147,7 @@ merge(Compressor.prototype, { consequent : self.body.body, alternative : self.alternative.body }) - }); + }).transform(compressor); } if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) { if (negated_is_best) return make_node(AST_SimpleStatement, self, { @@ -1153,14 +1156,14 @@ merge(Compressor.prototype, { left : negated, right : self.body.body }) - }); + }).transform(compressor); return make_node(AST_SimpleStatement, self, { body: make_node(AST_Binary, self, { operator : "&&", left : self.condition, right : self.body.body }) - }); + }).transform(compressor); } if (self.body instanceof AST_EmptyStatement && self.alternative @@ -1171,7 +1174,7 @@ merge(Compressor.prototype, { left : self.condition, right : self.alternative.body }) - }); + }).transform(compressor); } if (self.body instanceof AST_Exit && self.alternative instanceof AST_Exit @@ -1182,7 +1185,7 @@ merge(Compressor.prototype, { consequent : self.body.value, alternative : self.alternative.value || make_node(AST_Undefined, self).optimize(compressor) }) - }); + }).transform(compressor); } if (self.body instanceof AST_If && !self.body.alternative @@ -1200,7 +1203,7 @@ merge(Compressor.prototype, { self.alternative = null; return make_node(AST_BlockStatement, self, { body: [ self, alt ] - }); + }).transform(compressor); } } if (aborts(self.alternative)) { @@ -1210,12 +1213,17 @@ merge(Compressor.prototype, { self.alternative = null; return make_node(AST_BlockStatement, self, { body: [ self, body ] - }); + }).transform(compressor); } return self; }); 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]; if (last_branch) { var stat = last_branch.body[last_branch.body.length - 1]; // last statement @@ -1303,14 +1311,14 @@ merge(Compressor.prototype, { left: exp.expression, operator: "+", right: make_node(AST_String, self, { value: "" }) - }); + }).transform(compressor); } } if (compressor.option("side_effects")) { if (self.expression instanceof AST_Function && self.args.length == 0 && !self.expression.has_side_effects()) { - return make_node(AST_Undefined, self); + return make_node(AST_Undefined, self).transform(compressor); } } return self; @@ -1614,4 +1622,14 @@ merge(Compressor.prototype, { 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); + })(); |