-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RemoveUnusedCodeNameAnalyzerTest.java
3012 lines (2567 loc) · 81.8 KB
/
RemoveUnusedCodeNameAnalyzerTest.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;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Tests {@link RemoveUnusedCode} for functionality that was previously implemented in NameAnalyzer
* aka smartNamePass, which has now been removed.
*/
@RunWith(JUnit4.class)
public final class RemoveUnusedCodeNameAnalyzerTest extends CompilerTestCase {
private static final String EXTERNS =
lines(
"/**",
" * @constructor",
" * @param {*=} opt_value",
" * @return {!Object}",
" */",
"function Object(opt_value) {}",
"/** @type {Function} */",
"Object.defineProperties = function() {};",
"/**",
" * @constructor",
" * @param {string} message",
" */",
"function Error(message) {}",
"var window, top, console;",
"var document;",
"var Function;",
"var Array;",
"var goog = {};",
"goog.inherits = function(childClass, parentClass) {};",
"goog.mixin = function(target, base) {};",
"function goog$addSingletonGetter(a){}",
"var externfoo = {};",
"externfoo.externProp1 = 1;",
"externfoo.externProp2 = 2;",
"function doThing1() {}",
"function doThing2() {}",
"function use(something) {}",
"function alert(something) {}",
"function sideEffect() {}");
public RemoveUnusedCodeNameAnalyzerTest() {
super(EXTERNS);
}
@Override
protected int getNumRepetitions() {
// pass reaches steady state after 1 iteration.
return 1;
}
@Override
protected CompilerPass getProcessor(Compiler compiler) {
return new MarkNoSideEffectCallsAndRemoveUnusedCodeRunner(compiler);
}
private static class MarkNoSideEffectCallsAndRemoveUnusedCodeRunner implements CompilerPass {
PureFunctionIdentifier.Driver pureFunctionIdentifier;
RemoveUnusedCode removeUnusedCode;
MarkNoSideEffectCallsAndRemoveUnusedCodeRunner(Compiler compiler) {
this.pureFunctionIdentifier = new PureFunctionIdentifier.Driver(compiler);
this.removeUnusedCode =
new RemoveUnusedCode.Builder(compiler)
.removeGlobals(true)
.removeLocalVars(true)
.removeUnusedPrototypeProperties(true)
.removeUnusedThisProperties(true)
.removeUnusedConstructorProperties(true)
.removeUnusedObjectDefinePropertiesDefinitions(true)
// Removal of function expression names isn't what these tests are about.
// It just adds noise to the tests when we can't use testSame() because of it.
.preserveFunctionExpressionNames(true)
.build();
}
@Override
public void process(Node externs, Node root) {
pureFunctionIdentifier.process(externs, root);
removeUnusedCode.process(externs, root);
}
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2017);
enableNormalize();
enableGatherExternProperties();
}
@Test
public void testDefaultingAssignmentWithAssignedProperty() {
test("var x = function() {}; x.externProp1 = function() {}; var x = x || {};", "");
}
@Test
public void testRemoveVarDeclaration1() {
test("var foo = 3;", "");
}
@Test
public void testRemoveVarDeclaration2() {
test("var foo = 3, bar = 4; externfoo = foo;", "var foo = 3; externfoo = foo;");
}
@Test
public void testRemoveVarDeclaration3() {
// TODO(b/66971163): Don't block removal when the reference to a name is not actually used.
// e.g. `b; c` below.
test(
"var a = doThing1(), b = 1, c = 2; b; c", // preserve newline
" doThing1(); var b = 1, c = 2; b; c");
}
@Test
public void testRemoveVarDeclaration4() {
test(
"var a = 0, b = doThing1(), c = 2; a; c", // preserve newline
"var a = 0; doThing1(); var c = 2; a; c");
}
@Test
public void testRemoveVarDeclaration5() {
test(
"var a = 0, b = 1, c = doThing1(); a; b", // preserve newline
"var a = 0, b = 1; doThing1(); a; b");
}
@Test
public void testRemoveVarDeclaration6() {
test("var a = 0, b = a = 1; a", "var a = 0; a = 1; a");
}
@Test
public void testRemoveVarDeclaration7() {
test("var a = 0, b = a = 1", "");
}
@Test
public void testRemoveVarDeclaration8() {
test("var a;var b = 0, c = a = b = 1", "");
}
@Test
public void testRemoveLetDeclaration1() {
test("let foo = 3;", "");
}
@Test
public void testRemoveLetDeclaration2() {
test("let foo = 3, bar = 4; externfoo = foo;", "let foo = 3; externfoo = foo;");
}
@Test
public void testRemoveLetDeclaration3() {
test(
"let a = doThing1(), b = 1, c = 2; b; c", // preserve newline
" doThing1(); let b = 1, c = 2; b; c");
}
@Test
public void testRemoveLetDeclaration4() {
test(
"let a = 0, b = doThing1(), c = 2; a; c", // preserve newline
"let a = 0; doThing1(); let c = 2; a; c");
}
@Test
public void testRemoveLetDeclaration5() {
test(
"let a = 0, b = 1, c = doThing1(); a; b", // preserve newline
"let a = 0, b = 1; doThing1(); a; b");
}
@Test
public void testRemoveLetDeclaration6() {
test("let a = 0, b = a = 1; a", "let a = 0; a = 1; a");
}
@Test
public void testRemoveLetDeclaration7() {
test("let a = 0, b = a = 1", "");
}
@Test
public void testRemoveLetDeclaration8() {
test("let a;let b = 0, c = a = b = 1", "");
}
@Test
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; }");
}
@Test
public void testRemoveLetInBlock1() {
testSame(
lines(
"if (true) {", // preserve newline
" let x = 1; alert(x);",
"}"));
test(
"if (true) { let x = 1; }", // preserve newline
"if (true) ;");
}
@Test
public void testRemoveLetInBlock2() {
testSame(
lines(
"if (true) {", // preserve newline
" let x = 1; alert(x);",
"} else {",
" let x = 1; alert(x);",
"}"));
test(
srcs(lines(
"if (true) {", // preserve newline
" let x = 1;",
"} else {",
" let x = 1;",
"}")),
expected("if (true); else;"));
}
@Test
public void testRemoveConstDeclaration1() {
test("const a = 4;", "");
}
@Test
public void testRemoveConstDeclaration2() {
testSame("const a = 4; window.x = a;");
}
@Test
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; }");
}
@Test
public void testRemoveDeclaration1() {
test("var a;var b = 0, c = a = b = 1", "");
}
@Test
public void testRemoveDeclaration2() {
test("var a,b,c; c = a = b = 1", "");
}
// TODO(b/66971163): enable or remove
public void disabledTestRemoveDeclaration3() {
test("var a,b,c; c = a = b = {}; a.x = 1;", "");
}
@Test
public void testRemoveDeclaration4() {
test(
"var a,b,c; c = a = b = {}; a.x = 1;alert(c.x);",
"var a, c; c = a = {}; a.x = 1;alert(c.x);");
}
@Test
public void testRemoveDeclaration5() {
test("var a,b,c; c = a = b = null; use(b)", "var b;b=null;use(b)");
}
@Test
public void testRemoveDeclaration6() {
test("var a,b,c; c = a = b = 'str';use(b)", "var b;b='str';use(b)");
}
@Test
public void testRemoveDeclaration7() {
test("var a,b,c; c = a = b = true;use(b)", "var b;b=true;use(b)");
}
@Test
public void testRemoveFunction1() {
test("var foo = function(){};", "");
}
@Test
public void testRemoveFunction2() {
test("var foo; foo = function(){};", "");
}
@Test
public void testRemoveFunction3() {
test("var foo = {}; foo.bar = function() {};", "");
}
// TODO(b/66971163): enable or remove this test
public void disabledTestRemoveFunction4() {
test("var a = {}; a.b = {}; a.b.c = function() {};", "");
}
@Test
public void testDontRemoveFunctionOnNamespaceThatEscapes() {
testSame("var a = doThing1(); a.b = {}; a.b.c = function() {};");
}
@Test
public void testReferredToByWindow() {
testSame("var foo = {}; foo.bar = function() {}; window['fooz'] = foo.bar");
}
@Test
public void testExtern() {
testSame("externfoo = 5");
}
@Test
public void testRemoveNamedFunction() {
test("function foo(){}", "");
}
@Test
public void testRemoveRecursiveFunction1() {
test("function f(){f()}", "");
}
@Test
public void testRemoveRecursiveFunction2() {
test("var f = function (){f()}", "");
}
@Test
public void testRemoveRecursiveFunction2a() {
test("var f = function g(){g()}", "");
}
@Test
public void testRemoveRecursiveFunction3() {
test("var f;f = function (){f()}", "");
}
@Test
public void testRemoveRecursiveFunction4() {
// don't remove redefinition of an external variable
testSame("doThing1 = function (){doThing1()}");
}
@Test
public void testRemoveRecursiveFunction5() {
test("function g(){f()}function f(){g()}", "");
}
@Test
public void testRemoveRecursiveFunction6() {
test("var f=function(){g()};function g(){f()}", "");
}
@Test
public void testRemoveRecursiveFunction7() {
test("var g = function(){f()};var f = function(){g()}", "");
}
@Test
public void testRemoveRecursiveFunction8() {
test("var o = {};o.f = function(){o.f()}", "");
}
@Test
public void testRemoveRecursiveFunction9() {
testSame("var o = {};o.f = function(){o.f()};o.f()");
}
@Test
public void testSideEffectClassification1() {
testSame("doThing1();");
}
@Test
public void testSideEffectClassification2() {
test("var a = doThing1();", "doThing1();");
}
@Test
public void testSideEffectClassification3() {
testSame("var a = doThing1();window['b']=a;");
}
@Test
public void testSideEffectClassification4() {
testSame("function sef(){} sef();");
}
@Test
public void testSideEffectClassification5() {
testSame("function nsef(){} var a = nsef();window['b']=a;");
}
@Test
public void testSideEffectClassification6() {
testSame("function sef(){} sef();");
}
@Test
public void testSideEffectClassification7() {
testSame("function sef(){} var a = sef();window['b']=a;");
}
@Test
public void testNoSideEffectAnnotation1() {
test(externs("function f(){}"), srcs("var a = f();"), expected("f()"));
test(externs("function f(){}"), srcs("let a = f();"), expected("f()"));
test(externs("function f(){}"), srcs("const a = f();"), expected("f()"));
}
@Test
public void testNoSideEffectAnnotation2() {
test(externs("/** @nosideeffects */ function f(){}"), srcs("var a = f();"), expected(""));
}
@Test
public void testNoSideEffectAnnotation3() {
test(externs("var f = function(){};"), srcs("var a = f();"), expected("f()"));
}
@Test
public void testNoSideEffectAnnotation4() {
test(
externs("var f = /** @nosideeffects */ function(){};"), srcs("var a = f();"), expected(""));
test(
externs("/** @nosideeffects */ var f = function(){};"), srcs("var a = f();"), expected(""));
}
@Test
public void testNoSideEffectAnnotation5() {
test(
"var f; f = function(){alert('a')}; var a = f();",
"var f; f = function(){alert('a')}; f();");
}
@Test
public void testNoSideEffectAnnotation6() {
test(
externs("var f = /** @nosideeffects */ function(){};"), //
srcs("var a = f();"),
expected(""));
}
@Test
public void testNoSideEffectAnnotation7() {
test(
externs("var f = /** @nosideeffects */ function(){};"),
srcs("f = function(){};var a = f();"),
expected("f = function(){};"));
test(
externs("function sideEffect() {}; var f = /** @nosideeffects */ function(){};"),
srcs("f = function(){sideEffect()};var a = f();"),
expected("f = function(){sideEffect()};f()"));
}
@Test
public void testNoSideEffectAnnotation8() {
test(
externs("var f = function(){}; f = /** @nosideeffects */ function(){};"),
srcs("var a = f();"),
expected("f();"));
}
@Test
public void testNoSideEffectAnnotation_whenUsedOnDuplicateDefinitions_eliminatesSideEffects() {
test(
externs(
"var f = /** @nosideeffects */ function(){};",
"var f = /** @nosideeffects */ function(){};"),
srcs("var a = f();"),
expected(""));
}
@Test
public void testNoSideEffectAnnotation10() {
test(
externs("var o = {}; o.f = function(){};"), //
srcs("var a = o.f();"),
expected("o.f();"));
}
@Test
public void testNoSideEffectAnnotation11() {
test(
externs("var o = {}; o.f = /** @nosideeffects */ function(){};"),
srcs("var a = o.f();"),
expected(""));
}
@Test
public void testNoSideEffectAnnotation12() {
test(externs("function c(){}"), srcs("var a = new c()"), expected("new c()"));
}
@Test
public void testNoSideEffectAnnotation13() {
test(externs("/** @nosideeffects */ function c(){}"), srcs("var a = new c"), expected(""));
}
@Test
public void testNoSideEffectAnnotation14() {
String externs = "function c(){};" + "c.prototype.f = /**@nosideeffects*/function(){};";
test(
externs(externs),
srcs("var o = new c; var a = o.f()"),
expected("new c"));
}
@Test
public void testNoSideEffectAnnotation15() {
test(
externs("/** @nosideeffects */ function c(){}", "c.prototype.f = function(){};"),
srcs("var a = (new c).f()"),
expected("(new c).f()"));
}
@Test
public void testNoSideEffectAnnotation16() {
test(
externs(
"/** @nosideeffects */ function c(){}",
"c.prototype.f = /** @nosideeffects */ function(){};"),
srcs("var a = (new c).f()"),
expected(""));
}
@Test
public void testFunctionPrototype() {
test("var a = 5; Function.prototype.foo = function() {return a;}", "");
}
@Test
public void testTopLevelClass1() {
test("var Point = function() {}; Point.prototype.foo = function() {}", "");
}
@Test
public void testTopLevelClass2() {
test(
"var Point = {}; Point.prototype.foo = function() {};externfoo = new Point()",
"var Point = {}; externfoo = new Point()");
}
@Test
public void testTopLevelClass3() {
test("function Point() {this.me_ = Point}", "");
}
@Test
public void testTopLevelClass4() {
test(
"function f(){} function A(){} A.prototype = {x: function() {}}; f();",
"function f(){} f();");
}
@Test
public void testTopLevelClass5() {
test(
"function f(){} function A(){} A.prototype = {x: function() { f(); }}; new A();",
" function A(){} A.prototype = { }; new A();");
}
@Test
public void testTopLevelClass6() {
testSame(
"function f(){} function A(){}" + "A.prototype = {x: function() { f(); }}; new A().x();");
}
@Test
public void testTopLevelClass7() {
test("A.prototype.foo = function(){}; function A() {}", "");
}
@Test
public void testNamespacedClass1() {
test("var foo = {};foo.bar = {};foo.bar.prototype.baz = {}", "");
}
@Test
public void testNamespacedClass2() {
test(
"var foo = {}; foo.bar = {}; foo.bar.prototype.baz = {}; window.z = new foo.bar()",
"var foo = {}; foo.bar = {}; window.z = new foo.bar()");
}
// TODO(b/66971163): enable or remove this
public void disabledTestNamespacedClass3() {
test("var a = {}; a.b = function() {}; a.b.prototype = {x: function() {}};", "");
}
@Test
public void testNamespacedClass4() {
test(
lines(
"function f(){}",
"var a = {};",
"a.b = function() {};",
"a.b.prototype = {x: function() { f(); }};",
"new a.b();"),
lines(
" ",
"var a = {};",
"a.b = function() {};",
"a.b.prototype = { };",
"new a.b();"));
}
@Test
public void testNamespacedClass5() {
testSame(
"function f(){} var a = {}; a.b = function() {};"
+ "a.b.prototype = {x: function() { f(); }}; new a.b().x();");
}
@Test
public void testEs6Class() {
test("class C {}", "");
test("class C {constructor() {} }", "");
testSame("class C {} var c = new C(); use(c);");
testSame(
lines(
"class C {",
" constructor() {",
" this.x = 1;",
" }",
" add() {",
" this.x++",
" }",
"}",
"var c = new C;",
"c.add();",
"use(c.x);"));
test("class C{} class D{} var d = new D;", "class D{} new D;");
test("{class C{} }", "{}");
testSame("{class C{} var c = new C; use(c)}");
test(
"function f() { return class C {} } doThing1();", // preserve newline
" doThing1();");
testSame("export class C {}");
// class expressions
test("var c = class{}", "");
testSame("var c = class{}; use(c);");
test("var c = class C {}", "");
test(
"var c = class C {}; use(c);", // preserve newline
"var c = class {}; use(c);");
}
@Test
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 */
@Test
public void testEs6ClassExtendsQualifiedName1() {
testSame("var ns = {}; ns.Class1 = class {}; class Class2 extends ns.Class1 {}; use(Class2);");
}
/** @bug 67430253 */
@Test
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);");
}
@Test
public void testAssignmentToThisPrototype() {
testSame(
lines(
"Function.prototype.inherits = function(parentCtor) {",
" function tempCtor() {};",
" tempCtor.prototype = parentCtor.prototype;",
" this.superClass_ = parentCtor.prototype;",
" this.prototype = new tempCtor();",
" this.prototype.constructor = this;",
"};",
"/** @constructor */ function A() {}",
"/** @constructor */ function B() {}",
"B.inherits(A);",
"use(B.superClass_);"));
}
@Test
public void testAssignmentToCallResultPrototype() {
testSame("function f() { return function(){}; } f().prototype = {};");
}
@Test
public void testAssignmentToExternPrototype() {
testSame("externfoo.prototype = {};");
}
@Test
public void testAssignmentToUnknownPrototype() {
testSame("/** @suppress {duplicate} */ var window;" + "window['a'].prototype = {};");
}
@Test
public void testBug2099540() {
testSame(
lines(
"/** @suppress {duplicate} */ var document;",
"/** @suppress {duplicate} */ var window;",
"var klass;",
"window[klass].prototype = document.createElement('p')['__proto__'];"));
}
@Test
public void testOtherGlobal() {
testSame("goog.global.foo = bar(); function bar(){}");
}
@Test
public void testExternName1() {
testSame("top.z = bar(); function bar(){}");
}
@Test
public void testExternName2() {
testSame("top['z'] = bar(); function bar(){}");
}
@Test
public void testInherits1() {
test("var a = {}; var b = {}; goog.inherits(b, a)", "");
}
@Test
public void testInherits2() {
testSame("var a = {}; externfoo.inherits(a);");
}
@Test
public void testInherits3() {
testSame("var a = {}; goog.inherits(externfoo, a);");
}
@Test
public void testInherits4() {
test("var b = {}; goog.inherits(b, externfoo);", "");
}
@Test
public void testInherits5() {
testSame("var a = {}; goog.inherits(externfoo, a);");
}
@Test
public void testMixin1() {
testSame("Function.prototype.mixin = function(base) { goog.mixin(this.prototype, base); };");
}
@Test
public void testMixin2() {
testSame("var a = {}; goog.mixin(externfoo.prototype, a.prototype);");
}
@Test
public void testMixin3() {
test("var b = {}; goog.mixin(b.prototype, externfoo.prototype);", "");
}
@Test
public void testMixin4() {
testSame("var b = {}; goog.mixin(b.prototype, externfoo.prototype); new b()");
}
@Test
public void testMixin5() {
test(
lines(
"var b = {}; var c = {};",
"goog.mixin(b.prototype, externfoo.prototype);",
"goog.mixin(c.prototype, externfoo.prototype);",
"new b()"),
lines("var b = {};", "goog.mixin(b.prototype, externfoo.prototype);", "new b()"));
}
@Test
public void testMixin6() {
test(
lines(
"var b = {}; var c = {};",
"goog.mixin(c.prototype, externfoo.prototype), ",
"goog.mixin(b.prototype, externfoo.prototype);",
"new b()"),
lines(
"var b = {}; ",
" ",
"goog.mixin(b.prototype, externfoo.prototype);",
"new b()"));
}
@Test
public void testMixin7() {
test(
lines(
"var b = {}; var c = {};",
"var d = (goog.mixin(c.prototype, externfoo.prototype), ",
" goog.mixin(b.prototype, externfoo.prototype));",
"new b()"),
lines(
"var b = {};", // preserve newline
"goog.mixin(b.prototype, externfoo.prototype);",
"new b()"));
}
@Test
public void testConstants1() {
testSame("var bar = function(){}; var EXP_FOO = true; if (EXP_FOO) bar();");
}
@Test
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();");
}
@Test
public void testExpressions1() {
test("var foo={}; foo.A='A'; foo.AB=foo.A+'B'; foo.ABC=foo.AB+'C'", "");
}
@Test
public void testExpressions2() {
test("var foo={}; foo.A='A'; foo.AB=foo.A+'B'; this.ABC=foo.AB+'C'", "");
}
@Test
public void testExpressions3() {
testSame("var foo = 2; window.bar(foo + 3)");
}
@Test
public void testSetCreatingReference() {
test(
"var foo; var bar = function(){foo=6;}; bar();",
" var bar = function(){ }; bar();");
}
@Test
public void testAnonymous1() {
testSame("function foo() {}; function bar() {}; foo(function() {bar()})");
}
@Test
public void testAnonymous2() {
test("var foo;(function(){foo=6;})()", "(function(){})()");
}
@Test
public void testAnonymous3() {
testSame("var foo; (function(){ if(!foo)foo=6; })()");
}
@Test
public void testAnonymous4() {
testSame("var foo; (function(){ foo=6; })(); externfoo=foo;");
}
@Test
public void testAnonymous5() {
testSame(
"var foo;" + "(function(){ foo=function(){ bar() }; function bar(){} })();" + "foo();");
}
@Test
public void testAnonymous6() {
testSame("function foo(){}" + "function bar(){}" + "foo(function(){externfoo = bar});");
}
@Test
public void testAnonymous7() {
testSame("var foo;" + "(function (){ function bar(){ externfoo = foo; } bar(); })();");
}
@Test
public void testAnonymous8() {
testSame("var foo;" + "(function (){ var g=function(){ externfoo = foo; }; g(); })();");
}
@Test
public void testAnonymous9() {
testSame(
"function foo(){}"
+ "function bar(){}"
+ "foo(function(){ function baz(){ externfoo = bar; } baz(); });");
}
@Test
public void testFunctions1() {
test(
"var foo = null; function baz() {sideEffect()} function bar() {foo=baz();} bar();",
" function baz() {sideEffect()} function bar() { baz();} bar();");
}
@Test
public void testFunctions2() {
test(
"var foo; foo = function() {var a = bar()}; var bar = function(){sideEffect()}; foo();",
"var foo; foo = function() { bar()}; var bar = function(){sideEffect()}; foo();");
}
@Test
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()");
}
@Test
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()");
}
@Test
public void testGetElem3() {
testSame("var foo = {'i': 0, 'j': 1}; foo['k'] = 2; top.foo = foo;");
}
@Test
public void testIf1() {
test("var foo = {};if(externfoo)foo.bar=function(){};", "if(externfoo);");
}
@Test
public void testIf2() {
test("var e = false;var foo = {};if(e)foo.bar=function(){};", "var e = false;if(e);");
}
@Test
public void testIf3() {
test("var e = false;var foo = {};if(e + 1)foo.bar=function(){};", "var e = false;if(e + 1);");
}
@Test
public void testIf4() {
test("var e = false, f;var foo = {};if(f=e)foo.bar=function(){};", "var e = false;if(e);");
}
@Test
public void testIf4a() {
// TODO(johnlenz): fix this.
testSame("var e = [], f;if(f=e);f[0] = 1;");
}
@Test
public void testIf4b() {
// TODO(johnlenz): fix this.