diff options
author | Mihai Bazon <mihai@bazon.net> | 2012-09-25 18:03:31 +0300 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2012-09-25 20:39:15 +0300 |
commit | 242dd101313e1698b731954f551bef9014cc9b0f (patch) | |
tree | 3d17e929179356c11ffdf07c631f7f4346397881 /lib | |
parent | 9321d418bc76b299bfcc627f6dafb49df1832881 (diff) | |
download | tracifyjs-242dd101313e1698b731954f551bef9014cc9b0f.tar.gz tracifyjs-242dd101313e1698b731954f551bef9014cc9b0f.zip |
more cleanups
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 182 |
1 files changed, 55 insertions, 127 deletions
diff --git a/lib/compress.js b/lib/compress.js index c44114b6..d2e530bb 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -117,10 +117,6 @@ function Compressor(options, false_by_default) { (function(undefined){ - AST_Node.DEFMETHOD("squeeze", function(){ - return this; - }); - AST_Node.DEFMETHOD("optimize", function(){ return this; }); @@ -137,12 +133,6 @@ function Compressor(options, false_by_default) { return new ctor(props); }; - function do_list(array, compressor) { - return MAP(array, function(node){ - return node.squeeze(compressor); - }); - }; - function as_statement_array(thing) { if (thing === null) return []; if (thing instanceof AST_BlockStatement) return thing.body; @@ -948,7 +938,6 @@ function Compressor(options, false_by_default) { } ); self = self.transform(tt); - if (vars_found > 0) hoisted.unshift(make_node(AST_Var, self, { definitions: Object.keys(vars).map(function(name){ var def = vars[name].clone(); @@ -974,7 +963,7 @@ function Compressor(options, false_by_default) { var cond = self.condition.evaluate(compressor); self.condition = cond[0]; if (!compressor.option("loops")) return self; - if (cond.length == 2) { + if (cond.length > 1) { if (cond[1]) { return make_node(AST_For, self, { body: self.body @@ -1015,7 +1004,7 @@ function Compressor(options, false_by_default) { } if (!compressor.option("loops")) return this; if (cond) { - if (cond.length == 2 && !cond[1]) { + if (cond.length > 1 && !cond[1]) { if (compressor.option("dead_code")) { var a = []; if (this.init instanceof AST_Statement) { @@ -1043,7 +1032,7 @@ function Compressor(options, false_by_default) { // `x && true`, though it probably should. var cond = self.condition.evaluate(compressor); self.condition = cond[0]; - if (cond.length == 2) { + if (cond.length > 1) { if (cond[1]) { compressor.warn("Condition always true [{file}:{line},{col}]", self.condition.start); if (compressor.option("dead_code")) { @@ -1362,48 +1351,55 @@ function Compressor(options, false_by_default) { case "&&": var ll = this.left.evaluate(compressor), left = ll[0]; var rr = this.right.evaluate(compressor), right = rr[0]; - if ((ll.length == 2 && !ll[1]) || (rr.length == 2 && !rr[1])) { + if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) { compressor.warn("Boolean && always false [{file}:{line},{col}]", this.start); return make_node(AST_False, this).optimize(compressor); } - if (ll.length == 2 && ll[1]) { + if (ll.length > 1 && ll[1]) { return rr[0]; } - if (rr.length == 2 && rr[1]) { + if (rr.length > 1 && rr[1]) { return ll[0]; } break; case "||": var ll = this.left.evaluate(compressor), left = ll[0]; var rr = this.right.evaluate(compressor), right = rr[0]; - if ((ll.length == 2 && ll[1]) || (rr.length == 2 && rr[1])) { + if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) { compressor.warn("Boolean || always true [{file}:{line},{col}]", this.start); return make_node(AST_True, this).optimize(compressor); } - if (ll.length == 2 && !ll[1]) { + if (ll.length > 1 && !ll[1]) { return rr[0]; } - if (rr.length == 2 && !rr[1]) { + if (rr.length > 1 && !rr[1]) { return ll[0]; } break; case "+": var ll = this.left.evaluate(compressor), left = ll[0]; var rr = this.right.evaluate(compressor), right = rr[0]; - if ((ll.length == 2 && ll[0] instanceof AST_String && ll[1]) || - (rr.length == 2 && rr[0] instanceof AST_String && rr[1])) { + if ((ll.length > 1 && ll[0] instanceof AST_String && ll[1]) || + (rr.length > 1 && rr[0] instanceof AST_String && rr[1])) { compressor.warn("+ in boolean context always true [{file}:{line},{col}]", this.start); return make_node(AST_True, this).optimize(compressor); } break; } var exp = this.evaluate(compressor); - if (exp.length == 2) { + if (exp.length > 1) { if (best_of(exp[0], this) !== this) return exp[0]; } var self = this; if (compressor.option("comparations")) { + if (!(compressor.parent() instanceof AST_Binary)) { + var negated = make_node(AST_UnaryPrefix, self, { + operator: "!", + expression: self.negate(compressor) + }); + self = best_of(self, negated); + } var reverse = function(op) { self.operator = op; var tmp = self.left; @@ -1414,13 +1410,6 @@ function Compressor(options, false_by_default) { case "<": reverse(">"); break; case "<=": reverse(">="); break; } - if (!(compressor.parent() instanceof AST_Binary)) { - var negated = make_node(AST_UnaryPrefix, self, { - operator: "!", - expression: self.negate(compressor) - }); - self = best_of(self, negated); - } } return self; }); @@ -1475,7 +1464,7 @@ function Compressor(options, false_by_default) { return AST_Seq.cons(car, self.optimize(compressor)).optimize(compressor); } var cond = self.condition.evaluate(compressor); - if (cond.length == 2) { + if (cond.length > 1) { if (cond[1]) { compressor.warn("Condition always true [{file}:{line},{col}]", self.start); return self.consequent; @@ -1544,248 +1533,187 @@ function Compressor(options, false_by_default) { return this; }); + AST_Sub.DEFMETHOD("optimize", function(compressor){ + var prop = this.property; + if (prop instanceof AST_String && compressor.option("properties")) { + prop = prop.getValue(); + if (is_identifier(prop)) { + return make_node(AST_Dot, this, { + expression : this.expression, + property : prop + }); + } + } + return this; + }); + /* -----[ node squeezers ]----- */ function SQUEEZE(nodetype, squeeze) { nodetype.DEFMETHOD("squeeze", function(compressor){ compressor.push_node(this); - var new_node = squeeze(this, compressor); + var self = this.clone(), opt; + opt = squeeze(self, compressor); + if (opt !== undefined) self = opt; + opt = self.optimize(compressor); + if (opt !== undefined) self = opt; compressor.pop_node(); - return new_node !== undefined ? new_node : this; + return self; }); }; - SQUEEZE(AST_Directive, function(self, compressor){ - return self.optimize(compressor); - }); + function do_list(array, compressor) { + return MAP(array, function(node){ + return node.squeeze(compressor); + }); + }; - SQUEEZE(AST_Debugger, function(self, compressor){ - return self.optimize(compressor); - }); + SQUEEZE(AST_Node, noop); SQUEEZE(AST_LabeledStatement, function(self, compressor){ - self = self.clone(); self.body = self.body.squeeze(compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Statement, function(self, compressor){ - self = self.clone(); - self.body = self.body.squeeze(compressor); - return self; + if (self.body) self.body = self.body.squeeze(compressor); }); SQUEEZE(AST_BlockStatement, function(self, compressor){ - self = self.clone(); self.body = do_list(self.body, compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Block, function(self, compressor){ - self = self.clone(); self.body = do_list(self.body, compressor); - return self.optimize(compressor); - }); - - SQUEEZE(AST_Scope, function(self, compressor){ - self = self.clone().hoist_declarations(compressor); - self.body = do_list(self.body, compressor); - return self.optimize(compressor); }); SQUEEZE(AST_SimpleStatement, function(self, compressor){ - self = self.clone(); self.body = self.body.squeeze(compressor); - return self.optimize(compressor); - }); - - SQUEEZE(AST_EmptyStatement, function(self, compressor){ - return self; }); SQUEEZE(AST_DWLoop, function(self, compressor){ - self = self.clone(); self.condition = self.condition.squeeze(compressor); self.body = self.body.squeeze(compressor); - return self.optimize(compressor); }); SQUEEZE(AST_For, function(self, compressor){ - self = self.clone(); if (self.init) self.init = self.init.squeeze(compressor); if (self.condition) self.condition = self.condition.squeeze(compressor); if (self.step) self.step = self.step.squeeze(compressor); self.body = self.body.squeeze(compressor); - return self.optimize(compressor); }); SQUEEZE(AST_ForIn, function(self, compressor){ - self = self.clone(); self.init = self.init.squeeze(compressor); self.object = self.object.squeeze(compressor); self.body = self.body.squeeze(compressor); - return self; }); SQUEEZE(AST_With, function(self, compressor){ - self = self.clone(); self.expression = self.expression.squeeze(compressor); self.body = self.body.squeeze(compressor); - return self; }); SQUEEZE(AST_Exit, function(self, compressor){ - self = self.clone(); if (self.value) self.value = self.value.squeeze(compressor); - return self; }); SQUEEZE(AST_LoopControl, function(self, compressor){ - self = self.clone(); if (self.label) self.label = self.label.squeeze(compressor); - return self; }); SQUEEZE(AST_If, function(self, compressor){ - self = self.clone(); self.condition = self.condition.squeeze(compressor); self.body = self.body.squeeze(compressor); if (self.alternative) self.alternative = self.alternative.squeeze(compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Switch, function(self, compressor){ - self = self.clone(); self.expression = self.expression.squeeze(compressor); self.body = self.body.squeeze(compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Case, function(self, compressor){ - self = self.clone(); self.expression = self.expression.squeeze(compressor); self.body = do_list(self.body, compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Try, function(self, compressor){ - self = self.clone(); self.body = do_list(self.body, compressor); if (self.bcatch) self.bcatch = self.bcatch.squeeze(compressor); if (self.bfinally) self.bfinally = self.bfinally.squeeze(compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Definitions, function(self, compressor){ - self = self.clone(); self.definitions = do_list(self.definitions, compressor); - return self.optimize(compressor); }); SQUEEZE(AST_VarDef, function(self, compressor){ - self = self.clone(); if (self.value) self.value = self.value.squeeze(compressor); + }); + + SQUEEZE(AST_Scope, function(self, compressor){ + self = self.hoist_declarations(compressor); + self.body = do_list(self.body, compressor); return self; }); SQUEEZE(AST_Lambda, function(self, compressor){ - self = self.clone().hoist_declarations(compressor); + self = self.hoist_declarations(compressor); if (self.name) self.name = self.name.squeeze(compressor); self.argnames = do_list(self.argnames, compressor); self.body = do_list(self.body, compressor); - return self.optimize(compressor); + return self; }); SQUEEZE(AST_Call, function(self, compressor){ - self = self.clone(); self.expression = self.expression.squeeze(compressor); self.args = do_list(self.args, compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Seq, function(self, compressor){ - self = self.clone(); self.car = self.car.squeeze(compressor); self.cdr = self.cdr.squeeze(compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Dot, function(self, compressor){ - self = self.clone(); self.expression = self.expression.squeeze(compressor); - return self; }); SQUEEZE(AST_Sub, function(self, compressor){ - self = self.clone(); self.expression = self.expression.squeeze(compressor); - var prop = self.property = self.property.squeeze(compressor); - if (prop instanceof AST_String && compressor.option("properties")) { - prop = prop.getValue(); - if (is_identifier(prop)) { - self = new AST_Dot(self); - self.property = prop; - } - } - return self; + self.property = self.property.squeeze(compressor); }); SQUEEZE(AST_Unary, function(self, compressor){ - self = self.clone(); self.expression = self.expression.squeeze(compressor); - return self; }); SQUEEZE(AST_UnaryPrefix, function(self, compressor){ - self = self.clone(); self.expression = self.expression.squeeze(compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Binary, function(self, compressor){ - self = self.clone(); self.left = self.left.squeeze(compressor); self.right = self.right.squeeze(compressor); - return self.optimize(compressor); - }); - - SQUEEZE(AST_SymbolRef, function(self, compressor){ - return self.optimize(compressor); - }); - - SQUEEZE(AST_Undefined, function(self, compressor){ - return self.optimize(compressor); }); SQUEEZE(AST_Conditional, function(self, compressor){ - self = self.clone(); self.condition = self.condition.squeeze(compressor); self.consequent = self.consequent.squeeze(compressor); self.alternative = self.alternative.squeeze(compressor); - return self.optimize(compressor); }); SQUEEZE(AST_Array, function(self, compressor){ - self = self.clone(); self.elements = do_list(self.elements, compressor); - return self; }); SQUEEZE(AST_Object, function(self, compressor){ - self = self.clone(); self.properties = do_list(self.properties, compressor); - return self; }); SQUEEZE(AST_ObjectProperty, function(self, compressor){ - self = self.clone(); self.value = self.value.squeeze(compressor); - return self; - }); - - SQUEEZE(AST_Boolean, function(self, compressor){ - return self.optimize(compressor); }); })(); |