From a24e7ee976787915c4d8f232bc980174674edb0b Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Wed, 26 Sep 2012 12:16:16 +0300 Subject: [PATCH] checkpoint (refactoring, WIP) --- lib/ast.js | 36 +++++++++++++++++++++++++++- lib/compress.js | 62 ++++++++++++------------------------------------ lib/transform.js | 15 ++---------- 3 files changed, 52 insertions(+), 61 deletions(-) diff --git a/lib/ast.js b/lib/ast.js index 3291ed83..3e6e1771 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -709,5 +709,39 @@ TreeWalker.prototype = { }, parent: function(n) { return this.stack[this.stack.length - 2 - (n || 0)]; - } + }, + push: function (node) { + this.stack.push(node); + }, + pop: function() { + return this.stack.pop(); + }, + self: function() { + return this.stack[this.stack.length - 1]; + }, + find_parent: function(type) { + var stack = this.stack; + for (var i = stack.length; --i >= 0;) { + var x = stack[i]; + if (x instanceof type) return x; + } + }, + in_boolean_context: function() { + var stack = this.stack; + var i = stack.length, self = stack[--i]; + while (i > 0) { + var p = stack[--i]; + if ((p instanceof AST_If && p.condition === self) || + (p instanceof AST_Conditional && p.condition === self) || + (p instanceof AST_DWLoop && p.condition === self) || + (p instanceof AST_For && p.condition === self) || + (p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self)) + { + return true; + } + if (!(p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||"))) + return false; + self = p; + } + }, }; diff --git a/lib/compress.js b/lib/compress.js index d2e530bb..4161842d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -53,7 +53,10 @@ // squeezing nodes. function Compressor(options, false_by_default) { - options = defaults(options, { + if (!(this instanceof Compressor)) + return new Compressor(options, false_by_default); + TreeTransformer.call(this, this.before, this.after); + this.options = defaults(options, { sequences : !false_by_default, properties : !false_by_default, dead_code : !false_by_default, @@ -73,48 +76,17 @@ function Compressor(options, false_by_default) { warnings : true }); - var stack = []; - function in_boolean_context() { - var i = stack.length, self = stack[--i]; - while (i > 0) { - var p = stack[--i]; - if ((p instanceof AST_If && p.condition === self) || - (p instanceof AST_Conditional && p.condition === self) || - (p instanceof AST_DWLoop && p.condition === self) || - (p instanceof AST_For && p.condition === self) || - (p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self)) - { - return true; - } - if (!(p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||"))) - return false; - self = p; - } - }; - function find_parent(type) { - for (var i = stack.length; --i >= 0;) { - var x = stack[i]; - if (x instanceof type) return x; - } - }; - return { - option : function(key) { return options[key] }, - push_node : function(node) { stack.push(node) }, - pop_node : function() { return stack.pop() }, - stack : function() { return stack }, - self : function() { return stack[stack.length - 1] }, - parent : function(n) { - return stack[stack.length - 2 - (n || 0)]; - }, - warn : function() { - if (options.warnings) - AST_Node.warn.apply(AST_Node, arguments); - }, - in_boolean_context: in_boolean_context, - find_parent: find_parent, - }; }; +Compressor.prototype = new TreeTransformer; +defaults(Compressor.prototype, { + option: function(key) { return this.options[key] }, + warn: function() { + if (this.options.warnings) + AST_Node.warn.apply(AST_Node, arguments); + } +}); + (function(undefined){ AST_Node.DEFMETHOD("optimize", function(){ @@ -1551,13 +1523,13 @@ function Compressor(options, false_by_default) { function SQUEEZE(nodetype, squeeze) { nodetype.DEFMETHOD("squeeze", function(compressor){ - compressor.push_node(this); + compressor.push(this); 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(); + compressor.pop(); return self; }); }; @@ -1689,10 +1661,6 @@ function Compressor(options, false_by_default) { self.expression = self.expression.squeeze(compressor); }); - SQUEEZE(AST_UnaryPrefix, function(self, compressor){ - self.expression = self.expression.squeeze(compressor); - }); - SQUEEZE(AST_Binary, function(self, compressor){ self.left = self.left.squeeze(compressor); self.right = self.right.squeeze(compressor); diff --git a/lib/transform.js b/lib/transform.js index df51eac3..6f89679d 100644 --- a/lib/transform.js +++ b/lib/transform.js @@ -45,22 +45,11 @@ // XXX: eventually I should refactor the compressor to use this infrastructure. function TreeTransformer(before, after) { + TreeWalker.call(this); this.before = before; this.after = after; - this.stack = []; } - -TreeTransformer.prototype = { - push: function (node) { - this.stack.push(node); - }, - pop: function() { - return this.stack.pop(); - }, - parent: function (n) { - return this.stack[this.stack.length - 2 - (n || 0)]; - } -}; +TreeTransformer.prototype = new TreeWalker; (function(undefined){ -- 2.34.1