-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RenameVarsTest.java
988 lines (852 loc) · 31.4 KB
/
RenameVarsTest.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
/*
* Copyright 2005 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 static com.google.common.base.Preconditions.checkArgument;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.javascript.rhino.Node;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
/**
* Tests for {@link RenameVars}.
*/
public final class RenameVarsTest extends CompilerTestCase {
private static final String DEFAULT_PREFIX = "";
private String prefix = DEFAULT_PREFIX;
private VariableMap previouslyUsedMap =
new VariableMap(ImmutableMap.<String, String>of());
private RenameVars renameVars;
private boolean withClosurePass = false;
private boolean localRenamingOnly = false;
private boolean preserveFunctionExpressionNames = false;
private boolean useGoogleCodingConvention = true;
private boolean generatePseudoNames = false;
private boolean shouldShadow = false;
private boolean preferStableNames = false;
private boolean withNormalize = false;
// NameGenerator to use, or null for a default.
private DefaultNameGenerator nameGenerator = null;
@Override
protected CodingConvention getCodingConvention() {
if (useGoogleCodingConvention) {
return new GoogleCodingConvention();
} else {
return CodingConventions.getDefault();
}
}
@Override
protected CompilerPass getProcessor(Compiler compiler) {
CompilerPass pass;
if (withClosurePass) {
pass = new ClosurePassAndRenameVars(compiler);
} else if (nameGenerator != null) {
pass = renameVars = new RenameVars(compiler, prefix,
localRenamingOnly, preserveFunctionExpressionNames,
generatePseudoNames, shouldShadow, preferStableNames,
previouslyUsedMap, null, null, nameGenerator);
} else {
pass = renameVars = new RenameVars(compiler, prefix,
localRenamingOnly, preserveFunctionExpressionNames,
generatePseudoNames, shouldShadow, preferStableNames,
previouslyUsedMap, null, null, new DefaultNameGenerator());
}
if (withNormalize) {
// Don't use the standard CompilerTestCase normalization options
// as renaming is a post denormalization operation, but we do still
// want to run the normal normalizations on the input in some cases.
pass = new NormalizePassWrapper(compiler, pass);
}
return pass;
}
@Override
protected int getNumRepetitions() {
return 1;
}
@Override
protected void setUp() throws Exception {
super.setUp();
disableValidateAstChangeMarking();
previouslyUsedMap = new VariableMap(ImmutableMap.<String, String>of());
prefix = DEFAULT_PREFIX;
withClosurePass = false;
withNormalize = false;
localRenamingOnly = false;
preserveFunctionExpressionNames = false;
generatePseudoNames = false;
shouldShadow = false;
preferStableNames = false;
nameGenerator = null;
}
public void testRenameSimple() {
test("function Foo(v1, v2) {return v1;} Foo();",
"function a(b, c) {return b;} a();");
}
public void testRenameGlobals() {
test("var Foo; var Bar, y; function x() { Bar++; }",
"var a; var b, c; function d() { b++; }");
}
public void testRenameLocals() {
test("(function (v1, v2) {}); (function (v3, v4) {});",
"(function (a, b) {}); (function (a, b) {});");
test("function f1(v1, v2) {}; function f2(v3, v4) {};",
"function c(a, b) {}; function d(a, b) {};");
}
public void testRenameLocals_let() {
test(
"(function () { let var1 = 0; let another = 1; });",
"(function () { let a = 0; let b = 1; });");
}
public void testRenameLocals_const() {
test(
"(function () { const var1 = 0; const another = 1; });",
"(function () { const a = 0; const b = 1; });");
}
public void testRenameLocalsToSame() {
preferStableNames = true;
testSame("(function(a) {})");
testSame("(function(a, b) {})");
testSame("(function(a, b, c) {})");
testSame("(function() { var a; })");
testSame("(function() { var a, b; })");
testSame("(function() { var a, b, c; })");
}
public void testRenameRedeclaredGlobals() {
test(
LINE_JOINER.join(
"function f1(v1, v2) {f1()};",
"/** @suppress {duplicate} */",
"function f1(v3, v4) {f1()};"),
LINE_JOINER.join(
"function a(b, c) {a()};",
"/** @suppress {duplicate} */",
"function a(b, c) {a()};"));
localRenamingOnly = true;
test(
LINE_JOINER.join(
"function f1(v1, v2) {f1()};",
"/** @suppress {duplicate} */",
"function f1(v3, v4) {f1()};"),
LINE_JOINER.join(
"function f1(a, b) {f1()};",
"/** @suppress {duplicate} */",
"function f1(a, b) {f1()};"));
}
public void testRecursiveFunctions1() {
test("var walk = function walk(node, aFunction) {" +
" walk(node, aFunction);" +
"};",
"var a = function a(b, c) {" +
" a(b, c);" +
"};");
localRenamingOnly = true;
test("var walk = function walk(node, aFunction) {" +
" walk(node, aFunction);" +
"};",
"var walk = function walk(a, b) {" +
" walk(a, b);" +
"};");
}
public void testRecursiveFunctions2() {
preserveFunctionExpressionNames = true;
test("var walk = function walk(node, aFunction) {" +
" walk(node, aFunction);" +
"};",
"var c = function walk(a, b) {" +
" walk(a, b);" +
"};");
localRenamingOnly = true;
test("var walk = function walk(node, aFunction) {" +
" walk(node, aFunction);" +
"};",
"var walk = function walk(a, b) {" +
" walk(a, b);" +
"};");
}
public void testRenameLocalsClashingWithGlobals() {
test("function a(v1, v2) {return v1;} a();",
"function a(b, c) {return b;} a();");
}
public void testRenameNested() {
test("function f1(v1, v2) { (function(v3, v4) {}) }",
"function a(b, c) { (function(d, e) {}) }");
test("function f1(v1, v2) { function f2(v3, v4) {} }",
"function a(b, c) { function d(e, f) {} }");
}
public void testBleedingRecursiveFunctions1() {
// On IE, bleeding functions will interfere with each other if
// they are in the same scope. In the below example, we want to be
// sure that a and b get separate names.
test("var x = function a(x) { return x ? 1 : a(1); };" +
"var y = function b(x) { return x ? 2 : b(2); };",
"var c = function b(a) { return a ? 1 : b(1); };" +
"var e = function d(a) { return a ? 2 : d(2); };");
}
public void testBleedingRecursiveFunctions2() {
test(
LINE_JOINER.join(
"function f() {",
" var x = function a(x) { return x ? 1 : a(1); };",
" var y = function b(x) { return x ? 2 : b(2); };",
"}"),
LINE_JOINER.join(
"function d() {",
" var e = function a(b) { return b ? 1 : a(1); };",
" var f = function c(a) { return a ? 2 : c(2); };",
"}"));
}
public void testBleedingRecursiveFunctions3() {
test(
LINE_JOINER.join(
"function f() {",
" var x = function a(x) { return x ? 1 : a(1); };",
" var y = function b(x) { return x ? 2 : b(2); };",
" var z = function c(x) { return x ? y : c(2); };",
"}"),
LINE_JOINER.join(
"function f() {",
" var g = function a(c) { return c ? 1 : a(1); };",
" var d = function b(a) { return a ? 2 : b(2); };",
" var h = function e(b) { return b ? d : e(2); };",
"}"));
}
public void testBleedingFunctionInBlocks() {
test(LINE_JOINER.join(
"if (true) {",
" var x = function a(x) {return x;}",
"}"),
LINE_JOINER.join(
"if (true) {",
" var c = function b(a) {return a;}",
"}"));
}
public void testRenameWithExterns1() {
String externs = "var foo;";
test(externs, "var bar; foo(bar);", "var a; foo(a);");
}
public void testRenameWithExterns2() {
String externs = "var a;";
test(externs, "var b = 5", "var b = 5");
}
public void testDoNotRenameExportedName() {
testSame("_foo()");
}
public void testDoNotRenameArguments() {
testSame("function a() { arguments; }");
}
public void testRenameWithNameOverlap() {
testSame("var a = 1; var b = 2; b + b;");
}
public void testRenameWithPrefix1() {
prefix = "PRE_";
test("function Foo(v1, v2) {return v1} Foo();",
"function PRE_(a, b) {return a} PRE_();");
prefix = DEFAULT_PREFIX;
}
public void testRenameWithPrefix2() {
prefix = "PRE_";
test("function Foo(v1, v2) {var v3 = v1 + v2; return v3;} Foo();",
"function PRE_(a, b) {var c = a + b; return c;} PRE_();");
prefix = DEFAULT_PREFIX;
}
public void testRenameWithPrefix3() {
prefix = "a";
test("function Foo() {return 1;}" +
"function Bar() {" +
" var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z," +
" A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ab;" +
" Foo();" +
"} Bar();",
"function a() {return 1;}" +
"function aa() {" +
" var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A," +
" B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,$,ba,ca;" +
" a();" +
"} aa();");
prefix = DEFAULT_PREFIX;
}
public void testNamingBasedOnOrderOfOccurrence() {
test("var q,p,m,n,l,k; " +
"try { } catch(r) {try {} catch(s) {}}; var t = q + q;",
"var a,b,c,d,e,f; " +
"try { } catch(g) {try {} catch(h) {}}; var i = a + a;"
);
test("(function(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z," +
"a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,$){});" +
"var a4,a3,a2,a1,b4,b3,b2,b1,ab,ac,ad,fg;function foo(){};",
"(function(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z," +
"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,$){});" +
"var aa,ba,ca,da,ea,fa,ga,ha,ia,ja,ka,la;function ma(){};");
}
public void testTryCatchLifeTime () {
test("var q,p,m,n,l,k; " +
"(function (r) {}); try { } catch(s) {}; var t = q + q;",
"var a,c,d,e,f,g; " +
"(function(b) {}); try { } catch(b) {}; var h = a + a;"
);
test("try {try {} catch(p) {}} catch(s) {};",
"try {try {} catch(a) {}} catch(a) {};"
);
test(
LINE_JOINER.join(
"try {",
" try { ",
" } catch(p) {",
" try { ",
" } catch(r) {}",
" }",
"} catch(s) {",
" try { ",
" } catch(q) {}",
"};"),
LINE_JOINER.join(
"try {",
" try { ",
" } catch(a) {",
" try { ",
" } catch(b) {}",
" }",
"} catch(a) {",
" try { ",
" } catch(b) {}",
"};"));
}
public void testStableRenameSimple() {
VariableMap expectedVariableMap = makeVariableMap(
"Foo", "a", "L 0", "b", "L 1", "c");
testRenameMap("function Foo(v1, v2) {return v1;} Foo();",
"function a(b, c) {return b;} a();", expectedVariableMap);
expectedVariableMap = makeVariableMap(
"Foo", "a", "L 0", "b", "L 1", "c", "L 2", "d");
testRenameMapUsingOldMap("function Foo(v1, v2, v3) {return v1;} Foo();",
"function a(b, c, d) {return b;} a();", expectedVariableMap);
}
public void testStableRenameGlobals() {
VariableMap expectedVariableMap = makeVariableMap(
"Foo", "a", "Bar", "b", "y", "c", "x", "d");
testRenameMap("var Foo; var Bar, y; function x() { Bar++; }",
"var a; var b, c; function d() { b++; }",
expectedVariableMap);
expectedVariableMap = makeVariableMap(
"Foo", "a", "Bar", "b", "y", "c", "x", "d", "Baz", "f", "L 0" , "e");
testRenameMapUsingOldMap(
"var Foo, Baz; var Bar, y; function x(R) { return R + Bar++; }",
"var a, f; var b, c; function d(e) { return e + b++; }",
expectedVariableMap);
}
public void testStableRenameWithPointlesslyAnonymousFunctions() {
VariableMap expectedVariableMap = makeVariableMap("L 0", "a", "L 1", "b");
testRenameMap("(function (v1, v2) {}); (function (v3, v4) {});",
"(function (a, b) {}); (function (a, b) {});",
expectedVariableMap);
expectedVariableMap = makeVariableMap("L 0", "a", "L 1", "b", "L 2", "c");
testRenameMapUsingOldMap("(function (v0, v1, v2) {});" +
"(function (v3, v4) {});",
"(function (a, b, c) {});" +
"(function (a, b) {});",
expectedVariableMap);
}
public void testStableRenameLocalsClashingWithGlobals() {
test("function a(v1, v2) {return v1;} a();",
"function a(b, c) {return b;} a();");
previouslyUsedMap = renameVars.getVariableMap();
test("function bar(){return;}function a(v1, v2) {return v1;} a();",
"function d(){return;}function a(b, c) {return b;} a();");
}
public void testStableRenameNested() {
VariableMap expectedVariableMap = makeVariableMap(
"f1", "a", "L 0", "b", "L 1", "c", "L 2", "d", "L 3", "e");
testRenameMap("function f1(v1, v2) { (function(v3, v4) {}) }",
"function a(b, c) { (function(d, e) {}) }",
expectedVariableMap);
expectedVariableMap = makeVariableMap(
"f1", "a", "L 0", "b", "L 1", "c", "L 2", "d", "L 3", "e", "L 4", "f");
testRenameMapUsingOldMap(
"function f1(v1, v2) { (function(v3, v4, v5) {}) }",
"function a(b, c) { (function(d, e, f) {}) }",
expectedVariableMap);
}
public void testStableRenameWithExterns1() {
String externs = "var foo;";
test(externs, "var bar; foo(bar);", "var a; foo(a);");
previouslyUsedMap = renameVars.getVariableMap();
test(externs, "var bar, baz; foo(bar, baz);",
"var a, b; foo(a, b);");
}
public void testStableRenameWithExterns2() {
String externs = "var a;";
test(externs, "var b = 5", "var b = 5");
previouslyUsedMap = renameVars.getVariableMap();
test(externs, "var b = 5, catty = 9;", "var b = 5, c=9;");
}
public void testStableRenameWithNameOverlap() {
testSame("var a = 1; var b = 2; b + b;");
previouslyUsedMap = renameVars.getVariableMap();
testSame("var a = 1; var c, b = 2; b + b;");
}
public void testStableRenameWithAnonymousFunctions() {
VariableMap expectedVariableMap = makeVariableMap("L 0", "a", "foo", "b");
testRenameMap("function foo(bar){return bar;}foo(function(h){return h;});",
"function b(a){return a}b(function(a){return a;})",
expectedVariableMap);
expectedVariableMap = makeVariableMap("foo", "b", "L 0", "a", "L 1", "c");
testRenameMapUsingOldMap(
"function foo(bar) {return bar;}foo(function(g,h) {return g+h;});",
"function b(a){return a}b(function(a,c){return a+c;})",
expectedVariableMap);
}
public void testStableRenameSimpleExternsChanges() {
VariableMap expectedVariableMap = makeVariableMap(
"Foo", "a", "L 0", "b", "L 1", "c");
testRenameMap("function Foo(v1, v2) {return v1;} Foo();",
"function a(b, c) {return b;} a();", expectedVariableMap);
expectedVariableMap = makeVariableMap("L 0", "b", "L 1", "c", "L 2", "a");
String externs = "var Foo;";
testRenameMapUsingOldMap(externs,
"function Foo(v1, v2, v0) {return v1;} Foo();",
"function Foo(b, c, a) {return b;} Foo();",
expectedVariableMap);
}
public void testStableRenameSimpleLocalNameExterned() {
test("function Foo(v1, v2) {return v1;} Foo();",
"function a(b, c) {return b;} a();");
previouslyUsedMap = renameVars.getVariableMap();
String externs = "var b;";
test(externs, "function Foo(v1, v2) {return v1;} Foo(b);",
"function a(d, c) {return d;} a(b);");
}
public void testStableRenameSimpleGlobalNameExterned() {
test("function Foo(v1, v2) {return v1;} Foo();",
"function a(b, c) {return b;} a();");
previouslyUsedMap = renameVars.getVariableMap();
String externs = "var Foo;";
test(externs, "function Foo(v1, v2, v0) {return v1;} Foo();",
"function Foo(b, c, a) {return b;} Foo();");
}
public void testStableRenameWithPrefix1AndUnstableLocalNames() {
prefix = "PRE_";
test("function Foo(v1, v2) {return v1} Foo();",
"function PRE_(a, b) {return a} PRE_();");
previouslyUsedMap = renameVars.getVariableMap();
prefix = "PRE_";
test("function Foo(v0, v1, v2) {return v1} Foo();",
"function PRE_(a, b, c) {return b} PRE_();");
}
public void testStableRenameWithPrefix2() {
prefix = "a";
test("function Foo() {return 1;}" +
"function Bar() {" +
" var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z," +
" A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ab;" +
" Foo();" +
"} Bar();",
"function a() {return 1;}" +
"function aa() {" +
" var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A," +
" B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,$,ba,ca;" +
" a();" +
"} aa();");
previouslyUsedMap = renameVars.getVariableMap();
prefix = "a";
test("function Foo() {return 1;}" +
"function Baz() {return 1;}" +
"function Bar() {" +
" var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z," +
" A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ab;" +
" Foo();" +
"} Bar();",
"function a() {return 1;}" +
"function ab() {return 1;}" +
"function aa() {" +
" var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A," +
" B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,$,ba,ca;" +
" a();" +
"} aa();");
}
public void testContrivedExampleWhereConsistentRenamingIsWorse() {
previouslyUsedMap = makeVariableMap(
"Foo", "LongString", "L 0", "b", "L 1", "c");
test("function Foo(v1, v2) {return v1;} Foo();",
"function LongString(b, c) {return b;} LongString();");
previouslyUsedMap = renameVars.getVariableMap();
VariableMap expectedVariableMap = makeVariableMap(
"Foo", "LongString", "L 0", "b", "L 1", "c");
assertVariableMapsEqual(expectedVariableMap, previouslyUsedMap);
}
public void testPrevUsedMapWithDuplicates() {
try {
makeVariableMap("Foo", "z", "Bar", "z");
testSame("");
fail();
} catch (java.lang.IllegalArgumentException expected) {
}
}
public void testExportSimpleSymbolReservesName() {
test("var goog, x; goog.exportSymbol('a', x);",
"var a, b; a.exportSymbol('a', b);");
withClosurePass = true;
test("var goog, x; goog.exportSymbol('a', x);",
"var b, c; b.exportSymbol('a', c);");
}
public void testExportComplexSymbolReservesName() {
test("var goog, x; goog.exportSymbol('a.b', x);",
"var a, b; a.exportSymbol('a.b', b);");
withClosurePass = true;
test("var goog, x; goog.exportSymbol('a.b', x);",
"var b, c; b.exportSymbol('a.b', c);");
}
public void testExportToNonStringDoesntExplode() {
withClosurePass = true;
test("var goog, a, b; goog.exportSymbol(a, b);",
"var a, b, c; a.exportSymbol(b, c);");
}
public void testDollarSignSuperExport1() {
useGoogleCodingConvention = false;
// See http://blickly.github.io/closure-compiler-issues/#32
test("var x = function($super,duper,$fantastic){}",
"var c = function($super, a, b){}");
localRenamingOnly = false;
test("var $super = 1", "var a = 1");
useGoogleCodingConvention = true;
test("var x = function($super,duper,$fantastic){}",
"var c = function($super,a,b){}");
}
public void testDollarSignSuperExport2() {
withNormalize = true;
useGoogleCodingConvention = false;
// See http://blickly.github.io/closure-compiler-issues/#32
test("var x = function($super,duper,$fantastic){};" +
"var y = function($super,duper){};",
"var c = function($super, a, b){};" +
"var d = function($super, a){};");
localRenamingOnly = false;
test("var $super = 1", "var a = 1");
useGoogleCodingConvention = true;
test("var x = function($super,duper,$fantastic){};" +
"var y = function($super,duper){};",
"var c = function($super, a, b ){};" +
"var d = function($super,a){};");
}
public void testBias() {
nameGenerator = new DefaultNameGenerator(new HashSet<String>(), "", null);
nameGenerator.favors("AAAAAAAAHH");
test("var x, y", "var A, H");
}
public void testPseudoNames() {
generatePseudoNames = false;
// See http://blickly.github.io/closure-compiler-issues/#32
test("var foo = function(a, b, c){}",
"var d = function(a, b, c){}");
generatePseudoNames = true;
test("var foo = function(a, b, c){}",
"var $foo$$ = function($a$$, $b$$, $c$$){}");
test("var a = function(a, b, c){}",
"var $a$$ = function($a$$, $b$$, $c$$){}");
}
public void testArrowFunctions(){
test("foo => {return foo + 3;}",
"a => {return a + 3;}");
test("(foo, bar) => {return foo + bar + 3;}",
"(a, b) => {return a + b + 3;}");
}
public void testClasses() {
test("class fooBar {}",
"class a {}");
test(
LINE_JOINER.join(
"class fooBar {",
" constructor(foo, bar) {",
" this.foo = foo;",
" this.bar = bar;",
" }",
"}",
"var x = new fooBar(2, 3);"),
LINE_JOINER.join(
"class a {",
" constructor(b, c) {",
" this.foo = b;",
" this.bar = c;",
" }",
"}",
"var d = new a(2, 3);"));
test(
LINE_JOINER.join(
"class fooBar {",
" constructor(foo, bar) {",
" this.foo = foo;",
" this.bar = bar;",
" }",
" func(x) {",
" return this.foo + x;",
" }",
"}",
"var x = new fooBar(2,3);",
"var abcd = x.func(5);"),
LINE_JOINER.join(
"class b {",
" constructor(a, c) {",
" this.foo = a;",
" this.bar = c;",
" }",
" func(a) {",
" return this.foo + a;",
" }",
"}",
"var d = new b(2,3);",
"var e = d.func(5);"
));
}
public void testLetConst() {
test("let xyz;",
"let a;"
);
test("const xyz = 1;",
"const a = 1");
test(
LINE_JOINER.join(
"let zyx = 1; {",
" const xyz = 1;",
" let zyx = 2;",
" zyx = 3;",
"}",
"let xyz = 'potato';",
"zyx = 4;"
),
LINE_JOINER.join(
"let a = 1; {",
" const c = 1;",
" let b = 2;",
" b = 3;",
"}",
"let d = 'potato';",
"a = 4;"));
}
public void testGenerators() {
test(
LINE_JOINER.join(
"function* gen() {",
" var xyz = 3;",
" yield xyz + 4;",
"}",
"gen().next()"
),
LINE_JOINER.join(
"function* a() {",
" var b = 3;",
" yield b + 4;",
"}",
"a().next()"));
}
public void testForOf() {
test(
"for (var item of items) {}",
"for (var a of items) {}");
}
public void testTemplateStrings() {
test(
LINE_JOINER.join(
"var name = 'Foo';",
"`My name is ${name}`;"
),
LINE_JOINER.join(
"var a = 'Foo';",
"`My name is ${a}`;"));
}
public void testArrayDestructuring() {
test("var [x, y, z] = [1, 2, 3];",
"var [a, b, c] = [1, 2, 3];");
}
public void testObjectDestructuring() {
test(
LINE_JOINER.join(
"var obj = {p: 5, h: false};",
"var {p, h} = obj;"),
LINE_JOINER.join(
"var a = {p: 5, h: false};",
"var {p, h} = a;"));
test(
LINE_JOINER.join(
"var obj = {p: 5, h: false};",
"var {p: x, h: y} = obj;"),
LINE_JOINER.join(
"var a = {p: 5, h: false};",
"var {p: b, h: c} = a;"));
}
public void testDefaultFunction() {
test(
LINE_JOINER.join(
"function f(x, y=12) {",
" return x * y;",
"}"
),
LINE_JOINER.join(
"function c(a, b=12) {",
" return a * b;",
"}"));
}
public void testRestFunction() {
test(
LINE_JOINER.join(
"function f(x, ...y) {",
" return x * y[0];",
"}"
),
LINE_JOINER.join(
"function c(a, ...b) {",
" return a * b[0];",
"}"));
}
public void testObjectLiterals() {
test(
LINE_JOINER.join(
"var objSuper = {",
" f: 'potato'",
"};",
"var obj = {",
" __proto__: objSuper,",
" g: false,",
" x() {",
" return super.f;",
" }",
"};",
"obj.x();"
),
LINE_JOINER.join(
"var a = {",
" f: 'potato'",
"};",
"var b = {",
" __proto__: a,",
" g: false,",
" x() {",
" return super.f;",
" }",
"};",
"b.x();"));
}
public void testImport1() {
test("import name from './other.js'; use(name);", "import a from './other.js'; use(a);");
test(
"import * as name from './other.js'; use(name);",
"import * as a from './other.js'; use(a);");
test(
"import {default as name} from './other.js'; use(name);",
"import {default as a} from './other.js'; use(a);");
}
public void testImport2() {
withNormalize = true;
test(
"import {name} from './other.js'; use(name);",
"import {name as a} from './other.js'; use(a);");
}
private void testRenameMapUsingOldMap(String input, String expected,
VariableMap expectedMap) {
previouslyUsedMap = renameVars.getVariableMap();
testRenameMap("", input, expected, expectedMap);
}
private void testRenameMapUsingOldMap(String externs, String input,
String expected,
VariableMap expectedMap) {
previouslyUsedMap = renameVars.getVariableMap();
testRenameMap(externs, input, expected, expectedMap);
}
private void testRenameMap(String input, String expected,
VariableMap expectedRenameMap) {
testRenameMap("", input, expected, expectedRenameMap);
}
private void testRenameMap(String externs, String input, String expected,
VariableMap expectedRenameMap) {
test(externs, input, expected);
VariableMap renameMap = renameVars.getVariableMap();
assertVariableMapsEqual(expectedRenameMap, renameMap);
}
public void testPreferStableNames() {
preferStableNames = true;
// Locals in scopes with too many local variables (>1000) should
// not receive temporary names (eg, 'L 123'). These locals will
// appear in the name maps with the same name as in the code (eg,
// 'a0' in this case).
test(createManyVarFunction(1000), null);
assertEquals(null, renameVars.getVariableMap().lookupNewName("a0"));
assertEquals("b", renameVars.getVariableMap().lookupNewName("L 0"));
test(createManyVarFunction(1001), null);
assertEquals("b", renameVars.getVariableMap().lookupNewName("a0"));
assertEquals(null, renameVars.getVariableMap().lookupNewName("L 0"));
// With {@code preferStableNames} off locals should
// unconditionally receive temporary names.
preferStableNames = false;
test(createManyVarFunction(1000), null);
assertEquals(null, renameVars.getVariableMap().lookupNewName("a0"));
assertEquals("b", renameVars.getVariableMap().lookupNewName("L 0"));
test(createManyVarFunction(1001), null);
assertEquals(null, renameVars.getVariableMap().lookupNewName("a0"));
assertEquals("b", renameVars.getVariableMap().lookupNewName("L 0"));
}
private static String createManyVarFunction(int numVars) {
List<String> locals = new ArrayList<>();
for (int i = 0; i < numVars; i++) {
locals.add("a" + i);
}
return "function foo() { var " + Joiner.on(",").join(locals) + "; }";
}
private VariableMap makeVariableMap(String... keyValPairs) {
checkArgument(keyValPairs.length % 2 == 0);
ImmutableMap.Builder<String, String> renameMap = ImmutableMap.builder();
for (int i = 0; i < keyValPairs.length; i += 2) {
renameMap.put(keyValPairs[i], keyValPairs[i + 1]);
}
return new VariableMap(renameMap.build());
}
private static void assertVariableMapsEqual(VariableMap a, VariableMap b) {
Map<String, String> ma = a.getOriginalNameToNewNameMap();
Map<String, String> mb = b.getOriginalNameToNewNameMap();
assertEquals("VariableMaps not equal", ma, mb);
}
private class ClosurePassAndRenameVars implements CompilerPass {
private final Compiler compiler;
private ClosurePassAndRenameVars(Compiler compiler) {
this.compiler = compiler;
}
@Override
public void process(Node externs, Node root) {
ProcessClosurePrimitives closurePass =
new ProcessClosurePrimitives(
compiler, null, CheckLevel.WARNING, false);
closurePass.process(externs, root);
renameVars = new RenameVars(compiler, prefix,
false, false, false, false, false, previouslyUsedMap, null,
closurePass.getExportedVariableNames(),
new DefaultNameGenerator());
renameVars.process(externs, root);
}
}
private static class NormalizePassWrapper implements CompilerPass {
private final Compiler compiler;
private final CompilerPass wrappedPass;
private NormalizePassWrapper(Compiler compiler,
CompilerPass wrappedPass) {
this.compiler = compiler;
this.wrappedPass = wrappedPass;
}
@Override
public void process(Node externs, Node root) {
Normalize normalize = new Normalize(compiler, false);
normalize.process(externs, root);
wrappedPass.process(externs, root);
}
}
}