aboutsummaryrefslogtreecommitdiff
path: root/test/mocha/parentheses.js
blob: 0fbb4c95970bff08942c6620ebf04b99a98c82a1 (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
var assert = require("assert");
var UglifyJS = require("../..");

describe("parentheses", function() {
    it("Should add trailing parentheses for new expressions with zero arguments in beautify mode", function() {
        var tests = [
            "new x(1);",
            "new x;",
            "new new x;",
            "new (function(foo){this.foo=foo;})(1);",
            "new (function(foo){this.foo=foo;})();",
            "new (function test(foo){this.foo=foo;})(1);",
            "new (function test(foo){this.foo=foo;})();",
            "new true;",
            "new (0);",
            "new (!0);",
            "new (bar = function(foo) {this.foo=foo;})(123);",
            "new (bar = function(foo) {this.foo=foo;})();"
        ];
        var expected = [
            "new x(1);",
            "new x();",
            "new new x()();",
            "new function(foo) {\n    this.foo = foo;\n}(1);",
            "new function(foo) {\n    this.foo = foo;\n}();",
            "new function test(foo) {\n    this.foo = foo;\n}(1);",
            "new function test(foo) {\n    this.foo = foo;\n}();",
            "new true();",
            "new 0();",
            "new (!0)();",
            "new (bar = function(foo) {\n    this.foo = foo;\n})(123);",
            "new (bar = function(foo) {\n    this.foo = foo;\n})();"
        ];
        for (var i = 0; i < tests.length; i++) {
            assert.strictEqual(
                UglifyJS.minify(tests[i], {
                    output: {beautify: true},
                    compress: false,
                    mangle: false
                }).code,
                expected[i]
            );
        }
    });

    it("Should not add trailing parentheses for new expressions with zero arguments in non-beautify mode", function() {
        var tests = [
            "new x(1);",
            "new x;",
            "new new x;",
            "new (function(foo){this.foo=foo;})(1);",
            "new (function(foo){this.foo=foo;})();",
            "new (function test(foo){this.foo=foo;})(1);",
            "new (function test(foo){this.foo=foo;})();",
            "new true;",
            "new (0);",
            "new (!0);",
            "new (bar = function(foo) {this.foo=foo;})(123);",
            "new (bar = function(foo) {this.foo=foo;})();"
        ];
        var expected = [
            "new x(1);",
            "new x;",
            "new(new x);",
            "new function(foo){this.foo=foo}(1);",
            "new function(foo){this.foo=foo};",
            "new function test(foo){this.foo=foo}(1);",
            "new function test(foo){this.foo=foo};",
            "new true;",
            "new 0;",
            "new(!0);",
            "new(bar=function(foo){this.foo=foo})(123);",
            "new(bar=function(foo){this.foo=foo});"
        ];
        for (var i = 0; i < tests.length; i++) {
            assert.strictEqual(
                UglifyJS.minify(tests[i], {
                    output: {beautify: false},
                    compress: false,
                    mangle: false
                }).code,
                expected[i]
            );
        }
    });

    it("Should compress leading parentheses with reasonable performance", function() {
        this.timeout(30000);
        var code = [
            "({}?0:1)&&x();",
            "(function(){}).name;",
        ];
        for (var i = 16; --i >= 0;) {
            code = code.concat(code);
        }
        code = code.join("");
        var result = UglifyJS.minify(code, {
            compress: false,
            mangle: false,
        });
        if (result.error) throw result.error;
        // Dismal performance for `assert.strictEqual()` in Node.js 6
        assert.ok(result.code === code);
    });
});