From 0111497fc98d5098f81bc749f77da5734add37bb Mon Sep 17 00:00:00 2001 From: Anthony Van de Gejuchte Date: Sat, 3 Sep 2016 23:26:31 +0200 Subject: Make all comment options in cli available in js api Also removing more code within "loop" while at it. --- lib/output.js | 59 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 14 deletions(-) (limited to 'lib') 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 && -- cgit v1.2.3