/
NameAnalyzerTest.java
2439 lines (1994 loc) · 68.5 KB
/
NameAnalyzerTest.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2006 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;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.rhino.Node;
/**
* Tests for {@link NameAnalyzer}
*
*/
public final class NameAnalyzerTest extends CompilerTestCase {
private static final String EXTERNS =
lines(
"var window, top;",
"var document;",
"var Function;",
"var Array;",
"var externfoo; methods.externfoo;");
public NameAnalyzerTest() {
super(EXTERNS);
}
@Override
protected int getNumRepetitions() {
// pass reaches steady state after 1 iteration.
return 1;
}
@Override
protected CompilerPass getProcessor(Compiler compiler) {
return new MarkNoSideEffectCallsAndNameAnalyzerRunner(compiler);
}
private static class MarkNoSideEffectCallsAndNameAnalyzerRunner implements CompilerPass {
MarkNoSideEffectCalls markNoSideEffectCalls;
NameAnalyzer analyzer;
MarkNoSideEffectCallsAndNameAnalyzerRunner(Compiler compiler) {
this.markNoSideEffectCalls = new MarkNoSideEffectCalls(compiler);
this.analyzer = new NameAnalyzer(compiler, true, null);
}
@Override
public void process(Node externs, Node root) {
markNoSideEffectCalls.process(externs, root);
analyzer.process(externs, root);
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2017);
enableNormalize();
}
public void testRemoveVarDeclaration1() {
test("var foo = 3;", "");
}
public void testRemoveVarDeclaration2() {
test("var foo = 3, bar = 4; externfoo = foo;",
"var foo = 3; externfoo = foo;");
}
public void testRemoveVarDeclaration3() {
test("var a = f(), b = 1, c = 2; b; c", "f();var b = 1, c = 2; b; c");
}
public void testRemoveVarDeclaration4() {
test("var a = 0, b = f(), c = 2; a; c", "var a = 0;f();var c = 2; a; c");
}
public void testRemoveVarDeclaration5() {
test("var a = 0, b = 1, c = f(); a; b", "var a = 0, b = 1; f(); a; b");
}
public void testRemoveVarDeclaration6() {
test("var a = 0, b = a = 1; a", "var a = 0; a = 1; a");
}
public void testRemoveVarDeclaration7() {
test("var a = 0, b = a = 1", "");
}
public void testRemoveVarDeclaration8() {
test("var a;var b = 0, c = a = b = 1", "");
}
public void testRemoveLetDeclaration1() {
test("let foo = 3;", "");
}
public void testRemoveLetDeclaration2() {
test("let foo = 3, bar = 4; externfoo = foo;",
"let foo = 3; externfoo = foo;");
}
public void testRemoveLetDeclaration3() {
test("let a = f(), b = 1, c = 2; b; c", "f();let b = 1, c = 2; b; c");
}
public void testRemoveLetDeclaration4() {
test("let a = 0, b = f(), c = 2; a; c", "let a = 0;f();let c = 2; a; c");
}
public void testRemoveLetDeclaration5() {
test("let a = 0, b = 1, c = f(); a; b", "let a = 0, b = 1; f(); a; b");
}
public void testRemoveLetDeclaration6() {
test("let a = 0, b = a = 1; a", "let a = 0; a = 1; a");
}
public void testRemoveLetDeclaration7() {
test("let a = 0, b = a = 1", "");
}
public void testRemoveLetDeclaration8() {
test("let a;let b = 0, c = a = b = 1", "");
}
public void testRemoveLetDeclaration9() {
// The variable inside the block doesn't get removed (but does get renamed by Normalize).
test(
"let x = 1; if (true) { let x = 2; x; }",
"if (true) { let x$jscomp$1 = 2; x$jscomp$1; }");
}
// Let/const defined variables in blocks are not global so NameAnalyzer doesn't remove them.
public void testDontRemoveLetInBlock1() {
testSame(
LINE_JOINER.join(
"if (true) {",
" let x = 1; alert(x);",
"}"));
testSame(
LINE_JOINER.join(
"if (true) {",
" let x = 1;",
"}"));
}
public void testDontRemoveLetInBlock2() {
testSame(
LINE_JOINER.join(
"if (true) {",
" let x = 1; alert(x);",
"} else {",
" let x = 1; alert(x);",
"}"));
testSame(
LINE_JOINER.join(
"if (true) {",
" let x = 1;",
"} else {",
" let x = 1;",
"}"));
}
public void testRemoveConstDeclaration1() {
test("const a = 4;", "");
}
public void testRemoveConstDeclaration2() {
testSame("const a = 4; window.x = a;");
}
public void testRemoveConstDeclaration3() {
// The variable inside the block doesn't get removed (but does get renamed by Normalize).
test(
"const x = 1; if (true) { const x = 2; x; }",
"if (true) { const x$jscomp$1 = 2; x$jscomp$1; }");
}
public void testRemoveDeclaration1() {
test("var a;var b = 0, c = a = b = 1", "");
}
public void testRemoveDeclaration2() {
test("var a,b,c; c = a = b = 1", "");
}
public void testRemoveDeclaration3() {
test("var a,b,c; c = a = b = {}; a.x = 1;", "");
}
public void testRemoveDeclaration4() {
testSame("var a,b,c; c = a = b = {}; a.x = 1;alert(c.x);");
}
public void testRemoveDeclaration5() {
test("var a,b,c; c = a = b = null; use(b)", "var b;b=null;use(b)");
}
public void testRemoveDeclaration6() {
test("var a,b,c; c = a = b = 'str';use(b)", "var b;b='str';use(b)");
}
public void testRemoveDeclaration7() {
test("var a,b,c; c = a = b = true;use(b)", "var b;b=true;use(b)");
}
public void testRemoveFunction1() {
test("var foo = function(){};", "");
}
public void testRemoveFunction2() {
test("var foo; foo = function(){};", "");
}
public void testRemoveFunction3() {
test("var foo = {}; foo.bar = function() {};", "");
}
public void testRemoveFunction4() {
test("var a = {}; a.b = {}; a.b.c = function() {};", "");
}
public void testDontRemoveFunctionOnNamespaceThatEscapes() {
testSame("var a = foo(); a.b = {}; a.b.c = function() {};");
}
public void testReferredToByWindow() {
testSame("var foo = {}; foo.bar = function() {}; window['fooz'] = foo.bar");
}
public void testExtern() {
testSame("externfoo = 5");
}
public void testRemoveNamedFunction() {
test("function foo(){}", "");
}
public void testRemoveRecursiveFunction1() {
test("function f(){f()}", "");
}
public void testRemoveRecursiveFunction2() {
test("var f = function (){f()}", "");
}
public void testRemoveRecursiveFunction2a() {
test("var f = function g(){g()}", "");
}
public void testRemoveRecursiveFunction3() {
test("var f;f = function (){f()}", "");
}
public void testRemoveRecursiveFunction4() {
// don't removed if name definition doesn't exist.
testSame("f = function (){f()}");
}
public void testRemoveRecursiveFunction5() {
test("function g(){f()}function f(){g()}", "");
}
public void testRemoveRecursiveFunction6() {
test("var f=function(){g()};function g(){f()}", "");
}
public void testRemoveRecursiveFunction7() {
test("var g = function(){f()};var f = function(){g()}", "");
}
public void testRemoveRecursiveFunction8() {
test("var o = {};o.f = function(){o.f()}", "");
}
public void testRemoveRecursiveFunction9() {
testSame("var o = {};o.f = function(){o.f()};o.f()");
}
public void testSideEffectClassification1() {
testSame("foo();");
}
public void testSideEffectClassification2() {
test("var a = foo();", "foo();");
}
public void testSideEffectClassification3() {
testSame("var a = foo();window['b']=a;");
}
public void testSideEffectClassification4() {
testSame("function sef(){} sef();");
}
public void testSideEffectClassification5() {
testSame("function nsef(){} var a = nsef();window['b']=a;");
}
public void testSideEffectClassification6() {
testSame("function sef(){} sef();");
}
public void testSideEffectClassification7() {
testSame("function sef(){} var a = sef();window['b']=a;");
}
public void testNoSideEffectAnnotation1() {
test("function f(){} var a = f();",
"function f(){} f()");
test("function f(){} let a = f();",
"function f(){} f()");
test("function f(){} const a = f();",
"function f(){} f()");
}
public void testNoSideEffectAnnotation2() {
test(
"/**@nosideeffects*/function f(){}",
"var a = f();",
"");
}
public void testNoSideEffectAnnotation3() {
test("var f = function(){}; var a = f();",
"var f = function(){}; f();");
}
public void testNoSideEffectAnnotation4() {
test("var f = /**@nosideeffects*/function(){};", "var a = f();",
"");
}
public void testNoSideEffectAnnotation5() {
test("var f; f = function(){}; var a = f();",
"var f; f = function(){}; f();");
}
public void testNoSideEffectAnnotation6() {
test("f = /**@nosideeffects*/function(){};", "var a = f();",
"");
}
public void testNoSideEffectAnnotation7() {
test("f = /**@nosideeffects*/function(){};",
"f = function(){};" +
"var a = f();",
"f = function(){}; f();");
}
public void testNoSideEffectAnnotation8() {
test("f = function(){};" +
"f = /**@nosideeffects*/function(){};",
"var a = f();",
"f();");
}
public void testNoSideEffectAnnotation9() {
test("f = /**@nosideeffects*/function(){};" +
"f = /**@nosideeffects*/function(){};",
"var a = f();",
"");
test("f = /**@nosideeffects*/function(){};", "var a = f();",
"");
}
public void testNoSideEffectAnnotation10() {
test("var o = {}; o.f = function(){}; var a = o.f();",
"var o = {}; o.f = function(){}; o.f();");
}
public void testNoSideEffectAnnotation11() {
test(
"var o = {}; o.f = /**@nosideeffects*/function(){};",
"var a = o.f();",
"");
}
public void testNoSideEffectAnnotation12() {
test("function c(){} var a = new c",
"function c(){} new c");
}
public void testNoSideEffectAnnotation13() {
test(
"/**@nosideeffects*/function c(){}",
"var a = new c",
"");
}
public void testNoSideEffectAnnotation14() {
String externs =
"function c(){};"
+ "c.prototype.f = /**@nosideeffects*/function(){};";
test(
externs,
"var o = new c; var a = o.f()",
"new c");
}
public void testNoSideEffectAnnotation15() {
test("function c(){}; c.prototype.f = function(){}; var a = (new c).f()",
"function c(){}; c.prototype.f = function(){}; (new c).f()");
}
public void testNoSideEffectAnnotation16() {
test("/**@nosideeffects*/function c(){}" +
"c.prototype.f = /**@nosideeffects*/function(){};",
"var a = (new c).f()",
"");
}
public void testFunctionPrototype() {
testSame("var a = 5; Function.prototype.foo = function() {return a;}");
}
public void testTopLevelClass1() {
test("var Point = function() {}; Point.prototype.foo = function() {}", "");
}
public void testTopLevelClass2() {
testSame("var Point = {}; Point.prototype.foo = function() {};" +
"externfoo = new Point()");
}
public void testTopLevelClass3() {
test("function Point() {this.me_ = Point}", "");
}
public void testTopLevelClass4() {
test("function f(){} function A(){} A.prototype = {x: function() {}}; f();",
"function f(){} f();");
}
public void testTopLevelClass5() {
testSame("function f(){} function A(){}" +
"A.prototype = {x: function() { f(); }}; new A();");
}
public void testTopLevelClass6() {
testSame("function f(){} function A(){}" +
"A.prototype = {x: function() { f(); }}; new A().x();");
}
public void testTopLevelClass7() {
test("A.prototype.foo = function(){}; function A() {}", "");
}
public void testNamespacedClass1() {
test("var foo = {};foo.bar = {};foo.bar.prototype.baz = {}", "");
}
public void testNamespacedClass2() {
testSame("var foo = {};foo.bar = {};foo.bar.prototype.baz = {};" +
"window.z = new foo.bar()");
}
public void testNamespacedClass3() {
test("var a = {}; a.b = function() {}; a.b.prototype = {x: function() {}};",
"");
}
public void testNamespacedClass4() {
testSame("function f(){} var a = {}; a.b = function() {};" +
"a.b.prototype = {x: function() { f(); }}; new a.b();");
}
public void testNamespacedClass5() {
testSame("function f(){} var a = {}; a.b = function() {};" +
"a.b.prototype = {x: function() { f(); }}; new a.b().x();");
}
public void testEs6Class() {
test("class C {}", "");
test("class C {constructor() {} }", "");
testSame("class C {} var c = new C(); g(c);");
testSame(
LINE_JOINER.join(
"class C {",
" constructor() {",
" this.x = 1;",
" }",
" add() {",
" this.x++",
" }",
"}",
"var c = new C;",
"c.add();"));
test("class C{} class D{} var d = new D;", "class D{} new D;");
testSame("{class C{} }"); // global classes only
testSame("{class C{} var c = new C; f(c)}");
testSame("function f() { return class C {} } f();");
testSame("export class C {}");
// class expressions
test("var c = class{}", "");
testSame("var c = class{}; g(c);");
test("var c = class C {}", "");
testSame("var c = class C{}; g(c);");
}
public void testEs6ClassExtends() {
testSame("class D {} class C extends D {} var c = new C; c.g();");
test("class D {} class C extends D {}" , "");
}
/** @bug 67430253 */
public void testEs6ClassExtendsQualifiedName1() {
testSame("var ns = {}; ns.Class1 = class {}; class Class2 extends ns.Class1 {}; use(Class2);");
}
/** @bug 67430253 */
public void testEs6ClassExtendsQualifiedName2() {
test(
"var ns = {}; ns.Class1 = class {}; use(ns.Class1); class Class2 extends ns.Class1 {}",
"var ns = {}; ns.Class1 = class {}; use(ns.Class1);");
}
public void testAssignmentToThisPrototype() {
testSame("Function.prototype.inherits = function(parentCtor) {" +
" function tempCtor() {};" +
" tempCtor.prototype = parentCtor.prototype;" +
" this.superClass_ = parentCtor.prototype;" +
" this.prototype = new tempCtor();" +
" this.prototype.constructor = this;" +
"};");
}
public void testAssignmentToCallResultPrototype() {
testSame("function f() { return function(){}; } f().prototype = {};");
}
public void testAssignmentToExternPrototype() {
testSame("externfoo.prototype = {};");
}
public void testAssignmentToUnknownPrototype() {
testSame(
"/** @suppress {duplicate} */ var window;" +
"window['a'].prototype = {};");
}
public void testBug2099540() {
testSame(
"/** @suppress {duplicate} */ var document;\n" +
"/** @suppress {duplicate} */ var window;\n" +
"var klass;\n" +
"window[klass].prototype = " +
"document.createElement(tagName)['__proto__'];");
}
public void testOtherGlobal() {
testSame("goog.global.foo = bar(); function bar(){}");
}
public void testExternName1() {
testSame("top.z = bar(); function bar(){}");
}
public void testExternName2() {
testSame("top['z'] = bar(); function bar(){}");
}
public void testInherits1() {
test("var a = {}; var b = {}; var goog = {}; goog.inherits(b, a)", "");
}
public void testInherits2() {
testSame("var a = {}; this.b = {}; b.inherits(a);");
}
public void testInherits3() {
testSame("var a = {}; this.b = {}; var goog = {}; goog.inherits(b, a);");
}
public void testInherits4() {
test("this.a = {}; var b = {}; var goog = {}; goog.inherits(b, a);",
"this.a = {}");
}
public void testInherits5() {
testSame(
LINE_JOINER.join(
"var a = {};",
"this.b = {};",
"var goog = {};",
"goog.inherits = function() {};",
"goog.inherits(b, a);"));
}
public void testMixin1() {
testSame("var goog = {}; goog.mixin = function() {};" +
"Function.prototype.mixin = function(base) {" +
" goog.mixin(this.prototype, base); " +
"};");
}
public void testMixin2() {
testSame("var a = {}; this.b = {}; var goog = {};" +
" goog.mixin = function() {}; goog.mixin(b.prototype, a.prototype);");
}
public void testMixin3() {
test("this.a = {}; var b = {}; var goog = {};" +
" goog.mixin = function() {}; goog.mixin(b.prototype, a.prototype);",
"this.a = {};");
}
public void testMixin4() {
testSame("this.a = {}; var b = {}; var goog = {};" +
"goog.mixin = function() {};" +
"goog.mixin(b.prototype, a.prototype);" +
"new b()");
}
public void testMixin5() {
test("this.a = {}; var b = {}; var c = {}; var goog = {};" +
"goog.mixin = function() {};" +
"goog.mixin(b.prototype, a.prototype);" +
"goog.mixin(c.prototype, a.prototype);" +
"new b()",
"this.a = {}; var b = {}; var goog = {};" +
"goog.mixin = function() {};" +
"goog.mixin(b.prototype, a.prototype);" +
"new b()");
}
public void testMixin6() {
testSame("this.a = {}; var b = {}; var c = {}; var goog = {};" +
"goog.mixin = function() {};" +
"goog.mixin(c.prototype, a.prototype) + " +
"goog.mixin(b.prototype, a.prototype);" +
"new b()");
}
public void testMixin7() {
test("this.a = {}; var b = {}; var c = {}; var goog = {};" +
"goog.mixin = function() {};" +
"var d = goog.mixin(c.prototype, a.prototype) + " +
"goog.mixin(b.prototype, a.prototype);" +
"new b()",
"this.a = {}; var b = {}; var goog = {};" +
"goog.mixin = function() {};" +
"goog.mixin(b.prototype, a.prototype);" +
"new b()");
}
public void testConstants1() {
testSame("var bar = function(){}; var EXP_FOO = true; if (EXP_FOO) bar();");
}
public void testConstants2() {
test("var bar = function(){}; var EXP_FOO = true; var EXP_BAR = true;" +
"if (EXP_FOO) bar();",
"var bar = function(){}; var EXP_FOO = true; if (EXP_FOO) bar();");
}
public void testExpressions1() {
test("var foo={}; foo.A='A'; foo.AB=foo.A+'B'; foo.ABC=foo.AB+'C'",
"");
}
public void testExpressions2() {
testSame("var foo={}; foo.A='A'; foo.AB=foo.A+'B'; this.ABC=foo.AB+'C'");
}
public void testExpressions3() {
testSame("var foo = 2; window.bar(foo + 3)");
}
public void testSetCreatingReference() {
testSame("var foo; var bar = function(){foo=6;}; bar();");
}
public void testAnonymous1() {
testSame("function foo() {}; function bar() {}; foo(function() {bar()})");
}
public void testAnonymous2() {
test("var foo;(function(){foo=6;})()", "(function(){})()");
}
public void testAnonymous3() {
testSame("var foo; (function(){ if(!foo)foo=6; })()");
}
public void testAnonymous4() {
testSame("var foo; (function(){ foo=6; })(); externfoo=foo;");
}
public void testAnonymous5() {
testSame("var foo;" +
"(function(){ foo=function(){ bar() }; function bar(){} })();" +
"foo();");
}
public void testAnonymous6() {
testSame("function foo(){}" +
"function bar(){}" +
"foo(function(){externfoo = bar});");
}
public void testAnonymous7() {
testSame("var foo;" +
"(function (){ function bar(){ externfoo = foo; } bar(); })();");
}
public void testAnonymous8() {
testSame("var foo;" +
"(function (){ var g=function(){ externfoo = foo; }; g(); })();");
}
public void testAnonymous9() {
testSame("function foo(){}" +
"function bar(){}" +
"foo(function(){ function baz(){ externfoo = bar; } baz(); });");
}
public void testFunctions1() {
testSame("var foo = null; function baz() {}" +
"function bar() {foo=baz();} bar();");
}
public void testFunctions2() {
testSame("var foo; foo = function() {var a = bar()};" +
"var bar = function(){}; foo();");
}
public void testGetElem1() {
testSame("var foo = {}; foo.bar = {}; foo.bar.baz = {a: 5, b: 10};" +
"var fn = function() {window[foo.bar.baz.a] = 5;}; fn()");
}
public void testGetElem2() {
testSame("var foo = {}; foo.bar = {}; foo.bar.baz = {a: 5, b: 10};" +
"var fn = function() {this[foo.bar.baz.a] = 5;}; fn()");
}
public void testGetElem3() {
testSame("var foo = {'i': 0, 'j': 1}; foo['k'] = 2; top.foo = foo;");
}
public void testIf1() {
test("var foo = {};if(e)foo.bar=function(){};", "if(e);");
}
public void testIf2() {
test("var e = false;var foo = {};if(e)foo.bar=function(){};",
"var e = false;if(e);");
}
public void testIf3() {
test("var e = false;var foo = {};if(e + 1)foo.bar=function(){};",
"var e = false;if(e + 1);");
}
public void testIf4() {
test("var e = false, f;var foo = {};if(f=e)foo.bar=function(){};",
"var e = false;if(e);");
}
public void testIf4a() {
// TODO(johnlenz): fix this.
testSame("var e = [], f;if(f=e);f[0] = 1;");
}
public void testIf4b() {
// TODO(johnlenz): fix this.
test("var e = [], f;if(e=f);f[0] = 1;",
"var f;if(f);f[0] = 1;");
}
public void testIf4c() {
test("var e = [], f;if(f=e);e[0] = 1;",
"var e = [];if(e);e[0] = 1;");
}
public void testIf5() {
test("var e = false, f;var foo = {};if(f = e + 1)foo.bar=function(){};",
"var e = false;if(e + 1);");
}
public void testIfElse() {
test("var foo = {};if(e)foo.bar=function(){};else foo.bar=function(){};",
"if(e);else;");
}
public void testWhile() {
test("var foo = {};while(e)foo.bar=function(){};", "while(e);");
}
public void testForIn() {
test("var foo = {};for(e in x)foo.bar=function(){};", "for(e in x);");
}
public void testForOf() {
test("var foo = {};for(e of x)foo.bar=function(){};", "for(e of x);");
}
public void testDo() {
test("var cond = false;do {var a = 1} while (cond)",
"var cond = false;do {} while (cond)");
}
public void testSetterInForStruct1() {
test("var j = 0; for (var i = 1; i = 0; j++);",
"var j = 0; for (; 0; j++);");
}
public void testSetterInForStruct2() {
test("var Class = function() {}; " +
"for (var i = 1; Class.prototype.property_ = 0; i++);",
"for (var i = 1; 0; i++);");
}
public void testSetterInForStruct3() {
test("var j = 0; for (var i = 1 + f() + g() + h(); i = 0; j++);",
"var j = 0; f(); g(); h(); for (; 0; j++);");
}
public void testSetterInForStruct4() {
test("var i = 0;var j = 0; for (i = 1 + f() + g() + h(); i = 0; j++);",
"var j = 0; f(); g(); h(); for (; 0; j++);");
}
public void testSetterInForStruct5() {
test("var i = 0, j = 0; for (i = f(), j = g(); 0;);",
"for (f(), g(); 0;);");
}
public void testSetterInForStruct6() {
test("var i = 0, j = 0, k = 0; for (i = f(), j = g(), k = h(); i = 0;);",
"for (f(), g(), h(); 0;);");
}
public void testSetterInForStruct7() {
test("var i = 0, j = 0, k = 0; for (i = 1, j = 2, k = 3; i = 0;);",
"for (1, 2, 3; 0;);");
}
public void testSetterInForStruct8() {
test("var i = 0, j = 0, k = 0; for (i = 1, j = i, k = 2; i = 0;);",
"var i = 0; for(i = 1, i , 2; i = 0;);");
}
public void testSetterInForStruct9() {
test("var Class = function() {}; " +
"for (var i = 1; Class.property_ = 0; i++);",
"for (var i = 1; 0; i++);");
}
public void testSetterInForStruct10() {
test("var Class = function() {}; " +
"for (var i = 1; Class.property_ = 0; i = 2);",
"for (; 0;);");
}
public void testSetterInForStruct11() {
test("var Class = function() {}; " +
"for (;Class.property_ = 0;);",
"for (;0;);");
}
public void testSetterInForStruct12() {
test("var a = 1; var Class = function() {}; " +
"for (;Class.property_ = a;);",
"var a = 1; for (; a;);");
}
public void testSetterInForStruct13() {
test("var a = 1; var Class = function() {}; " +
"for (Class.property_ = a; 0 ;);",
"for (; 0;);");
}
public void testSetterInForStruct14() {
test("var a = 1; var Class = function() {}; " +
"for (; 0; Class.property_ = a);",
"for (; 0;);");
}
public void testSetterInForStruct15() {
test("var Class = function() {}; " +
"for (var i = 1; 0; Class.prototype.property_ = 0);",
"for (; 0; 0);");
}
public void testSetterInForStruct16() {
test("var Class = function() {}; " +
"for (var i = 1; i = 0; Class.prototype.property_ = 0);",
"for (; 0; 0);");
}
public void testSetterInForIn1() {
test("var foo = {}; var bar; for(e in bar = foo.a);",
"var foo = {}; for(e in foo.a);");
}
public void testSetterInForIn2() {
testSame("var foo = {}; var bar; for(e in bar = foo.a); bar");
}
public void testSetterInForIn3() {
testSame("var foo = {}; var bar; for(e in bar = foo.a); bar.b = 3");
}
public void testSetterInForIn4() {
testSame("var foo = {}; var bar; for (e in bar = foo.a); bar.b = 3; foo.a");
}
public void testSetterInForIn5() {
// TODO(user) Fix issue similar to b/2316773: bar should be preserved
// but isn't due to missing references between e and foo.a
test("var foo = {}; var bar; for (e in foo.a) { bar = e } bar.b = 3; foo.a",
"var foo={};for(e in foo.a);foo.a");
}
public void testSetterInForIn6() {
testSame("var foo = {};for(e in foo);");
}
public void testSetterInForOf1() {
test("var foo = {}; var bar; for(e of bar = foo.a);",
"var foo = {}; for(e of foo.a);");
}
public void testSetterInForOf2() {
testSame("var foo = {}; var bar; for(e of bar = foo.a); bar");
}
public void testSetterInForOf3() {
testSame("var foo = {}; var bar; for(e of bar = foo.a); bar.b = 3");
}
public void testSetterInForOf4() {
testSame("var foo = {}; var bar; for (e of bar = foo.a); bar.b = 3; foo.a");
}
public void testSetterInForOf5() {
test("var foo = {}; var bar; for (e of foo.a) { bar = e } bar.b = 3; foo.a",
"var foo={};for(e of foo.a);foo.a");
}
public void testSetterInForOf6() {
testSame("var foo = {};for(e of foo);");
}
public void testSetterInIfPredicate() {
// TODO(user) Make NameAnalyzer smarter so it can remove "Class".
testSame("var a = 1;" +
"var Class = function() {}; " +
"if (Class.property_ = a);");
}
public void testSetterInWhilePredicate() {
test("var a = 1;" +
"var Class = function() {}; " +
"while (Class.property_ = a);",
"var a = 1; for (;a;) {}");
}
public void testSetterInDoWhilePredicate() {
// TODO(user) Make NameAnalyzer smarter so it can remove "Class".
testSame("var a = 1;" +
"var Class = function() {}; " +
"do {} while(Class.property_ = a);");
}
public void testSetterInSwitchInput() {
// TODO(user) Make NameAnalyzer smarter so it can remove "Class".
testSame("var a = 1;" +
"var Class = function() {}; " +
"switch (Class.property_ = a) {" +