Do not overwrite options.comments + cleanup
This commit is contained in:
committed by
Richard van Velzen
parent
057de570e6
commit
79b98a9fe8
@@ -45,6 +45,20 @@
|
||||
|
||||
var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
|
||||
|
||||
function is_some_comments(comment) {
|
||||
var text = comment.value;
|
||||
var type = comment.type;
|
||||
if (type == "comment2") {
|
||||
// multiline comment
|
||||
return /@preserve|@license|@cc_on/i.test(text);
|
||||
}
|
||||
return type == "comment5";
|
||||
}
|
||||
|
||||
function is_comment5(comment) {
|
||||
return comment.type == "comment5";
|
||||
}
|
||||
|
||||
function OutputStream(options) {
|
||||
|
||||
options = defaults(options, {
|
||||
@@ -72,46 +86,30 @@ function OutputStream(options) {
|
||||
}, 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);
|
||||
}
|
||||
return type == "comment5";
|
||||
var comment_filter = options.shebang ? is_comment5 : return_false; // Default case, throw all comments away except shebangs
|
||||
if (options.comments) {
|
||||
var comments = options.comments;
|
||||
if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) {
|
||||
var regex_pos = options.comments.lastIndexOf("/");
|
||||
comments = new RegExp(
|
||||
options.comments.substr(1, regex_pos - 1),
|
||||
options.comments.substr(regex_pos + 1)
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (options.comments){ // NOTE includes "all" option
|
||||
options.comments = function() {
|
||||
return true;
|
||||
if (comments instanceof RegExp) {
|
||||
comment_filter = function(comment) {
|
||||
return comment.type == "comment5" || comments.test(comment.value);
|
||||
};
|
||||
}
|
||||
} else {
|
||||
// Falsy case, so reject all comments, except shebangs
|
||||
options.comments = function(comment) {
|
||||
return comment.type == "comment5";
|
||||
else if (typeof comments === "function") {
|
||||
comment_filter = function(comment) {
|
||||
return comment.type == "comment5" || comments(this, comment);
|
||||
};
|
||||
}
|
||||
else if (comments === "some") {
|
||||
comment_filter = is_some_comments;
|
||||
} else { // NOTE includes "all" option
|
||||
comment_filter = return_true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -421,6 +419,7 @@ function OutputStream(options) {
|
||||
with_square : with_square,
|
||||
add_mapping : add_mapping,
|
||||
option : function(opt) { return options[opt] },
|
||||
comment_filter : comment_filter,
|
||||
line : function() { return current_line },
|
||||
col : function() { return current_col },
|
||||
pos : function() { return current_pos },
|
||||
@@ -503,7 +502,7 @@ function OutputStream(options) {
|
||||
}));
|
||||
}
|
||||
|
||||
comments = comments.filter(output.option("comments"), self);
|
||||
comments = comments.filter(output.comment_filter, self);
|
||||
|
||||
// Keep single line comments after nlb, after nlb
|
||||
if (!output.option("beautify") && comments.length > 0 &&
|
||||
|
||||
Reference in New Issue
Block a user