Debian Codegraph @

test/com/google/javascript/jscomp/CheckAccessControlsTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import static com.google.javascript.jscomp.CheckAccessControls.BAD_PRIVATE_GLOBAL_ACCESS;
20 import static com.google.javascript.jscomp.CheckAccessControls.BAD_PRIVATE_PROPERTY_ACCESS;
21 import static com.google.javascript.jscomp.CheckAccessControls.BAD_PROTECTED_PROPERTY_ACCESS;
22 import static com.google.javascript.jscomp.CheckAccessControls.CONST_PROPERTY_DELETED;
23 import static com.google.javascript.jscomp.CheckAccessControls.CONST_PROPERTY_REASSIGNED_VALUE;
24 import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_CLASS;
25 import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_CLASS_REASON;
26 import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_NAME;
27 import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_NAME_REASON;
28 import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_PROP;
29 import static com.google.javascript.jscomp.CheckAccessControls.DEPRECATED_PROP_REASON;
30 import static com.google.javascript.jscomp.CheckAccessControls.EXTEND_FINAL_CLASS;
31 import static com.google.javascript.jscomp.CheckAccessControls.PRIVATE_OVERRIDE;
32 import static com.google.javascript.jscomp.CheckAccessControls.VISIBILITY_MISMATCH;
33
34 /**
35 * Tests for {@link CheckAccessControls}.
36 *
37 * @author [email protected] (Nick Santos)
38 */
39 public class CheckAccessControlsTest extends CompilerTestCase {
40
41 public CheckAccessControlsTest() {
42 super(CompilerTypeTestCase.DEFAULT_EXTERNS);
43 parseTypeInfo = true;
44 enableTypeCheck(CheckLevel.WARNING);
45 }
46
47 @Override
48 protected CompilerPass getProcessor(final Compiler compiler) {
49 return new CheckAccessControls(compiler);
50 }
51
52 @Override
53 protected CompilerOptions getOptions() {
54 CompilerOptions options = super.getOptions();
55 options.setWarningLevel(DiagnosticGroups.ACCESS_CONTROLS, CheckLevel.ERROR);
56 options.setWarningLevel(DiagnosticGroups.CONSTANT_PROPERTY,
57 CheckLevel.ERROR);
58 return options;
59 }
60
61 /**
62 * Tests that the given JavaScript code has a @deprecated marker
63 * somewhere in it which raises an error. Also tests that the
64 * deprecated marker works with a message. The JavaScript should
65 * have a JsDoc of the form "@deprecated %s\n".
66 *
67 * @param js The JavaScript code to parse and test.
68 * @param reason A simple deprecation reason string, used for testing
69 * the addition of a deprecation reason to the @deprecated tag.
70 * @param error The deprecation error expected when no reason is given.
71 * @param errorWithMessage The deprecation error expected when a reason
72 * message is given.
73 */
74 private void testDep(String js, String reason,
75 DiagnosticType error,
76 DiagnosticType errorWithMessage) {
77
78 // Test without a reason.
79 test(String.format(js, ""), null, error);
80
81 // Test with a reason.
82 test(String.format(js, reason), null, errorWithMessage, null, reason);
83 }
84
85 public void testDeprecatedFunction() {
86 testDep("/** @deprecated %s */ function f() {} function g() { f(); }",
87 "Some Reason",
88 DEPRECATED_NAME, DEPRECATED_NAME_REASON);
89 }
90
91 public void testWarningOnDeprecatedConstVariable() {
92 testDep("/** @deprecated %s */ var f = 4; function g() { alert(f); }",
93 "Another reason",
94 DEPRECATED_NAME, DEPRECATED_NAME_REASON);
95 }
96
97 public void testThatNumbersArentDeprecated() {
98 testSame("/** @deprecated */ var f = 4; var h = 3; " +
99 "function g() { alert(h); }");
100 }
101
102 public void testDeprecatedFunctionVariable() {
103 testDep("/** @deprecated %s */ var f = function() {}; " +
104 "function g() { f(); }", "I like g...",
105 DEPRECATED_NAME, DEPRECATED_NAME_REASON);
106 }
107
108 public void testNoWarningInGlobalScope() {
109 testSame("var goog = {}; goog.makeSingleton = function(x) {};" +
110 "/** @deprecated */ function f() {} goog.makeSingleton(f);");
111 }
112
113 public void testNoWarningInGlobalScopeForCall() {
114 testDep("/** @deprecated %s */ function f() {} f();",
115 "Some global scope", DEPRECATED_NAME, DEPRECATED_NAME_REASON);
116 }
117
118 public void testNoWarningInDeprecatedFunction() {
119 testSame("/** @deprecated */ function f() {} " +
120 "/** @deprecated */ function g() { f(); }");
121 }
122
123 public void testWarningInNormalClass() {
124 testDep("/** @deprecated %s */ function f() {}" +
125 "/** @constructor */ var Foo = function() {}; " +
126 "Foo.prototype.bar = function() { f(); }",
127 "FooBar", DEPRECATED_NAME, DEPRECATED_NAME_REASON);
128 }
129
130 public void testWarningForProperty1() {
131 testDep("/** @constructor */ function Foo() {}" +
132 "/** @deprecated %s */ Foo.prototype.bar = 3;" +
133 "Foo.prototype.baz = function() { alert((new Foo()).bar); };",
134 "A property is bad",
135 DEPRECATED_PROP, DEPRECATED_PROP_REASON);
136 }
137
138 public void testWarningForProperty2() {
139 testDep("/** @constructor */ function Foo() {}" +
140 "/** @deprecated %s */ Foo.prototype.bar = 3;" +
141 "Foo.prototype.baz = function() { alert(this.bar); };",
142 "Zee prop, it is deprecated!",
143 DEPRECATED_PROP,
144 DEPRECATED_PROP_REASON);
145 }
146
147 public void testWarningForDeprecatedClass() {
148 testDep("/** @constructor \n* @deprecated %s */ function Foo() {} " +
149 "function f() { new Foo(); }",
150 "Use the class 'Bar'",
151 DEPRECATED_CLASS,
152 DEPRECATED_CLASS_REASON);
153 }
154
155 public void testNoWarningForDeprecatedClassInstance() {
156 testSame("/** @constructor \n * @deprecated */ function Foo() {} " +
157 "/** @param {Foo} x */ function f(x) { return x; }");
158 }
159
160 public void testWarningForDeprecatedSuperClass() {
161 testDep("/** @constructor \n * @deprecated %s */ function Foo() {} " +
162 "/** @constructor \n * @extends {Foo} */ function SubFoo() {}" +
163 "function f() { new SubFoo(); }",
164 "Superclass to the rescue!",
165 DEPRECATED_CLASS,
166 DEPRECATED_CLASS_REASON);
167 }
168
169 public void testWarningForDeprecatedSuperClass2() {
170 testDep("/** @constructor \n * @deprecated %s */ function Foo() {} " +
171 "var namespace = {}; " +
172 "/** @constructor \n * @extends {Foo} */ " +
173 "namespace.SubFoo = function() {}; " +
174 "function f() { new namespace.SubFoo(); }",
175 "Its only weakness is Kryptoclass",
176 DEPRECATED_CLASS,
177 DEPRECATED_CLASS_REASON);
178 }
179
180 public void testWarningForPrototypeProperty() {
181 testDep("/** @constructor */ function Foo() {}" +
182 "/** @deprecated %s */ Foo.prototype.bar = 3;" +
183 "Foo.prototype.baz = function() { alert(Foo.prototype.bar); };",
184 "It is now in production, use that model...",
185 DEPRECATED_PROP,
186 DEPRECATED_PROP_REASON);
187 }
188
189 public void testNoWarningForNumbers() {
190 testSame("/** @constructor */ function Foo() {}" +
191 "/** @deprecated */ Foo.prototype.bar = 3;" +
192 "Foo.prototype.baz = function() { alert(3); };");
193 }
194
195 public void testWarningForMethod1() {
196 testDep("/** @constructor */ function Foo() {}" +
197 "/** @deprecated %s */ Foo.prototype.bar = function() {};" +
198 "Foo.prototype.baz = function() { this.bar(); };",
199 "There is a madness to this method",
200 DEPRECATED_PROP,
201 DEPRECATED_PROP_REASON);
202 }
203
204 public void testWarningForMethod2() {
205 testDep("/** @constructor */ function Foo() {} " +
206 "/** @deprecated %s */ Foo.prototype.bar; " +
207 "Foo.prototype.baz = function() { this.bar(); };",
208 "Stop the ringing!",
209 DEPRECATED_PROP,
210 DEPRECATED_PROP_REASON);
211 }
212
213 public void testNoWarningInDeprecatedClass() {
214 testSame("/** @deprecated */ function f() {} " +
215 "/** @constructor \n * @deprecated */ " +
216 "var Foo = function() {}; " +
217 "Foo.prototype.bar = function() { f(); }");
218 }
219
220 public void testNoWarningInDeprecatedClass2() {
221 testSame("/** @deprecated */ function f() {} " +
222 "/** @constructor \n * @deprecated */ " +
223 "var Foo = function() {}; " +
224 "Foo.bar = function() { f(); }");
225 }
226
227 public void testNoWarningInDeprecatedStaticMethod() {
228 testSame("/** @deprecated */ function f() {} " +
229 "/** @constructor */ " +
230 "var Foo = function() {}; " +
231 "/** @deprecated */ Foo.bar = function() { f(); }");
232 }
233
234 public void testWarningInStaticMethod() {
235 testDep("/** @deprecated %s */ function f() {} " +
236 "/** @constructor */ " +
237 "var Foo = function() {}; " +
238 "Foo.bar = function() { f(); }",
239 "crazy!",
240 DEPRECATED_NAME,
241 DEPRECATED_NAME_REASON);
242 }
243
244 public void testDeprecatedObjLitKey() {
245 testDep("var f = {}; /** @deprecated %s */ f.foo = 3; " +
246 "function g() { return f.foo; }",
247 "It is literally not used anymore",
248 DEPRECATED_PROP,
249 DEPRECATED_PROP_REASON);
250 }
251
252 public void testWarningForSubclassMethod() {
253 testDep("/** @constructor */ function Foo() {}" +
254 "Foo.prototype.bar = function() {};" +
255 "/** @constructor \n * @extends {Foo} */ function SubFoo() {}" +
256 "/** @deprecated %s */ SubFoo.prototype.bar = function() {};" +
257 "function f() { (new SubFoo()).bar(); };",
258 "I have a parent class!",
259 DEPRECATED_PROP,
260 DEPRECATED_PROP_REASON);
261 }
262
263 public void testWarningForSuperClassWithDeprecatedSubclassMethod() {
264 testSame("/** @constructor */ function Foo() {}" +
265 "Foo.prototype.bar = function() {};" +
266 "/** @constructor \n * @extends {Foo} */ function SubFoo() {}" +
267 "/** @deprecated \n * @override */ SubFoo.prototype.bar = " +
268 "function() {};" +
269 "function f() { (new Foo()).bar(); };");
270 }
271
272 public void testWarningForSuperclassMethod() {
273 testDep("/** @constructor */ function Foo() {}" +
274 "/** @deprecated %s */ Foo.prototype.bar = function() {};" +
275 "/** @constructor \n * @extends {Foo} */ function SubFoo() {}" +
276 "SubFoo.prototype.bar = function() {};" +
277 "function f() { (new SubFoo()).bar(); };",
278 "I have a child class!",
279 DEPRECATED_PROP,
280 DEPRECATED_PROP_REASON);
281 }
282
283 public void testWarningForSuperclassMethod2() {
284 testDep("/** @constructor */ function Foo() {}" +
285 "/** @deprecated %s \n* @protected */" +
286 "Foo.prototype.bar = function() {};" +
287 "/** @constructor \n * @extends {Foo} */ function SubFoo() {}" +
288 "/** @protected */SubFoo.prototype.bar = function() {};" +
289 "function f() { (new SubFoo()).bar(); };",
290 "I have another child class...",
291 DEPRECATED_PROP,
292 DEPRECATED_PROP_REASON);
293 }
294
295 public void testWarningForBind() {
296 testDep("/** @deprecated %s */ Function.prototype.bind = function() {};" +
297 "(function() {}).bind();",
298 "I'm bound to this method...",
299 DEPRECATED_PROP,
300 DEPRECATED_PROP_REASON);
301 }
302
303 public void testWarningForDeprecatedClassInGlobalScope() {
304 testDep("/** @constructor \n * @deprecated %s */ var Foo = function() {};" +
305 "new Foo();",
306 "I'm a very worldly object!",
307 DEPRECATED_CLASS,
308 DEPRECATED_CLASS_REASON);
309 }
310
311 public void testNoWarningForPrototypeCopying() {
312 testSame("/** @constructor */ var Foo = function() {};" +
313 "Foo.prototype.bar = function() {};" +
314 "/** @deprecated */ Foo.prototype.baz = Foo.prototype.bar;" +
315 "(new Foo()).bar();");
316 }
317
318 public void testNoWarningOnDeprecatedPrototype() {
319 // This used to cause an NPE.
320 testSame("/** @constructor */ var Foo = function() {};" +
321 "/** @deprecated */ Foo.prototype = {};" +
322 "Foo.prototype.bar = function() {};");
323 }
324
325 public void testPrivateAccessForNames() {
326 testSame("/** @private */ function foo_() {}; foo_();");
327 test(new String[] {
328 "/** @private */ function foo_() {};",
329 "foo_();"
330 }, null, BAD_PRIVATE_GLOBAL_ACCESS);
331 }
332
333 public void testPrivateAccessForProperties1() {
334 testSame("/** @constructor */ function Foo() {}" +
335 "/** @private */ Foo.prototype.bar_ = function() {};" +
336 "Foo.prototype.baz = function() { this.bar_(); }; (new Foo).bar_();");
337 }
338
339 public void testPrivateAccessForProperties2() {
340 testSame(new String[] {
341 "/** @constructor */ function Foo() {}",
342 "/** @private */ Foo.prototype.bar_ = function() {};" +
343 "Foo.prototype.baz = function() { this.bar_(); }; (new Foo).bar_();"
344 });
345 }
346
347 public void testPrivateAccessForProperties3() {
348 testSame(new String[] {
349 "/** @constructor */ function Foo() {}" +
350 "/** @private */ Foo.prototype.bar_ = function() {}; (new Foo).bar_();",
351 "Foo.prototype.baz = function() { this.bar_(); };"
352 });
353 }
354
355 public void testPrivateAccessForProperties4() {
356 testSame(new String[] {
357 "/** @constructor */ function Foo() {}" +
358 "/** @private */ Foo.prototype.bar_ = function() {};",
359 "Foo.prototype['baz'] = function() { (new Foo()).bar_(); };"
360 });
361 }
362
363 public void testNoPrivateAccessForProperties1() {
364 test(new String[] {
365 "/** @constructor */ function Foo() {} (new Foo).bar_();",
366 "/** @private */ Foo.prototype.bar_ = function() {};" +
367 "Foo.prototype.baz = function() { this.bar_(); };"
368 }, null, BAD_PRIVATE_PROPERTY_ACCESS);
369 }
370
371 public void testNoPrivateAccessForProperties2() {
372 test(new String[] {
373 "/** @constructor */ function Foo() {} " +
374 "/** @private */ Foo.prototype.bar_ = function() {};" +
375 "Foo.prototype.baz = function() { this.bar_(); };",
376 "(new Foo).bar_();"
377 }, null, BAD_PRIVATE_PROPERTY_ACCESS);
378 }
379
380 public void testNoPrivateAccessForProperties3() {
381 test(new String[] {
382 "/** @constructor */ function Foo() {} " +
383 "/** @private */ Foo.prototype.bar_ = function() {};",
384 "/** @constructor */ function OtherFoo() { (new Foo).bar_(); }"
385 }, null, BAD_PRIVATE_PROPERTY_ACCESS);
386 }
387
388 public void testNoPrivateAccessForProperties4() {
389 test(new String[] {
390 "/** @constructor */ function Foo() {} " +
391 "/** @private */ Foo.prototype.bar_ = function() {};",
392 "/** @constructor \n * @extends {Foo} */ " +
393 "function SubFoo() { this.bar_(); }"
394 }, null, BAD_PRIVATE_PROPERTY_ACCESS);
395 }
396
397 public void testNoPrivateAccessForProperties5() {
398 test(new String[] {
399 "/** @constructor */ function Foo() {} " +
400 "/** @private */ Foo.prototype.bar_ = function() {};",
401 "/** @constructor \n * @extends {Foo} */ " +
402 "function SubFoo() {};" +
403 "SubFoo.prototype.baz = function() { this.bar_(); }"
404 }, null, BAD_PRIVATE_PROPERTY_ACCESS);
405 }
406
407 public void testNoPrivateAccessForProperties6() {
408 // Overriding a private property with a non-private property
409 // in a different file causes problems.
410 test(new String[] {
411 "/** @constructor */ function Foo() {} " +
412 "/** @private */ Foo.prototype.bar_ = function() {};",
413 "/** @constructor \n * @extends {Foo} */ " +
414 "function SubFoo() {};" +
415 "SubFoo.prototype.bar_ = function() {};"
416 }, null, BAD_PRIVATE_PROPERTY_ACCESS);
417 }
418
419 public void testNoPrivateAccessForProperties7() {
420 // It's OK to override a private property with a non-private property
421 // in the same file, but you'll get yelled at when you try to use it.
422 test(new String[] {
423 "/** @constructor */ function Foo() {} " +
424 "/** @private */ Foo.prototype.bar_ = function() {};" +
425 "/** @constructor \n * @extends {Foo} */ " +
426 "function SubFoo() {};" +
427 "SubFoo.prototype.bar_ = function() {};",
428 "SubFoo.prototype.baz = function() { this.bar_(); }"
429 }, null, BAD_PRIVATE_PROPERTY_ACCESS);
430 }
431
432 public void testNoPrivateAccessForProperties8() {
433 test(new String[] {
434 "/** @constructor */ function Foo() { /** @private */ this.bar_ = 3; }",
435 "/** @constructor \n * @extends {Foo} */ " +
436 "function SubFoo() { /** @private */ this.bar_ = 3; };"
437 }, null, PRIVATE_OVERRIDE);
438 }
439
440 public void testProtectedAccessForProperties1() {
441 testSame(new String[] {
442 "/** @constructor */ function Foo() {}" +
443 "/** @protected */ Foo.prototype.bar = function() {};" +
444 "(new Foo).bar();",
445 "Foo.prototype.baz = function() { this.bar(); };"
446 });
447 }
448
449 public void testProtectedAccessForProperties2() {
450 testSame(new String[] {
451 "/** @constructor */ function Foo() {}" +
452 "/** @protected */ Foo.prototype.bar = function() {};" +
453 "(new Foo).bar();",
454 "/** @constructor \n * @extends {Foo} */" +
455 "function SubFoo() { this.bar(); }"
456 });
457 }
458
459 public void testProtectedAccessForProperties3() {
460 testSame(new String[] {
461 "/** @constructor */ function Foo() {}" +
462 "/** @protected */ Foo.prototype.bar = function() {};" +
463 "(new Foo).bar();",
464 "/** @constructor \n * @extends {Foo} */" +
465 "function SubFoo() { }" +
466 "SubFoo.baz = function() { (new Foo).bar(); }"
467 });
468 }
469
470 public void testProtectedAccessForProperties4() {
471 testSame(new String[] {
472 "/** @constructor */ function Foo() {}" +
473 "/** @protected */ Foo.bar = function() {};",
474 "/** @constructor \n * @extends {Foo} */" +
475 "function SubFoo() { Foo.bar(); }"
476 });
477 }
478
479 public void testProtectedAccessForProperties5() {
480 testSame(new String[] {
481 "/** @constructor */ function Foo() {}" +
482 "/** @protected */ Foo.prototype.bar = function() {};" +
483 "(new Foo).bar();",
484 "/** @constructor \n * @extends {Foo} */" +
485 "var SubFoo = function() { this.bar(); }"
486 });
487 }
488
489 public void testProtectedAccessForProperties6() {
490 testSame(new String[] {
491 "var goog = {};" +
492 "/** @constructor */ goog.Foo = function() {};" +
493 "/** @protected */ goog.Foo.prototype.bar = function() {};",
494 "/** @constructor \n * @extends {goog.Foo} */" +
495 "goog.SubFoo = function() { this.bar(); };"
496 });
497 }
498
499 public void testNoProtectedAccessForProperties1() {
500 test(new String[] {
501 "/** @constructor */ function Foo() {} " +
502 "/** @protected */ Foo.prototype.bar = function() {};",
503 "(new Foo).bar();"
504 }, null, BAD_PROTECTED_PROPERTY_ACCESS);
505 }
506
507 public void testNoProtectedAccessForProperties2() {
508 test(new String[] {
509 "/** @constructor */ function Foo() {} " +
510 "/** @protected */ Foo.prototype.bar = function() {};",
511 "/** @constructor */ function OtherFoo() { (new Foo).bar(); }"
512 }, null, BAD_PROTECTED_PROPERTY_ACCESS);
513 }
514
515 public void testNoProtectedAccessForProperties3() {
516 test(new String[] {
517 "/** @constructor */ function Foo() {} " +
518 "/** @constructor \n * @extends {Foo} */ " +
519 "function SubFoo() {}" +
520 "/** @protected */ SubFoo.prototype.bar = function() {};",
521 "/** @constructor \n * @extends {Foo} */ " +
522 "function SubberFoo() { (new SubFoo).bar(); }"
523 }, null, BAD_PROTECTED_PROPERTY_ACCESS);
524 }
525
526 public void testNoProtectedAccessForProperties4() {
527 test(new String[] {
528 "/** @constructor */ function Foo() { (new SubFoo).bar(); } ",
529 "/** @constructor \n * @extends {Foo} */ " +
530 "function SubFoo() {}" +
531 "/** @protected */ SubFoo.prototype.bar = function() {};",
532 }, null, BAD_PROTECTED_PROPERTY_ACCESS);
533 }
534
535 public void testNoProtectedAccessForProperties5() {
536 test(new String[] {
537 "var goog = {};" +
538 "/** @constructor */ goog.Foo = function() {};" +
539 "/** @protected */ goog.Foo.prototype.bar = function() {};",
540 "/** @constructor */" +
541 "goog.NotASubFoo = function() { (new goog.Foo).bar(); };"
542 }, null, BAD_PROTECTED_PROPERTY_ACCESS);
543 }
544
545 public void testNoExceptionsWithBadConstructors1() {
546 testSame(new String[] {
547 "function Foo() { (new SubFoo).bar(); } " +
548 "/** @constructor */ function SubFoo() {}" +
549 "/** @protected */ SubFoo.prototype.bar = function() {};"
550 });
551 }
552
553 public void testNoExceptionsWithBadConstructors2() {
554 testSame(new String[] {
555 "/** @constructor */ function Foo() {} " +
556 "Foo.prototype.bar = function() {};" +
557 "/** @constructor */" +
558 "function SubFoo() {}" +
559 "/** @protected */ " +
560 "SubFoo.prototype.bar = function() { (new Foo).bar(); };"
561 });
562 }
563
564 public void testGoodOverrideOfProtectedProperty() {
565 testSame(new String[] {
566 "/** @constructor */ function Foo() { } " +
567 "/** @protected */ Foo.prototype.bar = function() {};",
568 "/** @constructor \n * @extends {Foo} */ " +
569 "function SubFoo() {}" +
570 "/** @inheritDoc */ SubFoo.prototype.bar = function() {};",
571 });
572 }
573
574 public void testBadOverrideOfProtectedProperty() {
575 test(new String[] {
576 "/** @constructor */ function Foo() { } " +
577 "/** @protected */ Foo.prototype.bar = function() {};",
578 "/** @constructor \n * @extends {Foo} */ " +
579 "function SubFoo() {}" +
580 "/** @private */ SubFoo.prototype.bar = function() {};",
581 }, null, VISIBILITY_MISMATCH);
582 }
583
584 public void testBadOverrideOfPrivateProperty() {
585 test(new String[] {
586 "/** @constructor */ function Foo() { } " +
587 "/** @private */ Foo.prototype.bar = function() {};",
588 "/** @constructor \n * @extends {Foo} */ " +
589 "function SubFoo() {}" +
590 "/** @protected */ SubFoo.prototype.bar = function() {};",
591 }, null, PRIVATE_OVERRIDE);
592
593 testSame(new String[] {
594 "/** @constructor */ function Foo() { } " +
595 "/** @private */ Foo.prototype.bar = function() {};",
596 "/** @constructor \n * @extends {Foo} */ " +
597 "function SubFoo() {}" +
598 "/** @override \n *@suppress{visibility} */\n" +
599 " SubFoo.prototype.bar = function() {};",
600 });
601 }
602
603 public void testAccessOfStaticMethodOnPrivateConstructor() {
604 testSame(new String[] {
605 "/** @constructor \n * @private */ function Foo() { } " +
606 "Foo.create = function() { return new Foo(); };",
607 "Foo.create()",
608 });
609 }
610
611 public void testAccessOfStaticMethodOnPrivateQualifiedConstructor() {
612 testSame(new String[] {
613 "var goog = {};" +
614 "/** @constructor \n * @private */ goog.Foo = function() { }; " +
615 "goog.Foo.create = function() { return new goog.Foo(); };",
616 "goog.Foo.create()",
617 });
618 }
619
620 public void testInstanceofOfPrivateConstructor() {
621 testSame(new String[] {
622 "var goog = {};" +
623 "/** @constructor \n * @private */ goog.Foo = function() { }; " +
624 "goog.Foo.create = function() { return new goog.Foo(); };",
625 "goog instanceof goog.Foo",
626 });
627 }
628
629 public void testOkAssignmentOfDeprecatedProperty() {
630 testSame(
631 "/** @constructor */ function Foo() {" +
632 " /** @deprecated */ this.bar = 3;" +
633 "}");
634 }
635
636 public void testBadReadOfDeprecatedProperty() {
637 testDep(
638 "/** @constructor */ function Foo() {" +
639 " /** @deprecated %s */ this.bar = 3;" +
640 " this.baz = this.bar;" +
641 "}",
642 "GRR",
643 DEPRECATED_PROP,
644 DEPRECATED_PROP_REASON);
645 }
646
647 public void testAutoboxedDeprecatedProperty() {
648 test(
649 "", // no externs
650 "/** @constructor */ function String() {}" +
651 "/** @deprecated %s */ String.prototype.length;" +
652 "function f() { return 'x'.length; }",
653 "GRR",
654 DEPRECATED_PROP_REASON,
655 null);
656 }
657
658 public void testAutoboxedPrivateProperty() {
659 test(
660 "/** @constructor */ function String() {}" +
661 "/** @private */ String.prototype.length;", // externs
662 "function f() { return 'x'.length; }",
663 "", // output
664 BAD_PRIVATE_PROPERTY_ACCESS,
665 null);
666 }
667
668 public void testNullableDeprecatedProperty() {
669 testDep(
670 "/** @constructor */ function Foo() {}" +
671 "/** @deprecated %s */ Foo.prototype.length;" +
672 "/** @param {?Foo} x */ function f(x) { return x.length; }",
673 "GRR",
674 DEPRECATED_PROP,
675 DEPRECATED_PROP_REASON);
676 }
677
678 public void testNullablePrivateProperty() {
679 test(new String[] {
680 "/** @constructor */ function Foo() {}" +
681 "/** @private */ Foo.prototype.length;",
682 "/** @param {?Foo} x */ function f(x) { return x.length; }"
683 }, null, BAD_PRIVATE_PROPERTY_ACCESS);
684 }
685
686 public void testConstantProperty1() {
687 test("/** @constructor */ function A() {" +
688 "/** @const */ this.bar = 3;}" +
689 "/** @constructor */ function B() {" +
690 "/** @const */ this.bar = 3;this.bar += 4;}",
691 null, CONST_PROPERTY_REASSIGNED_VALUE);
692 }
693
694 public void testConstantProperty2() {
695 test("/** @constructor */ function Foo() {}" +
696 "/** @const */ Foo.prototype.prop = 2;" +
697 "var foo = new Foo();" +
698 "foo.prop = 3;",
699 null , CONST_PROPERTY_REASSIGNED_VALUE);
700 }
701
702 public void testConstantProperty3() {
703 testSame("var o = { /** @const */ x: 1 };" +
704 "o.x = 2;");
705 }
706
707 public void testConstantProperty4() {
708 test("/** @constructor */ function cat(name) {}" +
709 "/** @const */ cat.test = 1;" +
710 "cat.test *= 2;",
711 null, CONST_PROPERTY_REASSIGNED_VALUE);
712 }
713
714 public void testConstantProperty5() {
715 test("/** @constructor */ function Foo() { this.prop = 1;}" +
716 "/** @const */ Foo.prototype.prop;" +
717 "Foo.prototype.prop = 2",
718 null , CONST_PROPERTY_REASSIGNED_VALUE);
719 }
720
721 public void testConstantProperty6() {
722 test("/** @constructor */ function Foo() { this.prop = 1;}" +
723 "/** @const */ Foo.prototype.prop = 2;",
724 null , CONST_PROPERTY_REASSIGNED_VALUE);
725 }
726
727 public void testConstantProperty7() {
728 testSame("/** @constructor */ function Foo() {} " +
729 "Foo.prototype.bar_ = function() {};" +
730 "/** @constructor \n * @extends {Foo} */ " +
731 "function SubFoo() {};" +
732 "/** @const */ /** @override */ SubFoo.prototype.bar_ = function() {};" +
733 "SubFoo.prototype.baz = function() { this.bar_(); }");
734 }
735
736 public void testConstantProperty8() {
737 testSame("var o = { /** @const */ x: 1 };" +
738 "var y = o.x;");
739 }
740
741 public void testConstantProperty9() {
742 testSame("/** @constructor */ function A() {" +
743 "/** @const */ this.bar = 3;}" +
744 "/** @constructor */ function B() {" +
745 "this.bar = 4;}");
746 }
747
748 public void testConstantProperty10() {
749 testSame("/** @constructor */ function Foo() { this.prop = 1;}" +
750 "/** @const */ Foo.prototype.prop;");
751 }
752
753 public void testConstantProperty11() {
754 test("/** @constructor */ function Foo() {}" +
755 "/** @const */ Foo.prototype.bar;" +
756 "/**\n" +
757 " * @constructor\n" +
758 " * @extends {Foo}\n" +
759 " */ function SubFoo() { this.bar = 5; this.bar = 6; }",
760 null , CONST_PROPERTY_REASSIGNED_VALUE);
761 }
762
763 public void testConstantProperty12() {
764 testSame("/** @constructor */ function Foo() {}" +
765 "/** @const */ Foo.prototype.bar;" +
766 "/**\n" +
767 " * @constructor\n" +
768 " * @extends {Foo}\n" +
769 " */ function SubFoo() { this.bar = 5; }" +
770 "/**\n" +
771 " * @constructor\n" +
772 " * @extends {Foo}\n" +
773 " */ function SubFoo2() { this.bar = 5; }");
774 }
775
776 public void testConstantProperty13() {
777 test("/** @constructor */ function Foo() {}" +
778 "/** @const */ Foo.prototype.bar;" +
779 "/**\n" +
780 " * @constructor\n" +
781 " * @extends {Foo}\n" +
782 " */ function SubFoo() { this.bar = 5; }" +
783 "/**\n" +
784 " * @constructor\n" +
785 " * @extends {SubFoo}\n" +
786 " */ function SubSubFoo() { this.bar = 5; }",
787 null , CONST_PROPERTY_REASSIGNED_VALUE);
788 }
789
790 public void testConstantProperty14() {
791 test("/** @constructor */ function Foo() {" +
792 "/** @const */ this.bar = 3; delete this.bar; }",
793 null, CONST_PROPERTY_DELETED);
794 }
795
796 public void testSuppressConstantProperty() {
797 testSame("/** @constructor */ function A() {" +
798 "/** @const */ this.bar = 3;}" +
799 "/**\n" +
800 " * @suppress {constantProperty}\n" +
801 " * @constructor\n" +
802 " */ function B() {" +
803 "/** @const */ this.bar = 3;this.bar += 4;}");
804 }
805
806 public void testSuppressConstantProperty2() {
807 testSame("/** @constructor */ function A() {" +
808 "/** @const */ this.bar = 3;}" +
809 "/**\n" +
810 " * @suppress {const}\n" +
811 " * @constructor\n" +
812 " */ function B() {" +
813 "/** @const */ this.bar = 3;this.bar += 4;}");
814 }
815
816 public void testFinalClassCannotBeSubclassed() {
817 test(
818 "/**\n"
819 + " * @constructor\n"
820 + " * @const\n"
821 + " */ Foo = function() {};\n"
822 + "/**\n"
823 + " * @constructor\n"
824 + " * @extends {Foo}\n*"
825 + " */ Bar = function() {};",
826 null, EXTEND_FINAL_CLASS);
827 test(
828 "/**\n"
829 + " * @constructor\n"
830 + " * @const\n"
831 + " */ function Foo() {};\n"
832 + "/**\n"
833 + " * @constructor\n"
834 + " * @extends {Foo}\n*"
835 + " */ function Bar() {};",
836 null, EXTEND_FINAL_CLASS);
837 }
838 }