aboutsummaryrefslogtreecommitdiff
path: root/test/mocha/comments.js
blob: 04dd171e3e93d460d2ec330e506de52944c56be0 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
var assert = require("assert");
var UglifyJS = require("../node");

describe("comments", function() {
    it("Should recognize eol of single line comments", function() {
        var tests = [
            "//Some comment 1\n>",
            "//Some comment 2\r>",
            "//Some comment 3\r\n>",
            "//Some comment 4\u2028>",
            "//Some comment 5\u2029>"
        ];

        var fail = function(e) {
            return e instanceof UglifyJS.JS_Parse_Error
                && e.message === "Unexpected token: operator «>»"
                && e.line === 2
                && e.col === 0;
        }

        for (var i = 0; i < tests.length; i++) {
            assert.throws(function() {
                UglifyJS.parse(tests[i]);
            }, fail, tests[i]);
        }
    });

    it("Should update the position of a multiline comment correctly", function() {
        var tests = [
            "/*Some comment 1\n\n\n*/\n>\n\n\n\n\n\n",
            "/*Some comment 2\r\n\r\n\r\n*/\r\n>\n\n\n\n\n\n",
            "/*Some comment 3\r\r\r*/\r>\n\n\n\n\n\n",
            "/*Some comment 4\u2028\u2028\u2028*/\u2028>\n\n\n\n\n\n",
            "/*Some comment 5\u2029\u2029\u2029*/\u2029>\n\n\n\n\n\n"
        ];

        var fail = function(e) {
            return e instanceof UglifyJS.JS_Parse_Error
                && e.message === "Unexpected token: operator «>»"
                && e.line === 5
                && e.col === 0;
        }

        for (var i = 0; i < tests.length; i++) {
            assert.throws(function() {
                UglifyJS.parse(tests[i]);
            }, fail, tests[i]);
        }
    });

    it("Should handle comment within return correctly", function() {
        var result = UglifyJS.minify([
            "function unequal(x, y) {",
            "    return (",
            "        // Either one",
            "        x < y",
            "        ||",
            "        y < x",
            "    );",
            "}",
        ].join("\n"), {
            compress: false,
            mangle: false,
            output: {
                beautify: true,
                comments: "all",
            },
        });
        if (result.error) throw result.error;
        assert.strictEqual(result.code, [
            "function unequal(x, y) {",
            "    // Either one",
            "    return x < y || y < x;",
            "}",
        ].join("\n"));
    });

    it("Should handle comment folded into return correctly", function() {
        var result = UglifyJS.minify([
            "function f() {",
            "    /* boo */ x();",
            "    return y();",
            "}",
        ].join("\n"), {
            mangle: false,
            output: {
                beautify: true,
                comments: "all",
            },
        });
        if (result.error) throw result.error;
        assert.strictEqual(result.code, [
            "function f() {",
            "    /* boo */",
            "    return x(), y();",
            "}",
        ].join("\n"));
    });

    it("Should not drop comments after first OutputStream", function() {
        var code = "/* boo */\nx();";
        var ast = UglifyJS.parse(code);
        var out1 = UglifyJS.OutputStream({
            beautify: true,
            comments: "all",
        });
        ast.print(out1);
        var out2 = UglifyJS.OutputStream({
            beautify: true,
            comments: "all",
        });
        ast.print(out2);
        assert.strictEqual(out1.get(), code);
        assert.strictEqual(out2.get(), out1.get());
    });

    it("Should retain trailing comments", function() {
        var code = [
            "if (foo /* lost comment */ && bar /* lost comment */) {",
            "    // this one is kept",
            "    {/* lost comment */}",
            "    !function() {",
            "        // lost comment",
            "    }();",
            "    function baz() {/* lost comment */}",
            "    // lost comment",
            "}",
            "// comments right before EOF are lost as well",
        ].join("\n");
        var result = UglifyJS.minify(code, {
            compress: false,
            mangle: false,
            output: {
                beautify: true,
                comments: "all",
            },
        });
        if (result.error) throw result.error;
        assert.strictEqual(result.code, code);
    });

    it("Should retain comments within braces", function() {
        var code = [
            "{/* foo */}",
            "a({/* foo */});",
            "while (a) {/* foo */}",
            "switch (a) {/* foo */}",
            "if (a) {/* foo */} else {/* bar */}",
        ].join("\n\n");
        var result = UglifyJS.minify(code, {
            compress: false,
            mangle: false,
            output: {
                beautify: true,
                comments: "all",
            },
        });
        if (result.error) throw result.error;
        assert.strictEqual(result.code, code);
    });

    it("Should correctly preserve new lines around comments", function() {
        var tests = [
            [
                "// foo",
                "// bar",
                "x();",
            ].join("\n"),
            [
                "// foo",
                "/* bar */",
                "x();",
            ].join("\n"),
            [
                "// foo",
                "/* bar */ x();",
            ].join("\n"),
            [
                "/* foo */",
                "// bar",
                "x();",
            ].join("\n"),
            [
                "/* foo */ // bar",
                "x();",
            ].join("\n"),
            [
                "/* foo */",
                "/* bar */",
                "x();",
            ].join("\n"),
            [
                "/* foo */",
                "/* bar */ x();",
            ].join("\n"),
            [
                "/* foo */ /* bar */",
                "x();",
            ].join("\n"),
            "/* foo */ /* bar */ x();",
        ].forEach(function(code) {
            var result = UglifyJS.minify(code, {
                compress: false,
                mangle: false,
                output: {
                    beautify: true,
                    comments: "all",
                },
            });
            if (result.error) throw result.error;
            assert.strictEqual(result.code, code);
        });
    });

    it("Should preserve new line before comment without beautify", function() {
        var code = [
            "function f(){",
            "/* foo */bar()}",
        ].join("\n");
        var result = UglifyJS.minify(code, {
            compress: false,
            mangle: false,
            output: {
                comments: "all",
            },
        });
        if (result.error) throw result.error;
        assert.strictEqual(result.code, code);
    });

    it("Should preserve comments around IIFE", function() {
        var result = UglifyJS.minify("/*a*/(/*b*/function(){/*c*/}/*d*/)/*e*/();", {
            compress: false,
            mangle: false,
            output: {
                comments: "all",
            },
        });
        if (result.error) throw result.error;
        assert.strictEqual(result.code, "/*a*/ /*b*/(function(){/*c*/}/*d*/ /*e*/)();");
    });

    it("Should output line comments after statements", function() {
        var result = UglifyJS.minify([
            "x()//foo",
            "{y()//bar",
            "}",
        ].join("\n"), {
            compress: false,
            mangle: false,
            output: {
                comments: "all",
            },
        });
        if (result.error) throw result.error;
        assert.strictEqual(result.code, [
            "x();//foo",
            "{y();//bar",
            "}",
        ].join("\n"));
    });

    it("Should handle programmatic AST insertions gracefully", function() {
        var ast = UglifyJS.parse([
            "function f() {",
            "    //foo",
            "    bar;",
            "    return;",
            "}",
        ].join("\n"));
        ast.body[0].body[0] = new UglifyJS.AST_Throw({value: ast.body[0].body[0].body});
        ast.body[0].body[1].value = new UglifyJS.AST_Number({value: 42});
        assert.strictEqual(ast.print_to_string({comments: "all"}), [
            "function f(){",
            "//foo",
            "throw bar;return 42}",
        ].join("\n"));
    });

    it("Should not duplicate sourceMappingURL", function() {
        var code = [
            "//# sourceMappingURL=input.map",
            "print(42);",
            "//# sourceMappingURL=input.map",
            "",
            "//# sourceMappingURL=input.map",
            "//# sourceMappingURL=input.map",
            "",
        ].join("\n");

        var result = UglifyJS.minify(code, {
            output: { comments: true },
        });
        if (result.error) throw result.error;
        assert.strictEqual(result.code, [
            "//# sourceMappingURL=input.map",
            "print(42);",
            "//# sourceMappingURL=input.map",
            "//# sourceMappingURL=input.map",
            "//# sourceMappingURL=input.map",
        ].join("\n"));

        result = UglifyJS.minify(code, {
            output: { comments: true },
            sourceMap: { url: "output.map" },
        });
        if (result.error) throw result.error;
        assert.strictEqual(result.code, [
            "//# sourceMappingURL=input.map",
            "print(42);",
            "//# sourceMappingURL=input.map",
            "//# sourceMappingURL=input.map",
            "//# sourceMappingURL=output.map",
        ].join("\n"));
    });

    describe("comment before constant", function() {
        var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }';

        it("Should test comment before constant is retained and output after mangle.", function() {
            var result = UglifyJS.minify(js, {
                compress: { collapse_vars: false, reduce_vars: false },
                output: { comments: true },
            });
            assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}');
        });

        it("Should test code works when comments disabled.", function() {
            var result = UglifyJS.minify(js, {
                compress: { collapse_vars: false, reduce_vars: false },
                output: { comments: false },
            });
            assert.strictEqual(result.code, 'function f(){var n=!1;return n}');
        });
    });

    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*/");
        });

        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*/");
        });

        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*/");
        });

        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;", {
                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;", {
                output: { preamble: "/* Build */" }
            }).code;
            assert.strictEqual(code, "/* Build */\nvar x=10;");
        });
    });

    describe("Huge number of comments.", function() {
        it("Should parse and compress code with thousands of consecutive comments", function() {
            var js = "function lots_of_comments(x) { return 7 -";
            for (var i = 1; i <= 5000; ++i) js += "// " + i + "\n";
            for (; i <= 10000; ++i) js += "/* " + i + " */ /**/";
            js += "x; }";
            var result = UglifyJS.minify(js, { mangle: false });
            assert.strictEqual(result.code, "function lots_of_comments(x){return 7-x}");
        });
    });
});