fix corner case in `hoist_vars` (#5188)
[UglifyJS.git] / test / compress / hoist_vars.js
1 statements: {
2     options = {
3         hoist_funs: false,
4         hoist_vars: true,
5     }
6     input: {
7         function f() {
8             var a = 1;
9             var b = 2;
10             var c = 3;
11             function g() {}
12             return g(a, b, c);
13         }
14     }
15     expect: {
16         function f() {
17             var a = 1, b = 2, c = 3;
18             function g() {}
19             return g(a, b, c);
20         }
21     }
22 }
23
24 statements_funs: {
25     options = {
26         hoist_funs: true,
27         hoist_vars: true,
28     }
29     input: {
30         function f() {
31             var a = 1;
32             var b = 2;
33             var c = 3;
34             function g() {}
35             return g(a, b, c);
36         }
37     }
38     expect: {
39         function f() {
40             function g() {}
41             var a = 1, b = 2, c = 3;
42             return g(a, b, c);
43         }
44     }
45 }
46
47 sequences: {
48     options = {
49         hoist_funs: false,
50         hoist_vars: true,
51     }
52     input: {
53         function f() {
54             var a = 1, b = 2;
55             function g() {}
56             var c = 3;
57             return g(a, b, c);
58         }
59     }
60     expect: {
61         function f() {
62             var c, a = 1, b = 2;
63             function g() {}
64             c = 3;
65             return g(a, b, c);
66         }
67     }
68 }
69
70 sequences_funs: {
71     options = {
72         hoist_funs: true,
73         hoist_vars: true,
74     }
75     input: {
76         function f() {
77             var a = 1, b = 2;
78             function g() {}
79             var c = 3;
80             return g(a, b, c);
81         }
82     }
83     expect: {
84         function f() {
85             function g() {}
86             var a = 1, b = 2, c = 3;
87             return g(a, b, c);
88         }
89     }
90 }
91
92 catch_var: {
93     options = {
94         dead_code: true,
95         hoist_vars: true,
96         side_effects: true,
97         toplevel: true,
98         unused: true,
99     }
100     input: {
101         var a = "PASS";
102         try {
103             a;
104         } catch (a) {
105             var a = 0;
106             a;
107         }
108         console.log(a);
109     }
110     expect: {
111         var a = "PASS";
112         console.log(a);
113     }
114     expect_stdout: "PASS"
115 }
116
117 issue_2295: {
118     options = {
119         collapse_vars: true,
120         hoist_vars: true,
121     }
122     input: {
123         function foo(o) {
124             var a = o.a;
125             if (a) return a;
126             var a = 1;
127         }
128     }
129     expect: {
130         function foo(o) {
131             var a = o.a;
132             if (a) return a;
133             a = 1;
134         }
135     }
136 }
137
138 issue_4487_1: {
139     options = {
140         functions: true,
141         hoist_vars: true,
142         keep_fnames: true,
143         reduce_vars: true,
144         toplevel: true,
145         unused: true,
146     }
147     input: {
148         var a = function f() {
149             var f = console.log(typeof f);
150         };
151         var b = a();
152     }
153     expect: {
154         function a() {
155             var f = console.log(typeof f);
156         }
157         a();
158     }
159     expect_stdout: "undefined"
160 }
161
162 issue_4487_2: {
163     options = {
164         functions: true,
165         hoist_vars: true,
166         keep_fnames: true,
167         passes: 2,
168         reduce_vars: true,
169         toplevel: true,
170         unused: true,
171     }
172     input: {
173         var a = function f() {
174             var f = console.log(typeof f);
175         };
176         var b = a();
177     }
178     expect: {
179         (function a() {
180             console.log(typeof void 0);
181         })();
182     }
183     expect_stdout: "undefined"
184 }
185
186 issue_4489: {
187     options = {
188         collapse_vars: true,
189         evaluate: true,
190         hoist_vars: true,
191         reduce_vars: true,
192         toplevel: true,
193         unused: true,
194     }
195     input: {
196         A = 0;
197         var o = !0 || null;
198         for (var k in o);
199         console.log(k);
200     }
201     expect: {
202         !(A = 0);
203         for (var k in true);
204         console.log(k);
205     }
206     expect_stdout: "undefined"
207 }
208
209 issue_4517: {
210     options = {
211         collapse_vars: true,
212         hoist_vars: true,
213         join_vars: true,
214         reduce_vars: true,
215         unused: true,
216     }
217     input: {
218         console.log(function() {
219             var a = 2;
220             A = a;
221             var b = typeof !1;
222             return A + b;
223         }());
224     }
225     expect: {
226         console.log(function() {
227             var a = 2;
228             A = a;
229             return A + typeof !1;
230         }());
231     }
232     expect_stdout: "2boolean"
233 }
234
235 issue_4736: {
236     options = {
237         collapse_vars: true,
238         evaluate: true,
239         hoist_vars: true,
240         merge_vars: true,
241         reduce_vars: true,
242         toplevel: true,
243         unused: true,
244     }
245     input: {
246         var a;
247         function f() {
248             (function g() {
249                 var b = (a = 0, 1 << 30);
250                 var c = (a = 0, console.log(b));
251                 var d = c;
252             })(f);
253         }
254         f();
255     }
256     expect: {
257         (function() {
258             (function() {
259                 0,
260                 console.log(1073741824);
261             })();
262         })();
263     }
264     expect_stdout: "1073741824"
265 }
266
267 issue_4839: {
268     options = {
269         evaluate: true,
270         hoist_vars: true,
271         keep_fargs: false,
272         reduce_vars: true,
273         toplevel: true,
274         unused: true,
275     }
276     input: {
277         var o = function(a, b) {
278             return b && b;
279         }("foo");
280         for (var k in o)
281             throw "FAIL";
282         console.log("PASS");
283     }
284     expect: {
285         var k, o = void 0;
286         for (k in o)
287             throw "FAIL";
288         console.log("PASS");
289     }
290     expect_stdout: "PASS"
291 }
292
293 issue_4859: {
294     options = {
295         evaluate: true,
296         hoist_vars: true,
297         keep_infinity: true,
298         merge_vars: true,
299         reduce_vars: true,
300         toplevel: true,
301         unused: true,
302     }
303     input: {
304         function f(a) {
305             var b = (a = 2, 1 / 0), c = 3;
306             var d = a + b;
307             console.log(d);
308             return f;
309         }
310         f();
311     }
312     expect: {
313         (function f(a) {
314             var d = 1 / 0, d = Infinity;
315             console.log(d);
316             return f;
317         })();
318     }
319     expect_stdout: "Infinity"
320 }
321
322 issue_4893_1: {
323     options = {
324         collapse_vars: true,
325         evaluate: true,
326         hoist_vars: true,
327         reduce_vars: true,
328         toplevel: true,
329         unused: true,
330     }
331     input: {
332         function f() {
333             function g() {}
334             var a = null;
335             var b = null;
336             var c = null;
337             b.p += a = 42;
338             f;
339         }
340         try {
341             f();
342         } catch (e) {
343             console.log("PASS");
344         }
345     }
346     expect: {
347         try{
348             (function f() {
349                 null.p += 42;
350                 f;
351             })();
352         } catch (e) {
353             console.log("PASS");
354         }
355     }
356     expect_stdout: "PASS"
357 }
358
359 issue_4893_2: {
360     options = {
361         collapse_vars: true,
362         hoist_vars: true,
363         pure_getters: "strict",
364         reduce_vars: true,
365         side_effects: true,
366         toplevel: true,
367         unused: true,
368     }
369     input: {
370         function f() {
371             function g() {}
372             var a = null;
373             var b = null;
374             var c = null;
375             b.p += a = 42;
376             f;
377         }
378         try {
379             f();
380         } catch (e) {
381             console.log("PASS");
382         }
383     }
384     expect: {
385         try{
386             (function() {
387                 var b;
388                 b = null;
389                 b.p += 42;
390             })();
391         } catch (e) {
392             console.log("PASS");
393         }
394     }
395     expect_stdout: "PASS"
396 }
397
398 issue_4898: {
399     options = {
400         collapse_vars: true,
401         evaluate: true,
402         hoist_vars: true,
403         loops: true,
404         reduce_vars: true,
405         toplevel: true,
406         unused: true,
407     }
408     input: {
409         do {
410             var b = [ console.log("PASS") ];
411             var c = b;
412         } while (c.p = 0);
413     }
414     expect: {
415         var b;
416         b = [ console.log("PASS") ];
417         b.p = 0;
418     }
419     expect_stdout: "PASS"
420 }
421
422 issue_5187: {
423     options = {
424         hoist_props: true,
425         hoist_vars: true,
426         reduce_vars: true,
427         side_effects: true,
428         toplevel: true,
429         unused: true,
430     }
431     input: {
432         function f() {
433             var a = 42;
434             do {
435                 var b = { 0: a++ };
436             } while (console.log(b[b ^= 0]));
437         }
438         f();
439     }
440     expect: {
441         (function() {
442             var b, a = 42;
443             do {
444                 b = { 0: a++ };
445             } while (console.log(b[b ^= 0]));
446         })();
447     }
448     expect_stdout: "42"
449 }