-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RescopeGlobalSymbolsTest.java
708 lines (642 loc) · 25.9 KB
/
RescopeGlobalSymbolsTest.java
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*
* Copyright 2011 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
/**
* Unit tests for {@link RescopeGlobalSymbols}
*
*/
public final class RescopeGlobalSymbolsTest extends CompilerTestCase {
private static final String NAMESPACE = "_";
private boolean assumeCrossModuleNames = true;
@Override protected CompilerPass getProcessor(Compiler compiler) {
return new RescopeGlobalSymbols(
compiler,
NAMESPACE,
false,
assumeCrossModuleNames);
}
@Override
protected void setUp() throws Exception {
super.setUp();
assumeCrossModuleNames = true;
}
@Override
protected int getNumRepetitions() {
return 1;
}
public void testVarDeclarations() {
test("var a = 1;", "_.a = 1;");
test("var a = 1, b = 2, c = 3;", "_.a = 1; _.b = 2; _.c = 3;");
test(
"var a = 'str', b = 1, c = { foo: 'bar' }, d = function() {};",
"_.a = 'str'; _.b = 1; _.c = { foo: 'bar' }; _.d = function() {};");
test("if(1){var x = 1;}", "if(1){_.x = 1;}");
test("var x;", "");
test("var x; alert(x)", "window.alert(_.x);");
test("var a, b = 1;", "_.b = 1");
test("var a, b = 1; alert(a);", "_.b = 1; window.alert(_.a);");
}
public void testVarDeclarations_allSameModule() {
assumeCrossModuleNames = false;
testSame("var a = 1;");
testSame("var a = 1, b = 2, c = 3;");
testSame("var a = 'str', b = 1, c = { foo: 'bar' }, d = function() {};");
testSame("if(1){var x = 1;}");
testSame("var x;");
testSame("var a, b = 1;");
}
public void testVarDeclarations_export() {
assumeCrossModuleNames = false;
test("var _dumpException = 1;", "_._dumpException = 1");
}
public void testVarDeclarations_acrossModules() {
assumeCrossModuleNames = false;
test(createModules(
"var a = 1;", "a"),
new String[] {"_.a = 1", "_.a"});
test(createModules(
"var a = 1, b = 2, c = 3;", "a;c;"),
new String[] {"var b;_.a = 1; b = 2; _.c = 3;", "_.a;_.c"});
test(createModules(
"var a = 1, b = 2, c = 3;", "b;c;"),
new String[] {"var a;a = 1; _.b = 2; _.c = 3;", "_.b;_.c"});
test(createModules(
"var a = 1, b = 2, c = 3;b;c;", "a;c;"),
new String[] {"var b;_.a = 1; b = 2; _.c = 3;b;_.c", "_.a;_.c"});
test(createModules(
"var a, b = 1;", "b"),
new String[] {"var a;_.b = 1;", "_.b"});
test(createModules(
"var a, b = 1, c = 2;", "b"),
new String[] {"var a, c;_.b = 1;c = 2", "_.b"});
test(createModules(
"var a, b = 1, c = 2;", "a"),
new String[] {"var b, c;b = 1;c = 2", "_.a"});
test(createModules(
"var a=1; var b=2,c=3;", "a;c;"),
new String[] {"var b;_.a=1;b=2;_.c=3", "_.a;_.c"});
test(createModules(
"1;var a, b = 1, c = 2;", "b"),
new String[] {"var a, c;1;_.b = 1;c = 2", "_.b"});
}
public void testLetDeclarations() {
test("let a = 1;", "_.a = 1;");
test("let a = 1, b = 2, c = 3;", "_.a = 1; _.b = 2; _.c = 3;");
test(
"let a = 'str', b = 1, c = { foo: 'bar' }, d = function() {};",
"_.a = 'str'; _.b = 1; _.c = { foo: 'bar' }; _.d = function() {};");
testSame("if(1){let x = 1;}");
test("let x;", "");
test("let x; alert(x);", "window.alert(_.x);");
test("let a, b = 1;", "_.b = 1");
test("let a, b = 1; alert(a);", "_.b = 1; window.alert(_.a);");
}
public void testLetDeclarations_allSameModule() {
assumeCrossModuleNames = false;
testSame("let a = 1;");
testSame("let a = 1, b = 2, c = 3;");
testSame("let a = 'str', b = 1, c = { foo: 'bar' }, d = function() {};");
testSame("if(1){let x = 1;}");
testSame("let x;");
testSame("let a, b = 1;");
}
public void testLetDeclarations_export() {
assumeCrossModuleNames = false;
test("let _dumpException = 1;", "_._dumpException = 1");
}
public void testLetDeclarations_acrossModules() {
assumeCrossModuleNames = false;
// test references across modules.
test(createModules("let a = 1;", "a"), new String[] {"_.a = 1", "_.a"});
test(
createModules("let a = 1, b = 2, c = 3;", "a;c;"),
new String[] {"var b;_.a = 1; b = 2; _.c = 3;", "_.a;_.c"});
test(
createModules("let a = 1, b = 2, c = 3;", "b;c;"),
new String[] {"var a;a = 1; _.b = 2; _.c = 3;", "_.b;_.c"});
test(
createModules("let a = 1, b = 2, c = 3;b;c;", "a;c;"),
new String[] {"var b;_.a = 1; b = 2; _.c = 3;b;_.c", "_.a;_.c"});
test(createModules("let a, b = 1;", "b"), new String[] {"var a;_.b = 1;", "_.b"});
test(
createModules("let a, b = 1, c = 2;", "b"), new String[] {"var a, c;_.b = 1;c = 2", "_.b"});
test(createModules("let a, b = 1, c = 2;", "a"), new String[] {"var b, c;b = 1;c = 2", "_.a"});
test(
createModules("let a=1; let b=2,c=3;", "a;c;"),
new String[] {"var b;_.a=1;b=2;_.c=3", "_.a;_.c"});
test(
createModules("1;let a, b = 1, c = 2;", "b"),
new String[] {"var a, c;1;_.b = 1;c = 2", "_.b"});
// test non-globals with same name as cross-module globals.
testSame(createModules("1;let a, b = 1, c = 2;", "if (true) { let b = 3; b; }"));
test(
createModules("1;let a, b = 1, c = 2;", "b; if (true) { let b = 3; b; }"),
new String[] {"var a, c; 1;_.b = 1;c = 2", "_.b; if (true) { let b = 3; b; }"});
}
public void testConstDeclarations() {
test("const a = 1;", "_.a = 1;");
test("const a = 1, b = 2, c = 3;", "_.a = 1; _.b = 2; _.c = 3;");
test(
"const a = 'str', b = 1, c = { foo: 'bar' }, d = function() {};",
"_.a = 'str'; _.b = 1; _.c = { foo: 'bar' }; _.d = function() {};");
testSame("if(1){const x = 1;}");
}
public void testConstDeclarations_allSameModule() {
assumeCrossModuleNames = false;
testSame("const a = 1;");
testSame("const a = 1, b = 2, c = 3;");
testSame("const a = 'str', b = 1, c = { foo: 'bar' }, d = function() {};");
testSame("if(1){const x = 1;}");
}
public void testConstDeclarations_export() {
assumeCrossModuleNames = false;
test("const _dumpException = 1;", "_._dumpException = 1");
}
public void testConstDeclarations_acrossModules() {
assumeCrossModuleNames = false;
// test references across modules.
test(createModules("const a = 1;", "a"), new String[] {"_.a = 1", "_.a"});
test(
createModules("const a = 1, b = 2, c = 3;", "a;c;"),
new String[] {"var b;_.a = 1; b = 2; _.c = 3;", "_.a;_.c"});
test(
createModules("const a = 1, b = 2, c = 3;", "b;c;"),
new String[] {"var a;a = 1; _.b = 2; _.c = 3;", "_.b;_.c"});
test(
createModules("const a = 1, b = 2, c = 3;b;c;", "a;c;"),
new String[] {"var b;_.a = 1; b = 2; _.c = 3;b;_.c", "_.a;_.c"});
test(
createModules("const a=1; const b=2,c=3;", "a;c;"),
new String[] {"var b;_.a=1;b=2;_.c=3", "_.a;_.c"});
// test non-globals with same name as cross-module globals.
testSame(createModules("1;const a = 1, b = 1, c = 2;", "if (true) { const b = 3; b; }"));
test(
createModules("1;const a = 1, b = 1, c = 2;", "b; if (true) { const b = 3; b; }"),
new String[] {"var a, c; 1;a = 1; _.b = 1;c = 2", "_.b; if (true) { const b = 3; b; }"});
}
public void testObjectDestructuringDeclarations() {
// TODO(b/69868034): Fix commented-out tests before marking this pass ES6-compatible.
// This pass is not guaranteed to be normalized so it must handle the object literal shorthand.
// test("var {a} = {}; a;", "({a: _.a} = {}); _.a;");
test("var {a: a} = {}; a;", "({a: _.a} = {}); _.a;");
test("var {key: a} = {}; a;", "({key: _.a} = {}); _.a;");
// test("var {a: {b}} = {}; b;", "({a: {b: _.b}} = {}); _.b;");
test("var {a: {key: b}} = {}; b;", "({a: {key: _.b}} = {}); _.b;");
test("var {['computed']: a} = {}; a;", "({['computed']: _.a} = {}); _.a;");
// test("var {a = 3} = {}; a;", "({a: _.a = 3} = {}); _.a");
test("var {key: a = 3} = {}; a;", "({key: _.a = 3} = {}); _.a");
}
public void testObjectDestructuringDeclarations_allSameModule() {
assumeCrossModuleNames = false;
// TODO(b/69868034): Fix commented-out test before marking this pass ES6-compatible.
// test("var {a} = {}; a;", "var {a: a} = {}; a;");
testSame("var {a: a} = {}; a;");
testSame("var {key: a} = {}; a;");
testSame("var {a: {b: b}} = {}; b;");
testSame("var {['computed']: a} = {}; a;");
testSame("var {a: a = 3} = {}; a;");
testSame("var {key: a = 3} = {}; a;");
testSame("var {['computed']: a = 3} = {}; a;");
}
public void testObjectDestructuringDeclarations_acrossModules() {
assumeCrossModuleNames = false;
test(createModules("var {a: a} = {};", "a"), new String[] {"({a: _.a} = {});", "_.a"});
test(
createModules("var {a: a, b: b, c: c} = {};", "a; c;"),
new String[] {"var b;({a: _.a, b: b, c: _.c} = {});", "_.a; _.c"});
test(
createModules("var {a: a, b: b, c: c} = {};", "b; c;"),
new String[] {"var a;({a: a, b: _.b, c: _.c} = {});", "_.b; _.c"});
test(
createModules("var {a: a, b: b, c: c} = {}; b; c;", "a; c;"),
new String[] {"var b;({a: _.a, b: b, c: _.c} = {});b;_.c;", "_.a; _.c"});
}
public void testObjectDestructuringAssignments() {
test("var a, b; ({key1: a, key2: b} = {}); a; b;", "({key1: _.a, key2: _.b} = {}); _.a; _.b;");
// Test a destructuring assignment with mixed global and local variables.
test(
"var a; if (true) { let b; ({key1: a, key2: b} = {}); b; } a;",
"if (true) { let b; ({key1: _.a, key2: b} = {}); b; } _.a;");
test("var obj = {}; ({a: obj.a} = {}); obj.a;", "_.obj = {}; ({a: _.obj.a} = {}); _.obj.a;");
test(
"var obj = {}; ({a: obj['foo bar']} = {}); obj['foo bar'];",
" _.obj = {}; ({a: _.obj['foo bar']} = {}); _.obj['foo bar'];");
}
public void testArrayDestructuringDeclarations() {
test("var [a] = [1]; a", "[_.a] = [1]; _.a;");
test("var [a, b, c] = [1, 2, 3]; a; b; c;", "[_.a, _.b, _.c] = [1, 2, 3]; _.a; _.b; _.c");
test("var [[a, b], c] = []; a; b; c", "[[_.a, _.b], _.c] = []; _.a; _.b; _.c;");
test("var [a = 5] = [1]; a", "[_.a = 5] = [1]; _.a;");
test("var [a, b = 5] = [1]; a; b;", "[_.a, _.b = 5] = [1]; _.a; _.b;");
test("var [...a] = [1, 2, 3]; a;", "[..._.a] = [1, 2, 3]; _.a;");
test("var [a, ...b] = [1, 2, 3]; a; b;", "[_.a, ..._.b] = [1, 2, 3]; _.a; _.b;");
test("var [a] = 1, b = 2; a; b;", "[_.a] = 1; _.b = 2; _.a; _.b;");
}
public void testArrayDestructuringDeclarations_sameModule() {
assumeCrossModuleNames = false;
testSame("var [a] = [1]; a");
testSame("var [a, b] = [1, 2]; a; b;");
testSame("var [[a, b], c] = []; a; b; c");
testSame("var [a = 5] = [1]; a");
testSame("var [a, b = 5] = [1]; a; b;");
testSame("var [...a] = [1, 2, 3]; a;");
testSame("var [a, ...b] = [1, 2, 3]; a; b;");
}
public void testArrayDestructuringDeclarations_acrossModules() {
assumeCrossModuleNames = false;
test(createModules("var [a] = [];", "a"), new String[] {"[_.a] = [];", "_.a"});
test(
createModules("var [a, b, c] = [];", "a; c;"),
new String[] {"var b; [_.a, b, _.c] = [];", "_.a; _.c"});
test(
createModules("var [a, b, c] = [];", "b; c;"),
new String[] {"var a; [a, _.b, _.c] = [];", "_.b; _.c"});
test(
createModules("var [a, b, c] = []; b; c;", "a; c;"),
new String[] {"var b; [_.a, b, _.c] = []; b; _.c;", "_.a; _.c"});
}
public void testArrayDestructuringAssignments() {
test("var a, b; [a, b] = []; a; b;", "[_.a, _.b] = []; _.a; _.b;");
// Test a destructuring assignment with mixed global and local variables.
test(
"var a; if (true) { let b; [a, b] = []; b; } a;",
"if (true) { let b; [_.a, b] = []; b; } _.a;");
// Test assignments to qualified names and quoted properties.
test("var obj = {}; [obj.a] = []; obj.a;", "_.obj = {}; [_.obj.a] = []; _.obj.a;");
test(
"var obj = {}; [ obj['foo bar']] = []; obj['foo bar'];",
" _.obj = {}; [_.obj['foo bar']] = []; _.obj['foo bar'];");
}
public void testClasses() {
test("class A {}", "_.A = class {};");
test("class A {} class B extends A {}", "_.A = class {}; _.B = class extends _.A {}");
test("class A {} let a = new A;", "_.A = class {}; _.a = new _.A;");
test(
lines(
"const PI = 3.14;",
"class A {",
" static printPi() {",
" console.log(PI);",
" }",
"}",
"A.printPi();"),
lines(
"_.PI = 3.14;",
"_.A = class {",
" static printPi() {",
" window.console.log(_.PI);",
" }",
"}",
"_.A.printPi();"));
// Test that class expression names are not rewritten.
test("var A = class Name {};", "_.A = class Name {};");
test("var A = class A {};", "_.A = class A {};");
}
public void testClasses_nonGlobal() {
testSame("if (true) { class A {} }");
test("function foo() { class A {} }", "_.foo = function() { class A {} };");
test("const A = 5; { class A {} }", "_.A = 5; { class A {} }");
}
public void testClasses_allSameModule() {
assumeCrossModuleNames = false;
test("class A {}", "var A = class {};");
test("class A {} class B extends A {}", "var A = class {}; var B = class extends A {}");
testSame("if (true) { class A {} }");
}
public void testForLoops() {
assumeCrossModuleNames = false;
test(createModules(
"for (var i = 0, c = 2; i < 1000; i++);", "c"),
new String[] {"var i;for (i = 0, _.c = 2; i < 1000; i++);", "_.c"});
test(
createModules(" for (var i = 0, c = 2; i < 1000; i++);", "i"),
new String[] {"var c;for ( _.i = 0, c = 2; _.i < 1000; _.i++);", "_.i"});
test(
createModules(" for (var {i: i, c: c} = {}; i < 1000; i++);", "c"),
new String[] {"var i; for ( ({i: i, c: _.c} = {}); i < 1000; i++);", "_.c"});
test(
createModules(" for (var [i, c] = [0, 2]; i < 1000; i++);", "c;"),
new String[] {"var i; for ( [i, _.c] = [0, 2]; i < 1000; i++);", "_.c;"});
}
public void testForLoops_acrossModules() {
test(
"for (var i = 0; i < 1000; i++);",
"for (_.i = 0; _.i < 1000; _.i++);");
test(
"for (var i = 0, c = 2; i < 1000; i++);",
"for (_.i = 0, _.c = 2; _.i < 1000; _.i++);");
test(
"for (var i = 0, c = 2, d = 3; i < 1000; i++);",
"for (_.i = 0, _.c = 2, _.d = 3; _.i < 1000; _.i++);");
test(
"for (var i = 0, c = 2, d = 3, e = 4; i < 1000; i++);",
"for (_.i = 0, _.c = 2, _.d = 3, _.e = 4; _.i < 1000; _.i++);");
test(
"for (var i = 0; i < 1000;)i++;",
"for (_.i = 0; _.i < 1000;)_.i++;");
test(
"for (var i = 0,b; i < 1000;)i++;b++",
"for (_.i = 0,_.b; _.i < 1000;)_.i++;_.b++");
test("var o={};for (var i in o)i++;", "_.o={};for (_.i in _.o)_.i++;");
// Test destructuring.
test("for (var [i] = [0]; i < 1000; i++);", "for ([_.i] = [0]; _.i < 1000; _.i++);");
test("for (var {i: i} = {}; i < 1000; i++);", " for (({i: _.i} = {}); _.i < 1000; _.i++);");
testSame("for (let [i] = [0]; i < 1000; i++);");
}
public void testForInLoops_allSameModule() {
assumeCrossModuleNames = false;
testSame("for (var i in {});");
testSame("for (var [a] in {});");
testSame("for (var {a: a} in {});");
}
public void testForInLoops_acrossModules() {
assumeCrossModuleNames = false;
test(createModules("for (var i in {});", "i"), new String[] {"for (_.i in {});", "_.i"});
test(
createModules(" for (var [ a, b] in {});", "a;"),
new String[] {"var b; for ( [_.a, b] in {});", "_.a"});
test(
createModules(" for (var {i: i, c: c} in {});", "c"),
new String[] {"var i; for ( {i: i, c: _.c} in {});", "_.c"});
}
public void testForOfLoops_allSameModule() {
assumeCrossModuleNames = false;
testSame("for (var i of [1, 2, 3]);");
testSame("for (var [a] of []);");
testSame("for (var {a: a} of {});");
}
public void testForOfLoops_acrossModules() {
assumeCrossModuleNames = false;
test(createModules("for (var i of []);", "i"), new String[] {"for (_.i of []);", "_.i"});
test(
createModules(" for (var [ a, b] of []);", "a;"),
new String[] {"var b; for ( [_.a, b] of []);", "_.a"});
test(
createModules(" for (var {i: i, c: c} of []);", "c"),
new String[] {"var i; for ( {i: i, c: _.c} of []);", "_.c"});
}
public void testFunctionStatements() {
test(
"function test(){}",
"_.test=function (){}");
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT_2015);
test(
"if(1)function test(){}",
"if(1)_.test=function (){}");
}
public void testFunctionStatements_allSameModule() {
assumeCrossModuleNames = false;
test("function f() {}", "var f = function() {}");
test("if (true) { function f() {} }", "if (true) { var f = function() {}; }");
}
public void testFunctionStatements_freeCallSemantics1() throws Exception {
disableCompareAsTree();
// This triggers free call.
test(
"function x(){this};var y=function(){var val=x()||{}}",
"_.x=function(){this};_.y=function(){var val=(0,_.x)()||{}}");
test(
"function x(){this;x()}",
"_.x=function(){this;(0,_.x)()}");
test(
"var a=function(){this};a()",
"_.a=function(){this};(0,_.a)()");
// Always trigger free calls for variables assigned through destructuring.
test("var {a: a} = {a: function() {}}; a();", "({a:_.a}={a:function(){}});(0,_.a)()");
}
public void testFunctionStatements_freeCallSemantics2() {
// Cases where free call forcing through (0, foo)() is not necessary.
test(
"var a=function(){};a()",
"_.a=function(){};_.a()");
test(
"function a(){};a()",
"_.a=function(){};;_.a()");
test(
"var a;a=function(){};a()",
"_.a=function(){};_.a()");
}
public void testFunctionStatements_freeCallSemantics3() {
disableCompareAsTree();
// Ambiguous cases.
test("var a=1;a=function(){};a()", "_.a=1;_.a=function(){};(0,_.a)()");
test(
"var b;var a=b;a()",
"_.a=_.b;(0,_.a)()");
}
public void testDeeperScopes() {
test(
"var a = function(b){return b}",
"_.a = function(b){return b}");
test(
"var a = function(b){var a; return a+b}",
"_.a = function(b){var a; return a+b}");
test(
"var a = function(a,b){return a+b}",
"_.a = function(a,b){return a+b}");
test(
"var x=1,a = function(b){var a; return a+b+x}",
"_.x=1;_.a = function(b){var a; return a+b+_.x}");
test(
"var x=1,a = function(b){return function(){var a;return a+b+x}}",
"_.x=1;_.a = function(b){return function(){var a; return a+b+_.x}}");
}
public void testTryCatch() {
test(
"try{var a = 1}catch(e){throw e}",
"try{_.a = 1}catch(e){throw e}");
}
public void testShadowInFunctionScope() {
test(
"var _ = 1; (function () { _ = 2 })()",
"_._ = 1; (function () { _._ = 2 })()");
test(
"function foo() { var _ = {}; _.foo = foo; _.bar = 1; }",
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1}");
test(
"function foo() { var {key: _} = {}; _.foo = foo; _.bar = 1; }",
"_.foo = function () { var {key: _$} = {}; _$.foo = _.foo; _$.bar = 1}");
test(
lines(
"function foo() { var _ = {}; _.foo = foo; _.bar = 1; ",
"(function() { var _ = 0;})() }"),
lines(
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1; ",
"(function() { var _$ = 0;})() }"));
test(
"function foo() { var _ = {}; _.foo = foo; _.bar = 1; "
+ "var _$ = 1; }",
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1; "
+ "var _$$ = 1; }");
test(
"function foo() { var _ = {}; _.foo = foo; _.bar = 1; "
+ "var _$ = 1; (function() { _ = _$ })() }",
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1; "
+ "var _$$ = 1; (function() { _$ = _$$ })() }");
test(
"function foo() { var _ = {}; _.foo = foo; _.bar = 1; "
+ "var _$ = 1, _$$ = 2 (function() { _ = _$ = _$$; " +
"var _$, _$$$ })() }",
"_.foo = function () { var _$ = {}; _$.foo = _.foo; _$.bar = 1; "
+ "var _$$ = 1, _$$$ = 2 (function() { _$ = _$$ = _$$$; "
+ "var _$$, _$$$$ })() }");
test(
"var a = 5; function foo(_) { return _; }", "_.a = 5; _.foo = function(_$) { return _$; }");
test(
"var a = 5; function foo({key: _}) { return _; }",
"_.a = 5; _.foo = function({key: _$}) { return _$; }");
test(
"var a = 5; function foo([_]) { return _; }",
"_.a = 5; _.foo = function([_$]) { return _$; }");
// We accept this unnecessary renaming as acceptable to simplify pattern
// matching in the traversal.
test("function foo() { var _$a = 1;}", "_.foo = function () { var _$a$ = 1;}");
}
public void testShadowInBlockScope() {
test(
"var foo = 1; if (true) { const _ = {}; _.foo = foo; _.bar = 1; }",
"_.foo = 1; if (true) { const _$ = {}; _$.foo = _.foo; _$.bar = 1}");
test(
"var foo = 1; if (true) { const {key: _} = {}; _.foo = foo; _.bar = 1; }",
"_.foo = 1; if (true) { const {key: _$} = {}; _$.foo = _.foo; _$.bar = 1}");
test(
"var foo = 1; if (true) { const _ = {}; _.foo = foo; _.bar = 1; const _$ = 1; }",
"_.foo = 1; if (true) { const _$ = {}; _$.foo = _.foo; _$.bar = 1; const _$$ = 1; }");
test(
"var foo = 1; if (true) { const [_] = [{}]; _.foo = foo; }",
" _.foo = 1; if (true) { const [_$] = [{}]; _$.foo = _.foo; }");
}
public void testExterns() {
test(
"var document;",
"document",
"window.document");
test(
"var document;",
"document.getElementsByTagName('test')",
"window.document.getElementsByTagName('test')");
test(
"var document;",
"window.document.getElementsByTagName('test')",
"window.document.getElementsByTagName('test')");
test(
"var document;document.getElementsByTagName",
"document.getElementsByTagName('test')",
"window.document.getElementsByTagName('test')");
test(
"var document,navigator",
"document.navigator;navigator",
"window.document.navigator;window.navigator");
test(
"var iframes",
"function test() { iframes.resize(); }",
"_.test = function() { window.iframes.resize(); }");
test(
"var iframes",
"var foo = iframes;",
"_.foo = window.iframes;");
test("class A {}", "A", "window.A");
// Special names.
test(
"var arguments, window, eval;",
"arguments;window;eval;",
"arguments;window;eval;");
// Actually not an extern.
test(
"",
"document",
"window.document");
// Javascript builtin objects
testSame(
"Object;Function;Array;String;Boolean;Number;Math;"
+ "Date;RegExp;JSON;Error;EvalError;ReferenceError;"
+ "SyntaxError;TypeError;URIError;");
}
public void testSameVarDeclaredInExternsAndSource() {
test(
"/** @const */ var ns = {}; function f() {}",
"/** @const */ var ns = ns || {};",
"/** @const */ window.ns = window.ns || {};");
test(
"var x;",
"var x = 1; x = 2;",
"window.x = 1; window.x = 2;");
test(
"var x;",
"var x; x = 1;",
"window.x = 1;");
test(
"var x;",
"function f() { var x; x = 1; }",
"_.f = function() { var x; x = 1; }");
test(
"var x, y;",
"var x = 1, y = 2;",
"window.x = 1; window.y = 2;");
test(
"var x, y;",
"var x, y = 2;",
"window.y = 2;");
test(
"var x;",
"var x = 1, y = 2;",
"window.x = 1; _.y = 2;");
test(
"var x;",
"var y = 2, x = 1;",
"_.y = 2; window.x = 1;");
test(
"var x;",
"var y, x = 1;",
"window.x = 1;");
test(
"var foo;",
"var foo = function(x) { if (x > 0) { var y = foo; } };",
"window.foo = function(x) { if (x > 0) { var y = window.foo; } };");
// The parameter x doesn't conflict with the x in the source
test(
"function f(x) {}",
"var f = 1; var x = 2;",
"window.f = 1; _.x = 2;");
}
public void testSameVarDeclaredInExternsAndSource2() {
assumeCrossModuleNames = false;
test(createModules(
lines(
"Foo = function() { this.b = ns; };",
"var f = function(a) {",
" if (a instanceof Foo && a.b === ns) {}",
"},",
"ns = {},",
"g = function(a) { var b = new Foo; };"),
"f; g;"),
new String[] {
lines(
"var ns;",
"window.Foo = function() { this.b = ns; };",
"_.f = function(a) {",
" if (a instanceof window.Foo && a.b === ns) {}",
"};",
"ns = {};",
"_.g = function(a) { var b = new window.Foo; };"),
"_.f; _.g;"
});
test(
"var y;",
"var x = 1, y = 2; function f() { return x + window.y; }",
"var x; x = 1; window.y = 2; var f = function() { return x + window.y; }");
}
}