aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Van de Gejuchte <anthonyvdgent@gmail.com>2016-01-19 14:00:22 +0100
committerRichard van Velzen <rvanvelzen@experty.com>2016-01-19 19:14:19 +0100
commit70e5b6f15b130eb1366ff81e0a8a7f187e9cf427 (patch)
treeeedc031688eb1febd5ab95901b8fbbe82edd0ebb
parent57e0fafd5c35552fb1ea63c829a7f6ea7000b6f8 (diff)
downloadtracifyjs-70e5b6f15b130eb1366ff81e0a8a7f187e9cf427.tar.gz
tracifyjs-70e5b6f15b130eb1366ff81e0a8a7f187e9cf427.zip
Add some tests for comment-filters through api
Also never bother comment options to filter comment5/shebang comments as they have their custom filter.
-rw-r--r--lib/output.js4
-rw-r--r--test/mocha/comment-filter.js45
2 files changed, 47 insertions, 2 deletions
diff --git a/lib/output.js b/lib/output.js
index f10c918a..dceece34 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -444,11 +444,11 @@ function OutputStream(options) {
});
} else if (c.test) {
comments = comments.filter(function(comment){
- return c.test(comment.value) || comment.type == "comment5";
+ return comment.type == "comment5" || c.test(comment.value);
});
} else if (typeof c == "function") {
comments = comments.filter(function(comment){
- return c(self, comment) || comment.type == "comment5";
+ return comment.type == "comment5" || c(self, comment);
});
}
diff --git a/test/mocha/comment-filter.js b/test/mocha/comment-filter.js
new file mode 100644
index 00000000..ea2ec2eb
--- /dev/null
+++ b/test/mocha/comment-filter.js
@@ -0,0 +1,45 @@
+var UglifyJS = require('../../');
+var assert = require("assert");
+
+describe("comment filters", function() {
+ it("Should be able to filter comments by passing regex", function() {
+ var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
+ assert.strictEqual(ast.print_to_string({comments: /^!/}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n");
+ });
+
+ it("Should be able to filter comments by passing a function", function() {
+ var ast = UglifyJS.parse("/*TEST 123*/\n//An other comment\n//8 chars.");
+ var f = function(node, comment) {
+ return comment.value.length === 8;
+ };
+
+ assert.strictEqual(ast.print_to_string({comments: f}), "/*TEST 123*/\n//8 chars.\n");
+ });
+
+ it("Should be able to get the comment and comment type when using a function", function() {
+ var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
+ var f = function(node, comment) {
+ return comment.type == "comment1" || comment.type == "comment3";
+ };
+
+ assert.strictEqual(ast.print_to_string({comments: f}), "//!test3\n//test4\n//test5\n//!test6\n");
+ });
+
+ it("Should be able to filter comments by passing a boolean", function() {
+ var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
+
+ assert.strictEqual(ast.print_to_string({comments: true}), "/*!test1*/\n/*test2*/\n//!test3\n//test4\n//test5\n//!test6\n//test7\n//!test8\n");
+ assert.strictEqual(ast.print_to_string({comments: false}), "");
+ });
+
+ it("Should never be able to filter comment5 (shebangs)", function() {
+ var ast = UglifyJS.parse("#!Random comment\n//test1\n/*test2*/");
+ var f = function(node, comment) {
+ assert.strictEqual(comment.type === "comment5", false);
+
+ return true;
+ };
+
+ assert.strictEqual(ast.print_to_string({comments: f}), "#!Random comment\n//test1\n/*test2*/\n");
+ });
+});