aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAnthony Van de Gejuchte <anthonyvdgent@gmail.com>2016-09-03 23:26:31 +0200
committerAnthony Van de Gejuchte <anthonyvdgent@gmail.com>2016-09-06 17:54:45 +0200
commit0111497fc98d5098f81bc749f77da5734add37bb (patch)
treec0d7e4da363639a4c68457d2f90e2eebc61796e8 /lib
parent7d8dea3b2675f9d86ea15bb031b7fe166858d67e (diff)
downloadtracifyjs-0111497fc98d5098f81bc749f77da5734add37bb.tar.gz
tracifyjs-0111497fc98d5098f81bc749f77da5734add37bb.zip
Make all comment options in cli available in js api
Also removing more code within "loop" while at it.
Diffstat (limited to 'lib')
-rw-r--r--lib/output.js59
1 files changed, 45 insertions, 14 deletions
diff --git a/lib/output.js b/lib/output.js
index 801f7516..a3c6b4ab 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -70,6 +70,49 @@ function OutputStream(options) {
keep_quoted_props: false
}, true);
+ // Convert comment option to RegExp if neccessary and set up comments filter
+ if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) {
+ var regex_pos = options.comments.lastIndexOf("/");
+ options.comments = new RegExp(
+ options.comments.substr(1, regex_pos - 1),
+ options.comments.substr(regex_pos + 1)
+ );
+ }
+ if (options.comments instanceof RegExp) {
+ options.comments = (function(f) {
+ return function(comment) {
+ return comment.type == "comment5" || f.test(comment.value);
+ }
+ })(options.comments);
+ }
+ else if (typeof options.comments === "function") {
+ options.comments = (function(f) {
+ return function(comment) {
+ return comment.type == "comment5" || f(this, comment);
+ }
+ })(options.comments);
+ }
+ else if (options.comments === "some") {
+ options.comments = function(comment) {
+ var text = comment.value;
+ var type = comment.type;
+ if (type == "comment2") {
+ // multiline comment
+ return /@preserve|@license|@cc_on/i.test(text);
+ }
+ }
+ }
+ else if (options.comments){ // NOTE includes "all" option
+ options.comments = function() {
+ return true;
+ }
+ } else {
+ // Falsy case, so reject all comments, except shebangs
+ options.comments = function(comment) {
+ return comment.type == "comment5";
+ }
+ }
+
var indentation = 0;
var current_col = 0;
var current_line = 1;
@@ -435,7 +478,7 @@ function OutputStream(options) {
AST_Node.DEFMETHOD("add_comments", function(output){
if (output._readonly) return;
- var c = output.option("comments"), self = this;
+ var self = this;
var start = self.start;
if (start && !start._comments_dumped) {
start._comments_dumped = true;
@@ -458,19 +501,7 @@ function OutputStream(options) {
}));
}
- if (!c) {
- comments = comments.filter(function(comment) {
- return comment.type == "comment5";
- });
- } else if (c.test) {
- comments = comments.filter(function(comment){
- return comment.type == "comment5" || c.test(comment.value);
- });
- } else if (typeof c == "function") {
- comments = comments.filter(function(comment){
- return comment.type == "comment5" || c(self, comment);
- });
- }
+ comments = comments.filter(output.option("comments"), self);
// Keep single line comments after nlb, after nlb
if (!output.option("beautify") && comments.length > 0 &&