aboutsummaryrefslogtreecommitdiff
path: root/test/mocha/comment-filter.js
blob: ec17aa8cb448facef44b5212559d58debe5dad57 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var UglifyJS = require('../../');
var assert = require("assert");

describe("comment filters", function() {
    it("Should be able to filter comments by passing regexp", 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 with the 'all' option", 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: "all"}), "/*!test1*/\n/*test2*/\n//!test3\n//test4\n//test5\n//!test6\n//test7\n//!test8\n");
    });

    it("Should be able to filter commments with the 'some' option", function() {
        var ast = UglifyJS.parse("// foo\n/*@preserve*/\n// bar\n/*@license*/\n//@license with the wrong comment type\n/*@cc_on something*/");
        assert.strictEqual(ast.print_to_string({comments: "some"}), "/*@preserve*/\n/*@license*/\n/*@cc_on something*/\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 filter comments by passing regex in string format", 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 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");
    });

    it("Should never be able to filter comment5 when using 'some' as filter", function() {
        var ast = UglifyJS.parse("#!foo\n//foo\n/*@preserve*/\n/* please hide me */");
        assert.strictEqual(ast.print_to_string({comments: "some"}), "#!foo\n/*@preserve*/\n");
    });

    it("Should have no problem on multiple calls", function() {
        const options = {
            comments: /ok/
        };

        assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
        assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
        assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
    });

    it("Should handle shebang and preamble correctly", function() {
        var code = UglifyJS.minify("#!/usr/bin/node\nvar x = 10;", {
            fromString: true,
            output: { preamble: "/* Build */" }
        }).code;
        assert.strictEqual(code, "#!/usr/bin/node\n/* Build */\nvar x=10;");
    });

    it("Should handle preamble without shebang correctly", function() {
        var code = UglifyJS.minify("var x = 10;", {
            fromString: true,
            output: { preamble: "/* Build */" }
        }).code;
        assert.strictEqual(code, "/* Build */\nvar x=10;");
    });
});