diff options
author | Mihai Bazon <mihai@bazon.net> | 2012-10-02 14:32:30 +0300 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2012-10-02 14:32:30 +0300 |
commit | 2a5456260e01af2228356edfb6e31e3946fcbd91 (patch) | |
tree | c95ded579c501bba43881920dc1fee873c0159ba /lib/output.js | |
parent | 36be211e99ec7c65e156499d53f471a1e55a6fd1 (diff) | |
download | tracifyjs-2a5456260e01af2228356edfb6e31e3946fcbd91.tar.gz tracifyjs-2a5456260e01af2228356edfb6e31e3946fcbd91.zip |
added option to keep some comments in the output
Diffstat (limited to 'lib/output.js')
-rw-r--r-- | lib/output.js | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/lib/output.js b/lib/output.js index e5531f55..d36c9578 100644 --- a/lib/output.js +++ b/lib/output.js @@ -58,6 +58,7 @@ function OutputStream(options) { beautify : false, source_map : null, bracketize : false, + comments : false }, true); var indentation = 0; @@ -309,10 +310,12 @@ function OutputStream(options) { stream.push_node(self); if (self.needs_parens(stream)) { stream.with_parens(function(){ + self.add_comments(stream); self.add_source_map(stream); generator(self, stream); }); } else { + self.add_comments(stream); self.add_source_map(stream); generator(self, stream); } @@ -326,12 +329,49 @@ function OutputStream(options) { return s.get(); }); + /* -----[ comments ]----- */ + + AST_Node.DEFMETHOD("add_comments", function(output){ + var c = output.option("comments"), self = this; + if (c) { + var start = self.start; + if (start && !start._comments_dumped) { + start._comments_dumped = true; + var comments = start.comments_before; + if (c.test) { + comments = comments.filter(function(comment){ + return c.test(comment.value); + }); + } else if (typeof c == "function") { + comments = comments.filter(function(comment){ + return c(self, comment.value, comment.type); + }); + } + comments.forEach(function(c){ + if (c.type == "comment1") { + output.print("//" + c.value + "\n"); + output.indent(); + } + else if (c.type == "comment2") { + output.print("/*" + c.value + "*/"); + if (start.nlb) { + output.print("\n"); + output.indent(); + } else { + output.space(); + } + } + }); + } + } + }); + + /* -----[ PARENTHESES ]----- */ + function PARENS(nodetype, func) { nodetype.DEFMETHOD("needs_parens", func); }; - /* -----[ PARENTHESES ]----- */ - PARENS(AST_Node, function(){ return false; }); |