aboutsummaryrefslogtreecommitdiff
path: root/lib/output.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/output.js')
-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 &&