aboutsummaryrefslogtreecommitdiff
path: root/test/compress/const.js
blob: a88d5946717be80b0d11703cd69467bf77385a0b (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
issue_1191: {
    options = {
        evaluate      : true,
        booleans      : true,
        comparisons   : true,
        dead_code     : true,
        conditionals  : true,
        side_effects  : true,
        unused        : true,
        hoist_funs    : true,
        if_return     : true,
        join_vars     : true,
        sequences     : false,
        collapse_vars : false,
        reduce_vars   : true,
    }
    input: {
        function foo(rot) {
            const rotTol = 5;
            if (rot < -rotTol || rot > rotTol)
                bar();
            baz();
        }
    }
    expect: {
        function foo(rot) {
            (rot < -5 || rot > 5) && bar();
            baz();
        }
    }
}

issue_1194: {
    options = {
        evaluate      : true,
        booleans      : true,
        comparisons   : true,
        dead_code     : true,
        conditionals  : true,
        side_effects  : true,
        unused        : true,
        hoist_funs    : true,
        if_return     : true,
        join_vars     : true,
        sequences     : false,
        collapse_vars : false,
        reduce_vars   : true,
    }
    input: {
        function f1() {const a = "X"; return a + a;}
        function f2() {const aa = "X"; return aa + aa;}
        function f3() {const aaa = "X"; return aaa + aaa;}
    }
    expect: {
        function f1(){return"XX"}
        function f2(){return"XX"}
        function f3(){return"XX"}
    }
}

issue_1396: {
    options = {
        evaluate      : true,
        booleans      : true,
        comparisons   : true,
        dead_code     : true,
        conditionals  : true,
        side_effects  : true,
        unused        : true,
        hoist_funs    : true,
        if_return     : true,
        join_vars     : true,
        sequences     : false,
        collapse_vars : false,
        reduce_vars   : true,
    }
    input: {
        function foo(a) {
            const VALUE = 1;
            console.log(2 | VALUE);
            console.log(VALUE + 1);
            console.log(VALUE);
            console.log(a & VALUE);
        }
        function bar() {
            const s = "01234567890123456789";
            console.log(s + s + s + s + s);

            const CONSTANT = "abc";
            console.log(CONSTANT + CONSTANT + CONSTANT + CONSTANT + CONSTANT);
        }
    }
    expect: {
        function foo(a) {
            console.log(3);
            console.log(2);
            console.log(1);
            console.log(1 & a);
        }
        function bar() {
            const s = "01234567890123456789";
            console.log(s + s + s + s + s);

            console.log("abcabcabcabcabc");
        }
    }
}

unused_regexp_literal: {
    options = {
        evaluate      : true,
        booleans      : true,
        comparisons   : true,
        dead_code     : true,
        conditionals  : true,
        side_effects  : true,
        unused        : true,
        hoist_funs    : true,
        if_return     : true,
        join_vars     : true,
        sequences     : false,
        collapse_vars : false,
    }
    input: {
        function f(){ var a = /b/; }
    }
    expect: {
        function f(){}
    }
}

regexp_literal_not_const: {
    options = {
        evaluate      : true,
        booleans      : true,
        comparisons   : true,
        dead_code     : true,
        conditionals  : true,
        side_effects  : true,
        unused        : true,
        hoist_funs    : true,
        if_return     : true,
        join_vars     : true,
        sequences     : false,
        collapse_vars : false,
        reduce_vars   : true,
    }
    input: {
        (function(){
            var result;
            const s = 'acdabcdeabbb';
            const REGEXP_LITERAL = /ab*/g;
            while (result = REGEXP_LITERAL.exec(s)) {
                console.log(result[0]);
            }
        })();
    }
    expect: {
        (function() {
            var result;
            const REGEXP_LITERAL = /ab*/g;
            while (result = REGEXP_LITERAL.exec("acdabcdeabbb")) console.log(result[0]);
        })();
    }
    expect_stdout: true
}