-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CheckAccessControlsTest.java
2000 lines (1794 loc) · 70.5 KB
/
CheckAccessControlsTest.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 2008 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.javascript.jscomp.CheckAccessControls.BAD_PACKAGE_PROPERTY_ACCESS;
import static com.google.javascript.jscomp.CheckAccessControls.BAD_PRIVATE_GLOBAL_ACCESS;
import static com.google.javascript.jscomp.CheckAccessControls.BAD_PRIVATE_PROPERTY_ACCESS;
import static com.google.javascript.jscomp.CheckAccessControls.BAD_PROPERTY_OVERRIDE_IN_FILE_WITH_FILEOVERVIEW_VISIBILITY;
import static com.google.javascript.jscomp.CheckAccessControls.BAD_PROTECTED_PROPERTY_ACCESS;
import static com.google.javascript.jscomp.CheckAccessControls.CONST_PROPERTY_DELETED;
import static com.google.javascript.jscomp.CheckAccessControls.CONST_PROPERTY_REASSIGNED_VALUE;
import static com.google.javascript.jscomp.CheckAccessControls.CONVENTION_MISMATCH;
import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_CLASS;
import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_CLASS_REASON;
import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_NAME;
import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_NAME_REASON;
import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_PROP;
import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_PROP_REASON;
import static com.google.javascript.jscomp.CheckAccessControls.EXTEND_FINAL_CLASS;
import static com.google.javascript.jscomp.CheckAccessControls.PRIVATE_OVERRIDE;
import static com.google.javascript.jscomp.CheckAccessControls.VISIBILITY_MISMATCH;
import com.google.common.collect.ImmutableList;
/**
* Tests for {@link CheckAccessControls}.
*
* @author nicksantos@google.com (Nick Santos)
*/
public final class CheckAccessControlsTest extends TypeICompilerTestCase {
private static final DiagnosticGroup NTI_CONST =
new DiagnosticGroup(
GlobalTypeInfo.CONST_WITHOUT_INITIALIZER,
GlobalTypeInfo.COULD_NOT_INFER_CONST_TYPE,
GlobalTypeInfo.MISPLACED_CONST_ANNOTATION,
NewTypeInference.CONST_REASSIGNED,
NewTypeInference.CONST_PROPERTY_REASSIGNED,
NewTypeInference.CONST_PROPERTY_DELETED);
public CheckAccessControlsTest() {
super(CompilerTypeTestCase.DEFAULT_EXTERNS);
parseTypeInfo = true;
enableClosurePass();
enableRewriteClosureCode();
}
@Override
protected int getNumRepetitions() {
return 1;
}
@Override
protected CompilerPass getProcessor(final Compiler compiler) {
return new CheckAccessControls(compiler, true);
}
@Override
protected CompilerOptions getOptions() {
CompilerOptions options = super.getOptions();
options.setWarningLevel(DiagnosticGroups.ACCESS_CONTROLS, CheckLevel.ERROR);
options.setWarningLevel(DiagnosticGroups.CONSTANT_PROPERTY, CheckLevel.ERROR);
// Disable NTI's native const checks so as to suppress duplicate warnings that
// prevent us from testing the const checks of CheckAccessControls itself.
options.setWarningLevel(NTI_CONST, CheckLevel.OFF);
return options;
}
private void testDepName(String js, String errorMessage) {
test(js, null, DEPRECATED_NAME_REASON, null, errorMessage);
}
private void testDepProp(String js, String errorMessage) {
test(js, null, DEPRECATED_PROP_REASON, null, errorMessage);
}
private void testDepClass(String js, String errorMessage) {
test(js, null, DEPRECATED_CLASS_REASON, null, errorMessage);
}
public void testDeprecatedFunctionNoReason() {
testError("/** @deprecated */ function f() {} function g() { f(); }", DEPRECATED_NAME);
}
public void testDeprecatedFunction() {
testDepName(
"/** @deprecated Some Reason */ function f() {} function g() { f(); }",
"Variable f has been deprecated: Some Reason");
}
public void testWarningOnDeprecatedConstVariable() {
testDepName(
"/** @deprecated Another reason */ var f = 4; function g() { alert(f); }",
"Variable f has been deprecated: Another reason");
}
public void testThatNumbersArentDeprecated() {
testSame("/** @deprecated */ var f = 4; var h = 3; function g() { alert(h); }");
}
public void testDeprecatedFunctionVariable() {
testDepName(
"/** @deprecated I like g... */ var f = function() {}; function g() { f(); }",
"Variable f has been deprecated: I like g...");
}
public void testNoWarningInGlobalScope() {
testSame("var goog = {}; goog.makeSingleton = function(x) {};"
+ "/** @deprecated */ function f() {} goog.makeSingleton(f);");
}
public void testNoWarningInGlobalScopeForCall() {
testDepName(
"/** @deprecated Some global scope */ function f() {} f();",
"Variable f has been deprecated: Some global scope");
}
public void testNoWarningInDeprecatedFunction() {
testSame("/** @deprecated */ function f() {} /** @deprecated */ function g() { f(); }");
}
public void testWarningInNormalClass() {
testDepName(
"/** @deprecated FooBar */ function f() {}"
+ "/** @constructor */ var Foo = function() {}; "
+ "Foo.prototype.bar = function() { f(); }",
"Variable f has been deprecated: FooBar");
}
public void testWarningForProperty1() {
testDepProp(
"/** @constructor */ function Foo() {}"
+ "/** @deprecated A property is bad */ Foo.prototype.bar = 3;"
+ "Foo.prototype.baz = function() { alert((new Foo()).bar); };",
"Property bar of type Foo has been deprecated: A property is bad");
}
public void testWarningForProperty2() {
testDepProp(
"/** @constructor */ function Foo() {}"
+ "/** @deprecated Zee prop, it is deprecated! */ Foo.prototype.bar = 3;"
+ "Foo.prototype.baz = function() { alert(this.bar); };",
"Property bar of type Foo has been deprecated: Zee prop, it is deprecated!");
}
public void testWarningForDeprecatedClass() {
testDepClass(
"/** @constructor \n* @deprecated Use the class 'Bar' */ function Foo() {} "
+ "function f() { new Foo(); }",
"Class Foo has been deprecated: Use the class 'Bar'");
}
public void testWarningForDeprecatedClassNoReason() {
testError(
"/** @constructor \n* @deprecated */ function Foo() {} " + "function f() { new Foo(); }",
DEPRECATED_CLASS);
}
public void testNoWarningForDeprecatedClassInstance() {
testSame("/** @constructor \n * @deprecated */ function Foo() {} "
+ "/** @param {Foo} x */ function f(x) { return x; }");
}
public void testWarningForDeprecatedSuperClass() {
testDepClass(
"/** @constructor \n * @deprecated Superclass to the rescue! */ function Foo() {} "
+ "/** @constructor \n * @extends {Foo} */ function SubFoo() {}"
+ "function f() { new SubFoo(); }",
"Class SubFoo has been deprecated: Superclass to the rescue!");
}
public void testWarningForDeprecatedSuperClass2() {
testDepClass(
"/** @constructor \n * @deprecated Its only weakness is Kryptoclass */ function Foo() {} "
+ "/** @const */ var namespace = {}; "
+ "/** @constructor \n * @extends {Foo} */ "
+ "namespace.SubFoo = function() {}; "
+ "function f() { new namespace.SubFoo(); }",
"Class namespace.SubFoo has been deprecated: Its only weakness is Kryptoclass");
}
public void testWarningForPrototypeProperty() {
String js =
"/** @constructor */ function Foo() {}"
+ "/** @deprecated It is now in production, use that model... */ Foo.prototype.bar = 3;"
+ "Foo.prototype.baz = function() { alert(Foo.prototype.bar); };";
testDepProp(
js,
"Property bar of type Foo.prototype has been deprecated:"
+ " It is now in production, use that model...");
}
public void testNoWarningForNumbers() {
testSame("/** @constructor */ function Foo() {}"
+ "/** @deprecated */ Foo.prototype.bar = 3;"
+ "Foo.prototype.baz = function() { alert(3); };");
}
public void testWarningForMethod1() {
testDepProp(
"/** @constructor */ function Foo() {}"
+ "/** @deprecated There is a madness to this method */"
+ "Foo.prototype.bar = function() {};"
+ "Foo.prototype.baz = function() { this.bar(); };",
"Property bar of type Foo has been deprecated: There is a madness to this method");
}
public void testWarningForMethod2() {
testDepProp(
"/** @constructor */ function Foo() {}"
+ "/** @deprecated Stop the ringing! */ Foo.prototype.bar;"
+ "Foo.prototype.baz = function() { this.bar(); };",
"Property bar of type Foo has been deprecated: Stop the ringing!");
}
public void testNoWarningInDeprecatedClass() {
testSame("/** @deprecated */ function f() {} "
+ "/** @constructor \n * @deprecated */ "
+ "var Foo = function() {}; "
+ "Foo.prototype.bar = function() { f(); }");
}
public void testNoWarningOnDeclaration() {
testSame("/** @constructor */ function F() {\n"
+ " /**\n"
+ " * @type {number}\n"
+ " * @deprecated Use something else.\n"
+ " */\n"
+ " this.code;\n"
+ "}");
}
public void testNoWarningInDeprecatedClass2() {
testSame("/** @deprecated */ function f() {} "
+ "/** @constructor \n * @deprecated */ "
+ "var Foo = function() {}; "
+ "Foo.bar = function() { f(); }");
}
public void testNoWarningInDeprecatedStaticMethod() {
testSame("/** @deprecated */ function f() {} "
+ "/** @constructor */ "
+ "var Foo = function() {}; "
+ "/** @deprecated */ Foo.bar = function() { f(); }");
}
public void testWarningInStaticMethod() {
testDepName(
"/** @deprecated crazy! */ function f() {} "
+ "/** @constructor */ "
+ "var Foo = function() {}; "
+ "Foo.bar = function() { f(); }",
"Variable f has been deprecated: crazy!");
}
public void testDeprecatedObjLitKey() {
testDepProp(
"/** @const */ var f = {};"
+ "/** @deprecated It is literally not used anymore */ f.foo = 3;"
+ "function g() { return f.foo; }",
"Property foo of type f has been deprecated: It is literally not used anymore");
}
public void testWarningForSubclassMethod() {
testDepProp(
"/** @constructor */ function Foo() {}"
+ "Foo.prototype.bar = function() {};"
+ "/** @constructor \n * @extends {Foo} */ function SubFoo() {}"
+ "/** @deprecated I have a parent class! */ SubFoo.prototype.bar = function() {};"
+ "function f() { (new SubFoo()).bar(); };",
"Property bar of type SubFoo has been deprecated: I have a parent class!");
}
public void testWarningForSuperClassWithDeprecatedSubclassMethod() {
testSame("/** @constructor */ function Foo() {}"
+ "Foo.prototype.bar = function() {};"
+ "/** @constructor \n * @extends {Foo} */ function SubFoo() {}"
+ "/** @deprecated \n * @override */ SubFoo.prototype.bar = "
+ "function() {};"
+ "function f() { (new Foo()).bar(); };");
}
public void testWarningForSuperclassMethod() {
testDepProp(
"/** @constructor */ function Foo() {}"
+ "/** @deprecated I have a child class! */ Foo.prototype.bar = function() {};"
+ "/** @constructor \n * @extends {Foo} */ function SubFoo() {}"
+ "SubFoo.prototype.bar = function() {};"
+ "function f() { (new SubFoo()).bar(); };",
"Property bar of type SubFoo has been deprecated: I have a child class!");
}
public void testWarningForSuperclassMethod2() {
testDepProp(
"/** @constructor */ function Foo() {}"
+ "/** @deprecated I have another child class... \n* @protected */"
+ "Foo.prototype.bar = function() {};"
+ "/** @constructor \n * @extends {Foo} */ function SubFoo() {}"
+ "/** @protected */SubFoo.prototype.bar = function() {};"
+ "function f() { (new SubFoo()).bar(); };",
"Property bar of type SubFoo has been deprecated: I have another child class...");
}
public void testWarningForBind() {
// NTI reports NTI_REDCLARED_PROPERTY here, which is as intended. If this were a new
// property and not the existing `bind`, then we'd report the deprecation warning as expected
// (see testAutoboxedDeprecatedProperty and testAutoboxedPrivateProperty).
this.mode = TypeInferenceMode.OTI_ONLY;
testDepProp(
"/** @deprecated I'm bound to this method... */ Function.prototype.bind = function() {};"
+ "(function() {}).bind();",
"Property bind of type function has been deprecated: I'm bound to this method...");
}
public void testWarningForDeprecatedClassInGlobalScope() {
testDepClass(
"/** @constructor \n * @deprecated I'm a very worldly object! */ var Foo = function() {};"
+ "new Foo();",
"Class Foo has been deprecated: I'm a very worldly object!");
}
public void testNoWarningForPrototypeCopying() {
testSame("/** @constructor */ var Foo = function() {};"
+ "Foo.prototype.bar = function() {};"
+ "/** @deprecated */ Foo.prototype.baz = Foo.prototype.bar;"
+ "(new Foo()).bar();");
}
public void testNoWarningOnDeprecatedPrototype() {
// This used to cause an NPE.
testSame("/** @constructor */ var Foo = function() {};"
+ "/** @deprecated */ Foo.prototype = {};"
+ "Foo.prototype.bar = function() {};");
}
public void testPrivateAccessForNames() {
testSame("/** @private */ function foo_() {}; foo_();");
testError(new String[] {"/** @private */ function foo_() {};", "foo_();"},
BAD_PRIVATE_GLOBAL_ACCESS);
}
public void testPrivateAccessForNames2() {
// Private by convention
testSame("function foo_() {}; foo_();");
testError(new String[] {"function foo_() {};", "foo_();"}, BAD_PRIVATE_GLOBAL_ACCESS);
}
public void testPrivateAccessForProperties1() {
testSame("/** @constructor */ function Foo() {}"
+ "/** @private */ Foo.prototype.bar_ = function() {};"
+ "Foo.prototype.baz = function() { this.bar_(); }; (new Foo).bar_();");
}
public void testPrivateAccessForProperties2() {
testSame(new String[] {
"/** @constructor */ function Foo() {}",
"/** @private */ Foo.prototype.bar_ = function() {};"
+ "Foo.prototype.baz = function() { this.bar_(); }; (new Foo).bar_();"});
}
public void testPrivateAccessForProperties3() {
// Even though baz is "part of the Foo class" the access is disallowed since it's
// not in the same file.
testError(new String[] {
"/** @constructor */ function Foo() {}"
+ "/** @private */ Foo.prototype.bar_ = function() {}; (new Foo).bar_();",
"Foo.prototype.baz = function() { this.bar_(); };"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testPrivateAccessForProperties4() {
testSame(
"/** @constructor */ function Foo() {}"
+ "/** @private */ Foo.prototype.bar_ = function() {};"
+ "Foo.prototype['baz'] = function() { (new Foo()).bar_(); };");
}
public void testPrivateAccessForProperties5() {
test(
new String[] {
LINE_JOINER.join(
"/** @constructor */",
"function Parent () {",
" /** @private */",
" this.prop = 'foo';",
"};"),
LINE_JOINER.join(
"/**",
" * @constructor",
" * @extends {Parent}",
" */",
"function Child() {",
" this.prop = 'asdf';",
"}",
"Child.prototype = new Parent();")
},
null,
BAD_PRIVATE_PROPERTY_ACCESS,
null,
"Access to private property prop of Parent not allowed here.");
}
public void testPrivateAccessForProperties6() {
test(
new String[] {
LINE_JOINER.join(
"goog.provide('x.y.z.Parent');",
"",
"/** @constructor */",
"x.y.z.Parent = function() {",
" /** @private */",
" this.prop = 'foo';",
"};"),
LINE_JOINER.join(
"goog.require('x.y.z.Parent');",
"",
"/**",
" * @constructor",
" * @extends {x.y.z.Parent}",
" */",
"function Child() {",
" this.prop = 'asdf';",
"}",
"Child.prototype = new x.y.z.Parent();")
},
null,
BAD_PRIVATE_PROPERTY_ACCESS,
null,
"Access to private property prop of x.y.z.Parent not allowed here.");
}
public void testPrivateAccess_googModule() {
String[] js = new String[] {
LINE_JOINER.join(
"goog.module('example.One');",
"/** @constructor */ function One() {}",
"/** @private */ One.prototype.m = function() {};",
"exports = One;"),
LINE_JOINER.join(
"goog.module('example.two');",
"var One = goog.require('example.One');",
"(new One()).m();"),
};
this.mode = TypeInferenceMode.OTI_ONLY;
test(
js,
null,
BAD_PRIVATE_PROPERTY_ACCESS,
null,
"Access to private property m of One not allowed here.");
this.mode = TypeInferenceMode.NTI_ONLY;
test(
js,
null,
BAD_PRIVATE_PROPERTY_ACCESS,
null,
"Access to private property m of module$exports$example$One not allowed here.");
}
public void testNoPrivateAccessForProperties1() {
testError(new String[] {
"/** @constructor */ function Foo() {} (new Foo).bar_();",
"/** @private */ Foo.prototype.bar_ = function() {};"
+ "Foo.prototype.baz = function() { this.bar_(); };"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties2() {
testError(new String[] {
"/** @constructor */ function Foo() {} "
+ "/** @private */ Foo.prototype.bar_ = function() {};"
+ "Foo.prototype.baz = function() { this.bar_(); };",
"(new Foo).bar_();"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties3() {
testError(new String[] {
"/** @constructor */ function Foo() {} "
+ "/** @private */ Foo.prototype.bar_ = function() {};",
"/** @constructor */ function OtherFoo() { (new Foo).bar_(); }"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties4() {
testError(new String[] {
"/** @constructor */ function Foo() {} "
+ "/** @private */ Foo.prototype.bar_ = function() {};",
"/** @constructor \n * @extends {Foo} */ "
+ "function SubFoo() { this.bar_(); }"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties5() {
testError(new String[] {
"/** @constructor */ function Foo() {} "
+ "/** @private */ Foo.prototype.bar_ = function() {};",
"/** @constructor \n * @extends {Foo} */ "
+ "function SubFoo() {};"
+ "SubFoo.prototype.baz = function() { this.bar_(); }"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties6() {
// Overriding a private property with a non-private property
// in a different file causes problems.
testError(new String[] {
"/** @constructor */ function Foo() {} "
+ "/** @private */ Foo.prototype.bar_ = function() {};",
"/** @constructor \n * @extends {Foo} */ "
+ "function SubFoo() {};"
+ "SubFoo.prototype.bar_ = function() {};"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties6a() {
// Same as above, except with namespaced constructors
testError(new String[] {
"/** @const */ var ns = {};"
+ "/** @constructor */ ns.Foo = function() {}; "
+ "/** @private */ ns.Foo.prototype.bar_ = function() {};",
"/** @constructor \n * @extends {ns.Foo} */ "
+ "ns.SubFoo = function() {};"
+ "ns.SubFoo.prototype.bar_ = function() {};"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties7() {
// It's OK to override a private property with a non-private property
// in the same file, but you'll get yelled at when you try to use it.
testError(new String[] {
"/** @constructor */ function Foo() {} "
+ "/** @private */ Foo.prototype.bar_ = function() {};"
+ "/** @constructor \n * @extends {Foo} */ "
+ "function SubFoo() {};"
+ "SubFoo.prototype.bar_ = function() {};",
"SubFoo.prototype.baz = function() { this.bar_(); }"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties8() {
testError(new String[] {
"/** @constructor */ function Foo() { /** @private */ this.bar_ = 3; }",
"/** @constructor \n * @extends {Foo} */ "
+ "function SubFoo() { /** @private */ this.bar_ = 3; };"},
PRIVATE_OVERRIDE);
}
public void testNoPrivateAccessForProperties9() {
testError(new String[] {
"/** @constructor */ function Foo() {}"
+ "Foo.prototype = {"
+ "/** @private */ bar_: 3"
+ "}",
"new Foo().bar_;"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties10() {
testError(new String[] {
"/** @constructor */ function Foo() {}"
+ "Foo.prototype = {"
+ "/** @private */ bar_: function() {}"
+ "}",
"new Foo().bar_();"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties11() {
testError(new String[] {
"/** @constructor */ function Foo() {}"
+ "Foo.prototype = {"
+ "/** @private */ get bar_() { return 1; }"
+ "}",
"var a = new Foo().bar_;"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForProperties12() {
testError(new String[] {
"/** @constructor */ function Foo() {}"
+ "Foo.prototype = {"
+ "/** @private */ set bar_(x) { this.barValue = x; }"
+ "}",
"new Foo().bar_ = 1;"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testNoPrivateAccessForNamespaces() {
testError(new String[] {
"/** @const */ var foo = {};\n"
+ "/** @private */ foo.bar_ = function() {};",
"foo.bar_();"},
BAD_PRIVATE_PROPERTY_ACCESS);
}
public void testProtectedAccessForProperties1() {
testSame(new String[] {
"/** @constructor */ function Foo() {}"
+ "/** @protected */ Foo.prototype.bar = function() {};"
+ "(new Foo).bar();",
"Foo.prototype.baz = function() { this.bar(); };"});
}
public void testProtectedAccessForProperties2() {
testSame(new String[] {
"/** @constructor */ function Foo() {}"
+ "/** @protected */ Foo.prototype.bar = function() {};"
+ "(new Foo).bar();",
"/** @constructor \n * @extends {Foo} */"
+ "function SubFoo() { this.bar(); }"});
}
public void testProtectedAccessForProperties3() {
testSame(new String[] {
"/** @constructor */ function Foo() {}"
+ "/** @protected */ Foo.prototype.bar = function() {};"
+ "(new Foo).bar();",
"/** @constructor \n * @extends {Foo} */"
+ "function SubFoo() { }"
+ "SubFoo.baz = function() { (new Foo).bar(); }"});
}
public void testProtectedAccessForProperties4() {
testSame(new String[] {
"/** @constructor */ function Foo() {}"
+ "/** @protected */ Foo.bar = function() {};",
"/** @constructor \n * @extends {Foo} */"
+ "function SubFoo() { Foo.bar(); }"});
}
public void testProtectedAccessForProperties5() {
testSame(new String[] {
"/** @constructor */ function Foo() {}"
+ "/** @protected */ Foo.prototype.bar = function() {};"
+ "(new Foo).bar();",
"/** @constructor \n * @extends {Foo} */"
+ "var SubFoo = function() { this.bar(); }"});
}
public void testProtectedAccessForProperties6() {
testSame(new String[] {
"/** @const */ var goog = {};"
+ "/** @constructor */ goog.Foo = function() {};"
+ "/** @protected */ goog.Foo.prototype.bar = function() {};",
"/** @constructor \n * @extends {goog.Foo} */"
+ "goog.SubFoo = function() { this.bar(); };"});
}
public void testProtectedAccessForProperties7() {
testSame(new String[] {
"/** @constructor */ var Foo = function() {};"
+ "Foo.prototype = { /** @protected */ bar: function() {} }",
"/** @constructor \n * @extends {Foo} */"
+ "var SubFoo = function() { this.bar(); };"
+ "SubFoo.prototype = { moo: function() { this.bar(); }};"});
}
public void testProtectedAccessForProperties8() {
testSame(new String[] {
"/** @constructor */ var Foo = function() {};"
+ "Foo.prototype = { /** @protected */ bar: function() {} }",
"/** @constructor \n * @extends {Foo} */"
+ "var SubFoo = function() {};"
+ "SubFoo.prototype = { get moo() { this.bar(); }};"});
}
public void testProtectedAccessForProperties9() {
testSame(new String[] {
"/** @constructor */ var Foo = function() {};"
+ "Foo.prototype = { /** @protected */ bar: function() {} }",
"/** @constructor \n * @extends {Foo} */"
+ "var SubFoo = function() {};"
+ "SubFoo.prototype = { set moo(val) { this.x = this.bar(); }};"});
}
public void testProtectedAccessForProperties10() {
// NTI throws NTI_CTOR_IN_DIFFERENT_SCOPE
testSame(ImmutableList.of(
SourceFile.fromCode(
"foo.js",
"/** @constructor */ var Foo = function() {};"
+ "/** @protected */ Foo.prototype.bar = function() {};"),
SourceFile.fromCode(
"sub_foo.js",
"/** @constructor @extends {Foo} */"
+ "var SubFoo = function() {};"
+ "(/** @suppress {newCheckTypes} */ function() {"
+ "SubFoo.prototype.baz = function() { this.bar(); }"
+ "})();")));
}
public void testProtectedAccessForProperties11() {
test(ImmutableList.of(
SourceFile.fromCode(
"foo.js",
LINE_JOINER.join(
"goog.provide('Foo');",
"/** @interface */ Foo = function() {};",
"/** @protected */ Foo.prop = {};")),
SourceFile.fromCode(
"bar.js",
LINE_JOINER.join(
"goog.require('Foo');",
"/** @constructor @implements {Foo} */",
"function Bar() { Foo.prop; };"))),
null, null);
}
public void testProtectedAccessForProperties12() {
test(ImmutableList.of(
SourceFile.fromCode(
"a.js",
LINE_JOINER.join(
"goog.provide('A');",
"/** @constructor */",
"var A = function() {",
" /**",
" * @type {?String}",
" * @protected",
" */",
" this.prop;",
"}")),
SourceFile.fromCode(
"b.js",
LINE_JOINER.join(
"goog.require('A');",
"/**",
" * @constructor",
" * @extends {A}",
" */",
"var B = function() {",
" this.prop.length;",
" this.prop.length;",
"};"))),
null, null);
}
// FYI: Java warns for the b1.method access in c.js.
// Instead of following that in NTI, we chose to follow the behavior of
// the old JSCompiler type checker, to make migration easier.
public void testProtectedAccessForProperties13() {
test(ImmutableList.of(
SourceFile.fromCode(
"a.js",
LINE_JOINER.join(
"goog.provide('A');",
"/** @constructor */",
"var A = function() {}",
"/** @protected */",
"A.prototype.method = function() {};")),
SourceFile.fromCode(
"b1.js",
LINE_JOINER.join(
"goog.require('A');",
"goog.provide('B1');",
"/** @constructor @extends {A} */",
"var B1 = function() {};",
"/** @override */",
"B1.prototype.method = function() {};")),
SourceFile.fromCode(
"b2.js",
LINE_JOINER.join(
"goog.require('A');",
"goog.provide('B2');",
"/** @constructor @extends {A} */",
"var B2 = function() {};",
"/** @override */",
"B2.prototype.method = function() {};")),
SourceFile.fromCode(
"c.js",
LINE_JOINER.join(
"goog.require('B1');",
"goog.require('B2');",
"/**",
" * @param {!B1} b1",
" * @constructor",
" * @extends {B2}",
" */",
"var C = function(b1) {",
" var x = b1.method();",
"};"))),
null, null);
}
public void testNoProtectedAccessForProperties1() {
testError(new String[] {
"/** @constructor */ function Foo() {} "
+ "/** @protected */ Foo.prototype.bar = function() {};",
"(new Foo).bar();"},
BAD_PROTECTED_PROPERTY_ACCESS);
}
public void testNoProtectedAccessForProperties2() {
testError(new String[] {
"/** @constructor */ function Foo() {} "
+ "/** @protected */ Foo.prototype.bar = function() {};",
"/** @constructor */ function OtherFoo() { (new Foo).bar(); }"},
BAD_PROTECTED_PROPERTY_ACCESS);
}
public void testNoProtectedAccessForProperties3() {
testError(new String[] {
"/** @constructor */ function Foo() {} "
+ "/** @constructor \n * @extends {Foo} */ "
+ "function SubFoo() {}"
+ "/** @protected */ SubFoo.prototype.bar = function() {};",
"/** @constructor \n * @extends {Foo} */ "
+ "function SubberFoo() { (new SubFoo).bar(); }"},
BAD_PROTECTED_PROPERTY_ACCESS);
}
public void testNoProtectedAccessForProperties4() {
testError(new String[] {
"/** @constructor */ function Foo() { (new SubFoo).bar(); } ",
"/** @constructor \n * @extends {Foo} */ "
+ "function SubFoo() {}"
+ "/** @protected */ SubFoo.prototype.bar = function() {};",
},
BAD_PROTECTED_PROPERTY_ACCESS);
}
public void testNoProtectedAccessForProperties5() {
testError(new String[] {
"/** @const */ var goog = {};"
+ "/** @constructor */ goog.Foo = function() {};"
+ "/** @protected */ goog.Foo.prototype.bar = function() {};",
"/** @constructor */"
+ "goog.NotASubFoo = function() { (new goog.Foo).bar(); };"},
BAD_PROTECTED_PROPERTY_ACCESS);
}
public void testNoProtectedAccessForProperties6() {
testError(new String[] {
"/** @constructor */ function Foo() {}"
+ "Foo.prototype = {"
+ "/** @protected */ bar: 3"
+ "}",
"new Foo().bar;"},
BAD_PROTECTED_PROPERTY_ACCESS);
}
public void testNoProtectedAccessForProperties7() {
testError(new String[] {
"/** @constructor */ function Foo() {}"
+ "Foo.prototype = {"
+ "/** @protected */ bar: function() {}"
+ "}",
"new Foo().bar();"},
BAD_PROTECTED_PROPERTY_ACCESS);
}
public void testPackagePrivateAccessForNames() {
test(
ImmutableList.of(
SourceFile.fromCode(
Compiler.joinPathParts("foo", "bar.js"),
"/** @constructor */\n"
+ "function Parent() {\n"
+ "/** @package */\n"
+ "this.prop = 'foo';\n"
+ "}\n;"),
SourceFile.fromCode(
Compiler.joinPathParts("baz", "quux.js"),
"/**"
+ " * @constructor\n"
+ " * @extends {Parent}\n"
+ " */\n"
+ "function Child() {\n"
+ " this.prop = 'asdf';\n"
+ "}\n"
+ "Child.prototype = new Parent();")),
null, BAD_PACKAGE_PROPERTY_ACCESS);
}
public void testPackagePrivateAccessForProperties1() {
testSame("/** @constructor */ function Foo() {}"
+ "/** @package */ Foo.prototype.bar = function() {};"
+ "Foo.prototype.baz = function() { this.bar(); }; (new Foo).bar();");
}
public void testPackagePrivateAccessForProperties2() {
testSame(ImmutableList.of(
SourceFile.fromCode(Compiler.joinPathParts("foo", "bar.js"),
"/** @constructor */ function Foo() {}"),
SourceFile.fromCode(
Compiler.joinPathParts("baz", "quux.js"),
"/** @package */ Foo.prototype.bar = function() {};"
+ "Foo.prototype.baz = function() { this.bar(); }; (new Foo).bar();")));
}
public void testPackagePrivateAccessForProperties3() {
testSame(ImmutableList.of(
SourceFile.fromCode(
Compiler.joinPathParts("foo", "bar.js"),
"/** @constructor */ function Foo() {}"
+ "/** @package */ Foo.prototype.bar = function() {}; (new Foo).bar();"),
SourceFile.fromCode(Compiler.joinPathParts("foo", "baz.js"),
"Foo.prototype.baz = function() { this.bar(); };")));
}
public void testPackagePrivateAccessForProperties4() {
testSame(ImmutableList.of(
SourceFile.fromCode(
Compiler.joinPathParts("foo", "bar.js"),
"/** @constructor */ function Foo() {}"
+ "/** @package */ Foo.prototype.bar = function() {};"),
SourceFile.fromCode(
Compiler.joinPathParts("foo", "baz.js"),
"Foo.prototype['baz'] = function() { (new Foo()).bar(); };")));
}
public void testPackagePrivateAccessForProperties5() {
test(
ImmutableList.of(
SourceFile.fromCode(
Compiler.joinPathParts("foo", "bar.js"),
"/** @constructor */\n"
+ "function Parent () {\n"
+ " /** @package */\n"
+ " this.prop = 'foo';\n"
+ "};"),
SourceFile.fromCode(
Compiler.joinPathParts("baz", "quux.js"),
"/**\n"
+ " * @constructor\n"
+ " * @extends {Parent}\n"
+ " */\n"
+ "function Child() {\n"
+ " this.prop = 'asdf';\n"
+ "}\n"
+ "Child.prototype = new Parent();")),
null, BAD_PACKAGE_PROPERTY_ACCESS);
}
public void testNoPackagePrivateAccessForProperties1() {
test(
ImmutableList.of(
SourceFile.fromCode(
Compiler.joinPathParts("foo", "bar.js"),
"/** @constructor */ function Foo() {} (new Foo).bar();"),
SourceFile.fromCode(
Compiler.joinPathParts("baz", "quux.js"),
"/** @package */ Foo.prototype.bar = function() {};"
+ "Foo.prototype.baz = function() { this.bar(); };")),
null, BAD_PACKAGE_PROPERTY_ACCESS);
}
public void testNoPackagePrivateAccessForProperties2() {
test(
ImmutableList.of(
SourceFile.fromCode(
Compiler.joinPathParts("foo", "bar.js"),
"/** @constructor */ function Foo() {} "
+ "/** @package */ Foo.prototype.bar = function() {};"
+ "Foo.prototype.baz = function() { this.bar(); };"),
SourceFile.fromCode(Compiler.joinPathParts("baz", "quux.js"), "(new Foo).bar();")),
null, BAD_PACKAGE_PROPERTY_ACCESS);
}
public void testNoPackagePrivateAccessForProperties3() {
test(
ImmutableList.of(
SourceFile.fromCode(
Compiler.joinPathParts("foo", "bar.js"),
"/** @constructor */ function Foo() {} "
+ "/** @package */ Foo.prototype.bar = function() {};"),
SourceFile.fromCode(
Compiler.joinPathParts("baz", "quux.js"),
"/** @constructor */ function OtherFoo() { (new Foo).bar(); }")),
null, BAD_PACKAGE_PROPERTY_ACCESS);
}
public void testNoPackagePrivateAccessForProperties4() {
test(
ImmutableList.of(
SourceFile.fromCode(
Compiler.joinPathParts("foo", "bar.js"),
"/** @constructor */ function Foo() {} "
+ "/** @package */ Foo.prototype.bar = function() {};"),
SourceFile.fromCode(
Compiler.joinPathParts("baz", "quux.js"),
"/** @constructor \n * @extends {Foo} */ "