Debian Codegraph @

test/com/google/javascript/jscomp/DisambiguatePropertiesTest.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 package com.google.javascript.jscomp;
17
18 import com.google.common.collect.Maps;
19 import com.google.common.collect.Multimap;
20 import com.google.common.collect.Sets;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.jstype.JSTypeNative;
23 import com.google.javascript.rhino.jstype.JSTypeRegistry;
24 import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
25 import com.google.javascript.rhino.testing.TestErrorReporter;
26
27 import java.util.Collection;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.TreeMap;
31 import java.util.TreeSet;
32
33 /**
34 * Unit test for the Compiler DisambiguateProperties pass.
35 *
36 */
37 public class DisambiguatePropertiesTest extends CompilerTestCase {
38 private DisambiguateProperties<?> lastPass;
39 private boolean runTightenTypes;
40
41 public DisambiguatePropertiesTest() {
42 parseTypeInfo = true;
43 }
44
45 @Override
46 protected void setUp() throws Exception {
47 super.setUp();
48 super.enableNormalize(true);
49 super.enableTypeCheck(CheckLevel.WARNING);
50 }
51
52 @Override
53 public CompilerPass getProcessor(final Compiler compiler) {
54
55 return new CompilerPass() {
56 @Override
57 public void process(Node externs, Node root) {
58 Map<String, CheckLevel> propertiesToErrorFor =
59 Maps.<String, CheckLevel>newHashMap();
60 propertiesToErrorFor.put("foobar", CheckLevel.ERROR);
61
62 if (runTightenTypes) {
63 TightenTypes tightener = new TightenTypes(compiler);
64 tightener.process(externs, root);
65 lastPass = DisambiguateProperties.forConcreteTypeSystem(compiler,
66 tightener, propertiesToErrorFor);
67 } else {
68 // This must be created after type checking is run as it depends on
69 // any mismatches found during checking.
70 lastPass = DisambiguateProperties.forJSTypeSystem(
71 compiler, propertiesToErrorFor);
72 }
73
74 lastPass.process(externs, root);
75 }
76 };
77 }
78
79 @Override
80 protected int getNumRepetitions() {
81 return 1;
82 }
83
84 public void testOneType1() {
85 String js = ""
86 + "/** @constructor */ function Foo() {}\n"
87 + "Foo.prototype.a = 0;\n"
88 + "/** @type Foo */\n"
89 + "var F = new Foo;\n"
90 + "F.a = 0;";
91 testSets(false, js, js, "{a=[[Foo.prototype]]}");
92 testSets(true, js, js, "{a=[[Foo.prototype]]}");
93 }
94
95 public void testOneType2() {
96 String js = ""
97 + "/** @constructor */ function Foo() {}\n"
98 + "Foo.prototype = {a: 0};\n"
99 + "/** @type Foo */\n"
100 + "var F = new Foo;\n"
101 + "F.a = 0;";
102 String expected = "{a=[[Foo.prototype]]}";
103 testSets(false, js, js, expected);
104 testSets(true, js, js, expected);
105 }
106
107 public void testOneType3() {
108 String js = ""
109 + "/** @constructor */ function Foo() {}\n"
110 + "Foo.prototype = { get a() {return 0},"
111 + " set a(b) {} };\n"
112 + "/** @type Foo */\n"
113 + "var F = new Foo;\n"
114 + "F.a = 0;";
115 String expected = "{a=[[Foo.prototype]]}";
116 testSets(false, js, js, expected);
117 testSets(true, js, js, expected);
118 }
119
120 public void testPrototypeAndInstance() {
121 String js = ""
122 + "/** @constructor */ function Foo() {}\n"
123 + "Foo.prototype.a = 0;\n"
124 + "/** @type Foo */\n"
125 + "var F = new Foo;\n"
126 + "F.a = 0;";
127 testSets(false, js, js, "{a=[[Foo.prototype]]}");
128 testSets(true, js, js, "{a=[[Foo.prototype]]}");
129 }
130
131 public void testPrototypeAndInstance2() {
132 String js = ""
133 + "/** @constructor */ function Foo() {}\n"
134 + "Foo.prototype.a = 0;\n"
135 + "new Foo().a = 0;";
136 testSets(false, js, js, "{a=[[Foo.prototype]]}");
137 testSets(true, js, js, "{a=[[Foo.prototype]]}");
138 }
139
140 public void testTwoTypes1() {
141 String js = ""
142 + "/** @constructor */ function Foo() {}\n"
143 + "Foo.prototype.a = 0;"
144 + "/** @type Foo */\n"
145 + "var F = new Foo;\n"
146 + "F.a = 0;"
147 + "/** @constructor */ function Bar() {}\n"
148 + "Bar.prototype.a = 0;"
149 + "/** @type Bar */\n"
150 + "var B = new Bar;\n"
151 + "B.a = 0;";
152 String output = ""
153 + "function Foo(){}"
154 + "Foo.prototype.Foo_prototype$a=0;"
155 + "var F=new Foo;"
156 + "F.Foo_prototype$a=0;"
157 + "function Bar(){}"
158 + "Bar.prototype.Bar_prototype$a=0;"
159 + "var B=new Bar;"
160 + "B.Bar_prototype$a=0";
161 testSets(false, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
162 testSets(true, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
163 }
164
165 public void testTwoTypes2() {
166 String js = ""
167 + "/** @constructor */ function Foo() {}\n"
168 + "Foo.prototype = {a: 0};"
169 + "/** @type Foo */\n"
170 + "var F = new Foo;\n"
171 + "F.a = 0;"
172 + "/** @constructor */ function Bar() {}\n"
173 + "Bar.prototype = {a: 0};"
174 + "/** @type Bar */\n"
175 + "var B = new Bar;\n"
176 + "B.a = 0;";
177
178 String output = ""
179 + "function Foo(){}"
180 + "Foo.prototype = {Foo_prototype$a: 0};"
181 + "var F=new Foo;"
182 + "F.Foo_prototype$a=0;"
183 + "function Bar(){}"
184 + "Bar.prototype = {Bar_prototype$a: 0};"
185 + "var B=new Bar;"
186 + "B.Bar_prototype$a=0";
187
188 testSets(false, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
189 testSets(true, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
190 }
191
192 public void testTwoTypes3() {
193 String js = ""
194 + "/** @constructor */ function Foo() {}\n"
195 + "Foo.prototype = { get a() {return 0},"
196 + " set a(b) {} };\n"
197 + "/** @type Foo */\n"
198 + "var F = new Foo;\n"
199 + "F.a = 0;"
200 + "/** @constructor */ function Bar() {}\n"
201 + "Bar.prototype = { get a() {return 0},"
202 + " set a(b) {} };\n"
203 + "/** @type Bar */\n"
204 + "var B = new Bar;\n"
205 + "B.a = 0;";
206
207 String output = ""
208 + "function Foo(){}"
209 + "Foo.prototype = { get Foo_prototype$a() {return 0},"
210 + " set Foo_prototype$a(b) {} };\n"
211 + "var F=new Foo;"
212 + "F.Foo_prototype$a=0;"
213 + "function Bar(){}"
214 + "Bar.prototype = { get Bar_prototype$a() {return 0},"
215 + " set Bar_prototype$a(b) {} };\n"
216 + "var B=new Bar;"
217 + "B.Bar_prototype$a=0";
218
219 testSets(false, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
220 testSets(true, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
221 }
222
223 public void testTwoFields() {
224 String js = ""
225 + "/** @constructor */ function Foo() {}\n"
226 + "Foo.prototype.a = 0;"
227 + "Foo.prototype.b = 0;"
228 + "/** @type Foo */\n"
229 + "var F = new Foo;\n"
230 + "F.a = 0;"
231 + "F.b = 0;";
232 String output = "function Foo(){}Foo.prototype.a=0;Foo.prototype.b=0;"
233 + "var F=new Foo;F.a=0;F.b=0";
234 testSets(false, js, output, "{a=[[Foo.prototype]], b=[[Foo.prototype]]}");
235 testSets(true, js, output, "{a=[[Foo.prototype]], b=[[Foo.prototype]]}");
236 }
237
238 public void testTwoSeparateFieldsTwoTypes() {
239 String js = ""
240 + "/** @constructor */ function Foo() {}\n"
241 + "Foo.prototype.a = 0;"
242 + "Foo.prototype.b = 0;"
243 + "/** @type Foo */\n"
244 + "var F = new Foo;\n"
245 + "F.a = 0;"
246 + "F.b = 0;"
247 + "/** @constructor */ function Bar() {}\n"
248 + "Bar.prototype.a = 0;"
249 + "Bar.prototype.b = 0;"
250 + "/** @type Bar */\n"
251 + "var B = new Bar;\n"
252 + "B.a = 0;"
253 + "B.b = 0;";
254 String output = ""
255 + "function Foo(){}"
256 + "Foo.prototype.Foo_prototype$a=0;"
257 + "Foo.prototype.Foo_prototype$b=0;"
258 + "var F=new Foo;"
259 + "F.Foo_prototype$a=0;"
260 + "F.Foo_prototype$b=0;"
261 + "function Bar(){}"
262 + "Bar.prototype.Bar_prototype$a=0;"
263 + "Bar.prototype.Bar_prototype$b=0;"
264 + "var B=new Bar;"
265 + "B.Bar_prototype$a=0;"
266 + "B.Bar_prototype$b=0";
267 testSets(false, js, output, "{a=[[Bar.prototype], [Foo.prototype]],"
268 + " b=[[Bar.prototype], [Foo.prototype]]}");
269 testSets(true, js, output, "{a=[[Bar.prototype], [Foo.prototype]],"
270 + " b=[[Bar.prototype], [Foo.prototype]]}");
271 }
272
273 public void testUnionType() {
274 String js = ""
275 + "/** @constructor */ function Foo() {}\n"
276 + "Foo.prototype.a = 0;"
277 + "/** @constructor */ function Bar() {}\n"
278 + "Bar.prototype.a = 0;"
279 + "/** @type {Bar|Foo} */\n"
280 + "var B = new Bar;\n"
281 + "B.a = 0;\n"
282 + "B = new Foo;\n"
283 + "B.a = 0;\n"
284 + "/** @constructor */ function Baz() {}\n"
285 + "Baz.prototype.a = 0;\n";
286 testSets(false, js,
287 "{a=[[Bar.prototype, Foo.prototype], [Baz.prototype]]}");
288 testSets(true, js, "{a=[[Bar.prototype, Foo.prototype], [Baz.prototype]]}");
289 }
290
291 public void testIgnoreUnknownType() {
292 String js = ""
293 + "/** @constructor */\n"
294 + "function Foo() {}\n"
295 + "Foo.prototype.blah = 3;\n"
296 + "/** @type {Foo} */\n"
297 + "var F = new Foo;\n"
298 + "F.blah = 0;\n"
299 + "var U = function() { return {} };\n"
300 + "U().blah();";
301 String expected = ""
302 + "function Foo(){}Foo.prototype.blah=3;var F = new Foo;F.blah=0;"
303 + "var U=function(){return{}};U().blah()";
304 testSets(false, js, expected, "{}");
305 testSets(true, BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES,
306 js, expected, "{}");
307 }
308
309 public void testIgnoreUnknownType1() {
310 String js = ""
311 + "/** @constructor */\n"
312 + "function Foo() {}\n"
313 + "Foo.prototype.blah = 3;\n"
314 + "/** @type {Foo} */\n"
315 + "var F = new Foo;\n"
316 + "F.blah = 0;\n"
317 + "/** @return {Object} */\n"
318 + "var U = function() { return {} };\n"
319 + "U().blah();";
320 String expected = ""
321 + "function Foo(){}Foo.prototype.blah=3;var F = new Foo;F.blah=0;"
322 + "var U=function(){return{}};U().blah()";
323 testSets(false, js, expected, "{blah=[[Foo.prototype]]}");
324 testSets(true, BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES,
325 js, expected, "{}");
326 }
327
328 public void testIgnoreUnknownType2() {
329 String js = ""
330 + "/** @constructor */\n"
331 + "function Foo() {}\n"
332 + "Foo.prototype.blah = 3;\n"
333 + "/** @type {Foo} */\n"
334 + "var F = new Foo;\n"
335 + "F.blah = 0;\n"
336 + "/** @constructor */\n"
337 + "function Bar() {}\n"
338 + "Bar.prototype.blah = 3;\n"
339 + "/** @return {Object} */\n"
340 + "var U = function() { return {} };\n"
341 + "U().blah();";
342 String expected = ""
343 + "function Foo(){}Foo.prototype.blah=3;var F = new Foo;F.blah=0;"
344 + "function Bar(){}Bar.prototype.blah=3;"
345 + "var U=function(){return{}};U().blah()";
346 testSets(false, js, expected, "{}");
347 testSets(true, BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES,
348 js, expected, "{}");
349 }
350
351 public void testUnionTypeTwoFields() {
352 String js = ""
353 + "/** @constructor */ function Foo() {}\n"
354 + "Foo.prototype.a = 0;\n"
355 + "Foo.prototype.b = 0;\n"
356 + "/** @constructor */ function Bar() {}\n"
357 + "Bar.prototype.a = 0;\n"
358 + "Bar.prototype.b = 0;\n"
359 + "/** @type {Foo|Bar} */\n"
360 + "var B = new Bar;\n"
361 + "B.a = 0;\n"
362 + "B.b = 0;\n"
363 + "B = new Foo;\n"
364 + "/** @constructor */ function Baz() {}\n"
365 + "Baz.prototype.a = 0;\n"
366 + "Baz.prototype.b = 0;\n";
367 testSets(false, js, "{a=[[Bar.prototype, Foo.prototype], [Baz.prototype]],"
368 + " b=[[Bar.prototype, Foo.prototype], [Baz.prototype]]}");
369 testSets(true, js, "{a=[[Bar.prototype, Foo.prototype], [Baz.prototype]],"
370 + " b=[[Bar.prototype, Foo.prototype], [Baz.prototype]]}");
371 }
372
373 public void testCast() {
374 String js = ""
375 + "/** @constructor */ function Foo() {}\n"
376 + "Foo.prototype.a = 0;"
377 + "/** @constructor */ function Bar() {}\n"
378 + "Bar.prototype.a = 0;"
379 + "/** @type {Foo|Bar} */\n"
380 + "var F = new Foo;\n"
381 + "(/** @type {Bar} */(F)).a = 0;";
382 String output = ""
383 + "function Foo(){}Foo.prototype.Foo_prototype$a=0;"
384 + "function Bar(){}Bar.prototype.Bar_prototype$a=0;"
385 + "var F=new Foo;F.Bar_prototype$a=0;";
386 String ttOutput = ""
387 + "function Foo(){}Foo.prototype.Foo_prototype$a=0;"
388 + "function Bar(){}Bar.prototype.Bar_prototype$a=0;"
389 + "var F=new Foo;F.Unique$1$a=0;";
390 testSets(false, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
391 testSets(true, js, ttOutput,
392 "{a=[[Bar.prototype], [Foo.prototype], [Unique$1]]}");
393 }
394
395 public void testConstructorFields() {
396 String js = ""
397 + "/** @constructor */\n"
398 + "var Foo = function() { this.a = 0; };\n"
399 + "/** @constructor */ function Bar() {}\n"
400 + "Bar.prototype.a = 0;"
401 + "new Foo";
402 String output = ""
403 + "var Foo=function(){this.Foo$a=0};"
404 + "function Bar(){}"
405 + "Bar.prototype.Bar_prototype$a=0;"
406 + "new Foo";
407 String ttOutput = ""
408 + "var Foo=function(){this.Foo_prototype$a=0};"
409 + "function Bar(){}"
410 + "Bar.prototype.Bar_prototype$a=0;"
411 + "new Foo";
412 testSets(false, js, output, "{a=[[Bar.prototype], [Foo]]}");
413 testSets(true, js, ttOutput, "{a=[[Bar.prototype], [Foo.prototype]]}");
414 }
415
416 public void testStaticProperty() {
417 String js = ""
418 + "/** @constructor */ function Foo() {} \n"
419 + "/** @constructor */ function Bar() {}\n"
420 + "Foo.a = 0;"
421 + "Bar.a = 0;";
422 String output = ""
423 + "function Foo(){}"
424 + "function Bar(){}"
425 + "Foo.function__new_Foo___undefined$a = 0;"
426 + "Bar.function__new_Bar___undefined$a = 0;";
427
428 testSets(false, js, output,
429 "{a=[[function (new:Bar): undefined]," +
430 " [function (new:Foo): undefined]]}");
431 }
432
433 public void testSupertypeWithSameField() {
434 String js = ""
435 + "/** @constructor */ function Foo() {}\n"
436 + "Foo.prototype.a = 0;\n"
437 + "/** @constructor\n* @extends Foo */ function Bar() {}\n"
438 + "/** @override */\n"
439 + "Bar.prototype.a = 0;\n"
440 + "/** @type Bar */ var B = new Bar;\n"
441 + "B.a = 0;"
442 + "/** @constructor */ function Baz() {}\n"
443 + "Baz.prototype.a = function(){};\n";
444
445 String output = ""
446 + "function Foo(){}Foo.prototype.Foo_prototype$a=0;"
447 + "function Bar(){}Bar.prototype.Foo_prototype$a=0;"
448 + "var B = new Bar;B.Foo_prototype$a=0;"
449 + "function Baz(){}Baz.prototype.Baz_prototype$a=function(){};";
450 String ttOutput = ""
451 + "function Foo(){}Foo.prototype.Foo_prototype$a=0;"
452 + "function Bar(){}Bar.prototype.Bar_prototype$a=0;"
453 + "var B = new Bar;B.Bar_prototype$a=0;"
454 + "function Baz(){}Baz.prototype.Baz_prototype$a=function(){};";
455 testSets(false, js, output, "{a=[[Baz.prototype], [Foo.prototype]]}");
456 testSets(true, js, ttOutput,
457 "{a=[[Bar.prototype], [Baz.prototype], [Foo.prototype]]}");
458 }
459
460 public void testScopedType() {
461 String js = ""
462 + "var g = {};\n"
463 + "/** @constructor */ g.Foo = function() {}\n"
464 + "g.Foo.prototype.a = 0;"
465 + "/** @constructor */ g.Bar = function() {}\n"
466 + "g.Bar.prototype.a = 0;";
467 String output = ""
468 + "var g={};"
469 + "g.Foo=function(){};"
470 + "g.Foo.prototype.g_Foo_prototype$a=0;"
471 + "g.Bar=function(){};"
472 + "g.Bar.prototype.g_Bar_prototype$a=0;";
473 testSets(false, js, output, "{a=[[g.Bar.prototype], [g.Foo.prototype]]}");
474 testSets(true, js, output, "{a=[[g.Bar.prototype], [g.Foo.prototype]]}");
475 }
476
477 public void testUnresolvedType() {
478 // NOTE(nicksantos): This behavior seems very wrong to me.
479 String js = ""
480 + "var g = {};"
481 + "/** @constructor \n @extends {?} */ "
482 + "var Foo = function() {};\n"
483 + "Foo.prototype.a = 0;"
484 + "/** @constructor */ var Bar = function() {};\n"
485 + "Bar.prototype.a = 0;";
486 String output = ""
487 + "var g={};"
488 + "var Foo=function(){};"
489 + "Foo.prototype.Foo_prototype$a=0;"
490 + "var Bar=function(){};"
491 + "Bar.prototype.Bar_prototype$a=0;";
492
493 setExpectParseWarningsThisTest();
494 testSets(false, BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES,
495 js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
496 testSets(true, BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES,
497 js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
498 }
499
500 public void testNamedType() {
501 String js = ""
502 + "var g = {};"
503 + "/** @constructor \n @extends g.Late */ var Foo = function() {}\n"
504 + "Foo.prototype.a = 0;"
505 + "/** @constructor */ var Bar = function() {}\n"
506 + "Bar.prototype.a = 0;"
507 + "/** @constructor */ g.Late = function() {}";
508 String output = ""
509 + "var g={};"
510 + "var Foo=function(){};"
511 + "Foo.prototype.Foo_prototype$a=0;"
512 + "var Bar=function(){};"
513 + "Bar.prototype.Bar_prototype$a=0;"
514 + "g.Late = function(){}";
515 testSets(false, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
516 testSets(true, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
517 }
518
519 public void testUnknownType() {
520 String js = ""
521 + "/** @constructor */ var Foo = function() {};\n"
522 + "/** @constructor */ var Bar = function() {};\n"
523 + "/** @return {?} */ function fun() {}\n"
524 + "Foo.prototype.a = fun();\n"
525 + "fun().a;\n"
526 + "Bar.prototype.a = 0;";
527 String ttOutput = ""
528 + "var Foo=function(){};\n"
529 + "var Bar=function(){};\n"
530 + "function fun(){}\n"
531 + "Foo.prototype.Foo_prototype$a=fun();\n"
532 + "fun().Unique$1$a;\n"
533 + "Bar.prototype.Bar_prototype$a=0;";
534 testSets(false, js, js, "{}");
535 testSets(true, BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES, js, ttOutput,
536 "{a=[[Bar.prototype], [Foo.prototype], [Unique$1]]}");
537 }
538
539 public void testEnum() {
540 String js = ""
541 + "/** @enum {string} */ var En = {\n"
542 + " A: 'first',\n"
543 + " B: 'second'\n"
544 + "};\n"
545 + "var EA = En.A;\n"
546 + "var EB = En.B;\n"
547 + "/** @constructor */ function Foo(){};\n"
548 + "Foo.prototype.A = 0;\n"
549 + "Foo.prototype.B = 0;\n";
550 String output = ""
551 + "var En={A:'first',B:'second'};"
552 + "var EA=En.A;"
553 + "var EB=En.B;"
554 + "function Foo(){};"
555 + "Foo.prototype.Foo_prototype$A=0;"
556 + "Foo.prototype.Foo_prototype$B=0";
557 String ttOutput = ""
558 + "var En={A:'first',B:'second'};"
559 + "var EA=En.A;"
560 + "var EB=En.B;"
561 + "function Foo(){};"
562 + "Foo.prototype.Foo_prototype$A=0;"
563 + "Foo.prototype.Foo_prototype$B=0";
564 testSets(false, js, output, "{A=[[Foo.prototype]], B=[[Foo.prototype]]}");
565 testSets(true, js, ttOutput, "{A=[[Foo.prototype]], B=[[Foo.prototype]]}");
566 }
567
568 public void testEnumOfObjects() {
569 String js = ""
570 + "/** @constructor */ function Formatter() {}"
571 + "Formatter.prototype.format = function() {};"
572 + "/** @constructor */ function Unrelated() {}"
573 + "Unrelated.prototype.format = function() {};"
574 + "/** @enum {!Formatter} */ var Enum = {\n"
575 + " A: new Formatter()\n"
576 + "};\n"
577 + "Enum.A.format();\n";
578 String output = ""
579 + "/** @constructor */ function Formatter() {}"
580 + "Formatter.prototype.Formatter_prototype$format = function() {};"
581 + "/** @constructor */ function Unrelated() {}"
582 + "Unrelated.prototype.Unrelated_prototype$format = function() {};"
583 + "/** @enum {!Formatter} */ var Enum = {\n"
584 + " A: new Formatter()\n"
585 + "};\n"
586 + "Enum.A.Formatter_prototype$format();\n";
587 testSets(false, js, output,
588 "{format=[[Formatter.prototype], [Unrelated.prototype]]}");
589
590 // TODO(nicksantos): Fix the type tightener to handle this case.
591 // It currently doesn't work, because getSubTypes is broken for enums.
592 }
593
594 public void testEnumOfObjects2() {
595 String js = ""
596 + "/** @constructor */ function Formatter() {}"
597 + "Formatter.prototype.format = function() {};"
598 + "/** @constructor */ function Unrelated() {}"
599 + "Unrelated.prototype.format = function() {};"
600 + "/** @enum {?Formatter} */ var Enum = {\n"
601 + " A: new Formatter(),\n"
602 + " B: new Formatter()\n"
603 + "};\n"
604 + "function f() {\n"
605 + " var formatter = window.toString() ? Enum.A : Enum.B;\n"
606 + " formatter.format();\n"
607 + "}";
608 String output = ""
609 + "/** @constructor */ function Formatter() {}"
610 + "Formatter.prototype.format = function() {};"
611 + "/** @constructor */ function Unrelated() {}"
612 + "Unrelated.prototype.format = function() {};"
613 + "/** @enum {?Formatter} */ var Enum = {\n"
614 + " A: new Formatter(),\n"
615 + " B: new Formatter()\n"
616 + "};\n"
617 + "function f() {\n"
618 + " var formatter = window.toString() ? Enum.A : Enum.B;\n"
619 + " formatter.format();\n"
620 + "}";
621 testSets(false, js, output, "{}");
622 }
623
624 public void testEnumOfObjects3() {
625 String js = ""
626 + "/** @constructor */ function Formatter() {}"
627 + "Formatter.prototype.format = function() {};"
628 + "/** @constructor */ function Unrelated() {}"
629 + "Unrelated.prototype.format = function() {};"
630 + "/** @enum {!Formatter} */ var Enum = {\n"
631 + " A: new Formatter(),\n"
632 + " B: new Formatter()\n"
633 + "};\n"
634 + "/** @enum {!Enum} */ var SubEnum = {\n"
635 + " C: Enum.A\n"
636 + "};\n"
637 + "function f() {\n"
638 + " var formatter = SubEnum.C\n"
639 + " formatter.format();\n"
640 + "}";
641 String output = ""
642 + "/** @constructor */ function Formatter() {}"
643 + "Formatter.prototype.Formatter_prototype$format = function() {};"
644 + "/** @constructor */ function Unrelated() {}"
645 + "Unrelated.prototype.Unrelated_prototype$format = function() {};"
646 + "/** @enum {!Formatter} */ var Enum = {\n"
647 + " A: new Formatter(),\n"
648 + " B: new Formatter()\n"
649 + "};\n"
650 + "/** @enum {!Enum} */ var SubEnum = {\n"
651 + " C: Enum.A\n"
652 + "};\n"
653 + "function f() {\n"
654 + " var formatter = SubEnum.C\n"
655 + " formatter.Formatter_prototype$format();\n"
656 + "}";
657 testSets(false, js, output,
658 "{format=[[Formatter.prototype], [Unrelated.prototype]]}");
659 }
660
661 public void testUntypedExterns() {
662 String externs =
663 BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES
664 + "var window;"
665 + "window.alert = function() {x};";
666 String js = ""
667 + "/** @constructor */ function Foo() {}\n"
668 + "Foo.prototype.a = 0;\n"
669 + "Foo.prototype.alert = 0;\n"
670 + "Foo.prototype.window = 0;\n"
671 + "/** @constructor */ function Bar() {}\n"
672 + "Bar.prototype.a = 0;\n"
673 + "Bar.prototype.alert = 0;\n"
674 + "Bar.prototype.window = 0;\n"
675 + "window.alert();";
676 String output = ""
677 + "function Foo(){}"
678 + "Foo.prototype.Foo_prototype$a=0;"
679 + "Foo.prototype.alert=0;"
680 + "Foo.prototype.Foo_prototype$window=0;"
681 + "function Bar(){}"
682 + "Bar.prototype.Bar_prototype$a=0;"
683 + "Bar.prototype.alert=0;"
684 + "Bar.prototype.Bar_prototype$window=0;"
685 + "window.alert();";
686
687 testSets(false, externs, js, output, "{a=[[Bar.prototype], [Foo.prototype]]"
688 + ", window=[[Bar.prototype], [Foo.prototype]]}");
689 testSets(true, externs, js, output, "{a=[[Bar.prototype], [Foo.prototype]],"
690 + " window=[[Bar.prototype], [Foo.prototype]]}");
691 }
692
693 public void testUnionTypeInvalidation() {
694 String externs = ""
695 + "/** @constructor */ function Baz() {}"
696 + "Baz.prototype.a";
697 String js = ""
698 + "/** @constructor */ function Ind() {this.a=0}\n"
699 + "/** @constructor */ function Foo() {}\n"
700 + "Foo.prototype.a = 0;\n"
701 + "/** @constructor */ function Bar() {}\n"
702 + "Bar.prototype.a = 0;\n"
703 + "/** @type {Foo|Bar} */\n"
704 + "var F = new Foo;\n"
705 + "F.a = 1\n;"
706 + "F = new Bar;\n"
707 + "/** @type {Baz} */\n"
708 + "var Z = new Baz;\n"
709 + "Z.a = 1\n;"
710 + "/** @type {Bar|Baz} */\n"
711 + "var B = new Baz;\n"
712 + "B.a = 1;\n"
713 + "B = new Bar;\n";
714 // Only the constructor field a of Ind is renamed, as Foo is related to Baz
715 // through Bar in the unions Bar|Baz and Foo|Bar.
716 String output = ""
717 + "function Ind() { this.Ind$a = 0; }"
718 + "function Foo() {}"
719 + "Foo.prototype.a = 0;"
720 + "function Bar() {}"
721 + "Bar.prototype.a = 0;"
722 + "var F = new Foo;"
723 + "F.a = 1;"
724 + "F = new Bar;"
725 + "var Z = new Baz;"
726 + "Z.a = 1;"
727 + "var B = new Baz;"
728 + "B.a = 1;"
729 + "B = new Bar;";
730 String ttOutput = ""
731 + "function Ind() { this.Unique$1$a = 0; }"
732 + "function Foo() {}"
733 + "Foo.prototype.a = 0;"
734 + "function Bar() {}"
735 + "Bar.prototype.a = 0;"
736 + "var F = new Foo;"
737 + "F.a = 1;"
738 + "F = new Bar;"
739 + "var Z = new Baz;"
740 + "Z.a = 1;"
741 + "var B = new Baz;"
742 + "B.a = 1;"
743 + "B = new Bar;";
744 testSets(false, externs, js, output, "{a=[[Ind]]}");
745 testSets(true, externs, js, ttOutput, "{a=[[Unique$1]]}");
746 }
747
748 public void testUnionAndExternTypes() {
749 String externs = ""
750 + "/** @constructor */ function Foo() { }"
751 + "Foo.prototype.a = 4;\n";
752 String js = ""
753 + "/** @constructor */ function Bar() { this.a = 2; }\n"
754 + "/** @constructor */ function Baz() { this.a = 3; }\n"
755 + "/** @constructor */ function Buz() { this.a = 4; }\n"
756 + "/** @constructor */ function T1() { this.a = 3; }\n"
757 + "/** @constructor */ function T2() { this.a = 3; }\n"
758 + "/** @type {Bar|Baz} */ var b;\n"
759 + "/** @type {Baz|Buz} */ var c;\n"
760 + "/** @type {Buz|Foo} */ var d;\n"
761 + "b.a = 5; c.a = 6; d.a = 7;";
762 String output = ""
763 + "/** @constructor */ function Bar() { this.a = 2; }\n"
764 + "/** @constructor */ function Baz() { this.a = 3; }\n"
765 + "/** @constructor */ function Buz() { this.a = 4; }\n"
766 + "/** @constructor */ function T1() { this.T1$a = 3; }\n"
767 + "/** @constructor */ function T2() { this.T2$a = 3; }\n"
768 + "/** @type {Bar|Baz} */ var b;\n"
769 + "/** @type {Baz|Buz} */ var c;\n"
770 + "/** @type {Buz|Foo} */ var d;\n"
771 + "b.a = 5; c.a = 6; d.a = 7;";
772
773 // We are testing the skipping of multiple types caused by unionizing with
774 // extern types.
775 testSets(false, externs, js, output, "{a=[[T1], [T2]]}");
776 }
777
778 public void testTypedExterns() {
779 String externs = ""
780 + "/** @constructor */ function Window() {};\n"
781 + "Window.prototype.alert;"
782 + "/** @type {Window} */"
783 + "var window;";
784 String js = ""
785 + "/** @constructor */ function Foo() {}\n"
786 + "Foo.prototype.alert = 0;\n"
787 + "window.alert('blarg');";
788 String output = ""
789 + "function Foo(){}"
790 + "Foo.prototype.Foo_prototype$alert=0;"
791 + "window.alert('blarg');";
792 testSets(false, externs, js, output, "{alert=[[Foo.prototype]]}");
793 testSets(true, externs, js, output, "{alert=[[Foo.prototype]]}");
794 }
795
796 public void testSubtypesWithSameField() {
797 String js = ""
798 + "/** @constructor */ function Top() {}\n"
799 + "/** @constructor \n@extends Top*/ function Foo() {}\n"
800 + "Foo.prototype.a;\n"
801 + "/** @constructor \n@extends Top*/ function Bar() {}\n"
802 + "Bar.prototype.a;\n"
803 + "/** @param {Top} top */"
804 + "function foo(top) {\n"
805 + " var x = top.a;\n"
806 + "}\n"
807 + "foo(new Foo);\n"
808 + "foo(new Bar);\n";
809 testSets(false, js, "{}");
810 testSets(true, js, "{a=[[Bar.prototype, Foo.prototype]]}");
811 }
812
813 public void testSupertypeReferenceOfSubtypeProperty() {
814 String externs = ""
815 + "/** @constructor */ function Ext() {}"
816 + "Ext.prototype.a;";
817 String js = ""
818 + "/** @constructor */ function Foo() {}\n"
819 + "/** @constructor \n@extends Foo*/ function Bar() {}\n"
820 + "Bar.prototype.a;\n"
821 + "/** @param {Foo} foo */"
822 + "function foo(foo) {\n"
823 + " var x = foo.a;\n"
824 + "}\n";
825 String result = ""
826 + "function Foo() {}\n"
827 + "function Bar() {}\n"
828 + "Bar.prototype.Bar_prototype$a;\n"
829 + "function foo(foo$$1) {\n"
830 + " var x = foo$$1.Bar_prototype$a;\n"
831 + "}\n";
832 testSets(false, externs, js, result, "{a=[[Bar.prototype]]}");
833 }
834
835 public void testObjectLiteralNotRenamed() {
836 String js = ""
837 + "var F = {a:'a', b:'b'};"
838 + "F.a = 'z';";
839 testSets(false, js, js, "{}");
840 testSets(true, js, js, "{}");
841 }
842
843 public void testObjectLiteralReflected() {
844 String js = ""
845 + "var goog = {};"
846 + "goog.reflect = {};"
847 + "goog.reflect.object = function(x, y) { return y; };"
848 + "/** @constructor */ function F() {}"
849 + "/** @type {number} */ F.prototype.foo = 3;"
850 + "/** @constructor */ function G() {}"
851 + "/** @type {number} */ G.prototype.foo = 3;"
852 + "goog.reflect.object(F, {foo: 5});";
853 String result = ""
854 + "var goog = {};"
855 + "goog.reflect = {};"
856 + "goog.reflect.object = function(x, y) { return y; };"
857 + "function F() {}"
858 + "F.prototype.F_prototype$foo = 3;"
859 + "function G() {}"
860 + "G.prototype.G_prototype$foo = 3;"
861 + "goog.reflect.object(F, {F_prototype$foo: 5});";
862 testSets(false, js, result, "{foo=[[F.prototype], [G.prototype]]}");
863 testSets(true, js, result, "{foo=[[F.prototype], [G.prototype]]}");
864 }
865
866 public void testObjectLiteralLends() {
867 String js = ""
868 + "var mixin = function(x) { return x; };"
869 + "/** @constructor */ function F() {}"
870 + "/** @type {number} */ F.prototype.foo = 3;"
871 + "/** @constructor */ function G() {}"
872 + "/** @type {number} */ G.prototype.foo = 3;"
873 + "mixin(/** @lends {F.prototype} */ ({foo: 5}));";
874 String result = ""
875 + "var mixin = function(x) { return x; };"
876 + "function F() {}"
877 + "F.prototype.F_prototype$foo = 3;"
878 + "function G() {}"
879 + "G.prototype.G_prototype$foo = 3;"
880 + "mixin(/** @lends {F.prototype} */ ({F_prototype$foo: 5}));";
881 testSets(false, js, result, "{foo=[[F.prototype], [G.prototype]]}");
882 testSets(true, js, result, "{foo=[[F.prototype], [G.prototype]]}");
883 }
884
885 public void testClosureInherits() {
886 String js = ""
887 + "var goog = {};"
888 + "/** @param {Function} childCtor Child class.\n"
889 + " * @param {Function} parentCtor Parent class. */\n"
890 + "goog.inherits = function(childCtor, parentCtor) {\n"
891 + " /** @constructor */\n"
892 + " function tempCtor() {};\n"
893 + " tempCtor.prototype = parentCtor.prototype;\n"
894 + " childCtor.superClass_ = parentCtor.prototype;\n"
895 + " childCtor.prototype = new tempCtor();\n"
896 + " childCtor.prototype.constructor = childCtor;\n"
897 + "};"
898 + "/** @constructor */ function Top() {}\n"
899 + "Top.prototype.f = function() {};"
900 + "/** @constructor \n@extends Top*/ function Foo() {}\n"
901 + "goog.inherits(Foo, Top);\n"
902 + "/** @override */\n"
903 + "Foo.prototype.f = function() {"
904 + " Foo.superClass_.f();"
905 + "};\n"
906 + "/** @constructor \n* @extends Foo */ function Bar() {}\n"
907 + "goog.inherits(Bar, Foo);\n"
908 + "/** @override */\n"
909 + "Bar.prototype.f = function() {"
910 + " Bar.superClass_.f();"
911 + "};\n"
912 + "(new Bar).f();\n";
913 testSets(false, js, "{f=[[Top.prototype]]}");
914 testSets(true, js, "{constructor=[[Bar.prototype, Foo.prototype]], "
915 + "f=[[Bar.prototype], [Foo.prototype], [Top.prototype]]}");
916 }
917
918 public void testSkipNativeFunctionMethod() {
919 String externs = ""
920 + "/** @constructor \n @param {*} var_args */"
921 + "function Function(var_args) {}"
922 + "Function.prototype.call = function() {};";
923 String js = ""
924 + "/** @constructor */ function Foo(){};"
925 + "/** @constructor\n @extends Foo */"
926 + "function Bar() { Foo.call(this); };"; // call should not be renamed
927 testSame(externs, js, null);
928 }
929
930 public void testSkipNativeObjectMethod() {
931 String externs = ""
932 + "/** @constructor \n @param {*} opt_v */ function Object(opt_v) {}"
933 + "Object.prototype.hasOwnProperty;";
934 String js = ""
935 + "/** @constructor */ function Foo(){};"
936 + "(new Foo).hasOwnProperty('x');";
937 testSets(false, externs, js, js, "{}");
938 testSets(true, externs, js, js, "{}");
939 }
940
941 public void testExtendNativeType() {
942 String externs = ""
943 + "/** @constructor \n @return {string} */"
944 + "function Date(opt_1, opt_2, opt_3, opt_4, opt_5, opt_6, opt_7) {}"
945 + "/** @override */ Date.prototype.toString = function() {}";
946 String js = ""
947 + "/** @constructor\n @extends {Date} */ function SuperDate() {};\n"
948 + "(new SuperDate).toString();";
949 testSets(true, externs, js, js, "{}");
950 testSets(false, externs, js, js, "{}");
951 }
952
953 public void testStringFunction() {
954 // Extern functions are not renamed, but user functions on a native
955 // prototype object are.
956 String externs = "/**@constructor\n@param {*} opt_str \n @return {string}*/"
957 + "function String(opt_str) {};\n"
958 + "/** @override \n @return {string} */\n"
959 + "String.prototype.toString = function() { };\n";
960 String js = ""
961 + "/** @constructor */ function Foo() {};\n"
962 + "Foo.prototype.foo = function() {};\n"
963 + "String.prototype.foo = function() {};\n"
964 + "var a = 'str'.toString().foo();\n";
965 String output = ""
966 + "function Foo() {};\n"
967 + "Foo.prototype.Foo_prototype$foo = function() {};\n"
968 + "String.prototype.String_prototype$foo = function() {};\n"
969 + "var a = 'str'.toString().String_prototype$foo();\n";
970
971 testSets(false, externs, js, output,
972 "{foo=[[Foo.prototype], [String.prototype]]}");
973 testSets(true, externs, js, output,
974 "{foo=[[Foo.prototype], [String.prototype]]}");
975 }
976
977 public void testUnusedTypeInExterns() {
978 String externs = ""
979 + "/** @constructor */ function Foo() {};\n"
980 + "Foo.prototype.a";
981 String js = ""
982 + "/** @constructor */ function Bar() {};\n"
983 + "Bar.prototype.a;"
984 + "/** @constructor */ function Baz() {};\n"
985 + "Baz.prototype.a;";
986 String output = ""
987 + "/** @constructor */ function Bar() {};\n"
988 + "Bar.prototype.Bar_prototype$a;"
989 + "/** @constructor */ function Baz() {};\n"
990 + "Baz.prototype.Baz_prototype$a";
991 testSets(false, externs, js, output,
992 "{a=[[Bar.prototype], [Baz.prototype]]}");
993 testSets(true, externs, js, output,
994 "{a=[[Bar.prototype], [Baz.prototype]]}");
995 }
996
997 public void testInterface() {
998 String js = ""
999 + "/** @interface */ function I() {};\n"
1000 + "I.prototype.a;\n"
1001 + "/** @constructor \n @implements I */ function Foo() {};\n"
1002 + "Foo.prototype.a;\n"
1003 + "/** @type I */\n"
1004 + "var F = new Foo;"
1005 + "var x = F.a;";
1006 testSets(false, js, "{a=[[Foo.prototype, I.prototype]]}");
1007 testSets(true, js, "{a=[[Foo.prototype], [I.prototype]]}");
1008 }
1009
1010 public void testInterfaceOfSuperclass() {
1011 String js = ""
1012 + "/** @interface */ function I() {};\n"
1013 + "I.prototype.a;\n"
1014 + "/** @constructor \n @implements I */ function Foo() {};\n"
1015 + "Foo.prototype.a;\n"
1016 + "/** @constructor \n @extends Foo */ function Bar() {};\n"
1017 + "Bar.prototype.a;\n"
1018 + "/** @type Bar */\n"
1019 + "var B = new Bar;"
1020 + "B.a = 0";
1021 testSets(false, js, "{a=[[Foo.prototype, I.prototype]]}");
1022 testSets(true, js,
1023 "{a=[[Bar.prototype], [Foo.prototype], [I.prototype]]}");
1024 }
1025
1026 public void testTwoInterfacesWithSomeInheritance() {
1027 String js = ""
1028 + "/** @interface */ function I() {};\n"
1029 + "I.prototype.a;\n"
1030 + "/** @interface */ function I2() {};\n"
1031 + "I2.prototype.a;\n"
1032 + "/** @constructor \n @implements I */ function Foo() {};\n"
1033 + "Foo.prototype.a;\n"
1034 + "/** @constructor \n @extends Foo \n @implements I2*/\n"
1035 + "function Bar() {};\n"
1036 + "Bar.prototype.a;\n"
1037 + "/** @type Bar */\n"
1038 + "var B = new Bar;"
1039 + "B.a = 0";
1040 testSets(false, js, "{a=[[Foo.prototype, I.prototype, I2.prototype]]}");
1041 testSets(true, js, "{a=[[Bar.prototype], [Foo.prototype], "
1042 + "[I.prototype], [I2.prototype]]}");
1043 }
1044
1045 public void testInvalidatingInterface() {
1046 String js = ""
1047 + "/** @interface */ function I2() {};\n"
1048 + "I2.prototype.a;\n"
1049 + "/** @constructor */ function Bar() {}\n"
1050 + "/** @type I */\n"
1051 + "var i = new Bar;\n" // Make I invalidating
1052 + "/** @constructor \n @implements I \n @implements I2 */"
1053 + "function Foo() {};\n"
1054 + "/** @override */\n"
1055 + "Foo.prototype.a = 0;\n"
1056 + "(new Foo).a = 0;"
1057 + "/** @interface */ function I() {};\n"
1058 + "I.prototype.a;\n";
1059 testSets(false, js, "{}", TypeValidator.TYPE_MISMATCH_WARNING);
1060 testSets(true, js, "{}", TypeValidator.TYPE_MISMATCH_WARNING);
1061 }
1062
1063 public void testMultipleInterfaces() {
1064 String js = ""
1065 + "/** @interface */ function I() {};\n"
1066 + "/** @interface */ function I2() {};\n"
1067 + "I2.prototype.a;\n"
1068 + "/** @constructor \n @implements I \n @implements I2 */"
1069 + "function Foo() {};\n"
1070 + "/** @override */"
1071 + "Foo.prototype.a = 0;\n"
1072 + "(new Foo).a = 0";
1073 testSets(false, js, "{a=[[Foo.prototype, I2.prototype]]}");
1074 testSets(true, js, "{a=[[Foo.prototype], [I2.prototype]]}");
1075 }
1076
1077 public void testInterfaceWithSupertypeImplementor() {
1078 String js = ""
1079 + "/** @interface */ function C() {}\n"
1080 + "C.prototype.foo = function() {};\n"
1081 + "/** @constructor */ function A (){}\n"
1082 + "A.prototype.foo = function() {};\n"
1083 + "/** @constructor \n @implements {C} \n @extends {A} */\n"
1084 + "function B() {}\n"
1085 + "/** @type {C} */ var b = new B();\n"
1086 + "b.foo();\n";
1087 testSets(false, js, "{foo=[[A.prototype, C.prototype]]}");
1088 testSets(true, js, "{foo=[[A.prototype], [C.prototype]]}");
1089 }
1090
1091 public void testSuperInterface() {
1092 String js = ""
1093 + "/** @interface */ function I() {};\n"
1094 + "I.prototype.a;\n"
1095 + "/** @interface \n @extends I */ function I2() {};\n"
1096 + "/** @constructor \n @implements I2 */"
1097 + "function Foo() {};\n"
1098 + "/** @override */\n"
1099 + "Foo.prototype.a = 0;\n"
1100 + "(new Foo).a = 0";
1101 testSets(false, js, "{a=[[Foo.prototype, I.prototype]]}");
1102 testSets(true, js, "{a=[[Foo.prototype], [I.prototype]]}");
1103 }
1104
1105 public void testInterfaceUnionWithCtor() {
1106 String js = ""
1107 + "/** @interface */ function I() {};\n"
1108 + "/** @type {!Function} */ I.prototype.addEventListener;\n"
1109 + "/** @constructor \n * @implements {I} */ function Impl() {};\n"
1110 + "/** @type {!Function} */ Impl.prototype.addEventListener;"
1111 + "/** @constructor */ function C() {};\n"
1112 + "/** @type {!Function} */ C.prototype.addEventListener;"
1113 + "/** @param {C|I} x */"
1114 + "function f(x) { x.addEventListener(); };\n"
1115 + "f(new C()); f(new Impl());";
1116
1117 testSets(false, js, js,
1118 "{addEventListener=[[C.prototype, I.prototype, Impl.prototype]]}");
1119
1120 // In the tightened case, the disambiguator is smart enough to
1121 // disambiguate Impl's method from the interface method.
1122 String tightenedOutput = ""
1123 + "function I() {};\n"
1124 + "I.prototype.I_prototype$addEventListener;\n"
1125 + "function Impl() {};\n"
1126 + "Impl.prototype.C_prototype$addEventListener;"
1127 + "function C() {};\n"
1128 + "C.prototype.C_prototype$addEventListener;"
1129 + "/** @param {C|I} x */"
1130 + "function f(x) { x.C_prototype$addEventListener(); };\n"
1131 + "f(new C()); f(new Impl());";
1132
1133 testSets(true, js, tightenedOutput,
1134 "{addEventListener=[[C.prototype, Impl.prototype], [I.prototype]]}");
1135 }
1136
1137 public void testExternInterfaceUnionWithCtor() {
1138 String externs = ""
1139 + "/** @interface */ function I() {};\n"
1140 + "/** @type {!Function} */ I.prototype.addEventListener;\n"
1141 + "/** @constructor \n * @implements {I} */ function Impl() {};\n"
1142 + "/** @type {!Function} */ Impl.prototype.addEventListener;";
1143
1144 String js = ""
1145 + "/** @constructor */ function C() {};\n"
1146 + "/** @type {!Function} */ C.prototype.addEventListener;"
1147 + "/** @param {C|I} x */"
1148 + "function f(x) { x.addEventListener(); };\n"
1149 + "f(new C()); f(new Impl());";
1150
1151 testSets(false, externs, js, js, "{}");
1152 testSets(true, externs, js, js, "{}");
1153 }
1154
1155 /**
1156 * Tests that the type based version skips renaming on types that have a
1157 * mismatch, and the type tightened version continues to work as normal.
1158 */
1159 public void testMismatchInvalidation() {
1160 String js = ""
1161 + "/** @constructor */ function Foo() {}\n"
1162 + "Foo.prototype.a = 0;\n"
1163 + "/** @constructor */ function Bar() {}\n"
1164 + "Bar.prototype.a = 0;\n"
1165 + "/** @type Foo */\n"
1166 + "var F = new Bar;\n"
1167 + "F.a = 0;";
1168
1169 testSets(false, "", js, js, "{}", TypeValidator.TYPE_MISMATCH_WARNING,
1170 "initializing variable\n"
1171 + "found : Bar\n"
1172 + "required: (Foo|null)");
1173 testSets(true, "", js, js, "{}", TypeValidator.TYPE_MISMATCH_WARNING,
1174 "initializing variable\n"
1175 + "found : Bar\n"
1176 + "required: (Foo|null)");
1177 }
1178
1179 public void testBadCast() {
1180 String js = "/** @constructor */ function Foo() {};\n"
1181 + "Foo.prototype.a = 0;\n"
1182 + "/** @constructor */ function Bar() {};\n"
1183 + "Bar.prototype.a = 0;\n"
1184 + "var a = /** @type {!Foo} */ (new Bar);\n"
1185 + "a.a = 4;";
1186 testSets(false, "", js, js, "{}",
1187 TypeValidator.INVALID_CAST,
1188 "invalid cast - must be a subtype or supertype\n"
1189 + "from: Bar\n"
1190 + "to : Foo");
1191 }
1192
1193 public void testDeterministicNaming() {
1194 String js =
1195 "/** @constructor */function A() {}\n"
1196 + "/** @return {string} */A.prototype.f = function() {return 'a';};\n"
1197 + "/** @constructor */function B() {}\n"
1198 + "/** @return {string} */B.prototype.f = function() {return 'b';};\n"
1199 + "/** @constructor */function C() {}\n"
1200 + "/** @return {string} */C.prototype.f = function() {return 'c';};\n"
1201 + "/** @type {A|B} */var ab = 1 ? new B : new A;\n"
1202 + "/** @type {string} */var n = ab.f();\n";
1203
1204 String output =
1205 "function A() {}\n"
1206 + "A.prototype.A_prototype$f = function() { return'a'; };\n"
1207 + "function B() {}\n"
1208 + "B.prototype.A_prototype$f = function() { return'b'; };\n"
1209 + "function C() {}\n"
1210 + "C.prototype.C_prototype$f = function() { return'c'; };\n"
1211 + "var ab = 1 ? new B : new A; var n = ab.A_prototype$f();\n";
1212
1213 for (int i = 0; i < 5; i++) {
1214 testSets(false, js, output,
1215 "{f=[[A.prototype, B.prototype], [C.prototype]]}");
1216
1217 testSets(true, js, output,
1218 "{f=[[A.prototype, B.prototype], [C.prototype]]}");
1219 }
1220 }
1221
1222 public void testObjectLiteral() {
1223 String js = "/** @constructor */ function Foo() {}\n"
1224 + "Foo.prototype.a;\n"
1225 + "/** @constructor */ function Bar() {}\n"
1226 + "Bar.prototype.a;\n"
1227 + "var F = /** @type {Foo} */({ a: 'a' });\n";
1228
1229 String output = "function Foo() {}\n"
1230 + "Foo.prototype.Foo_prototype$a;\n"
1231 + "function Bar() {}\n"
1232 + "Bar.prototype.Bar_prototype$a;\n"
1233 + "var F = { Foo_prototype$a: 'a' };\n";
1234
1235 testSets(false, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
1236 testSets(true, js, output, "{a=[[Bar.prototype], [Foo.prototype]]}");
1237 }
1238
1239 public void testCustomInherits() {
1240 String js = "Object.prototype.inheritsFrom = function(shuper) {\n" +
1241 " /** @constructor */\n" +
1242 " function Inheriter() { }\n" +
1243 " Inheriter.prototype = shuper.prototype;\n" +
1244 " this.prototype = new Inheriter();\n" +
1245 " this.superConstructor = shuper;\n" +
1246 "};\n" +
1247 "function Foo(var1, var2, strength) {\n" +
1248 " Foo.superConstructor.call(this, strength);\n" +
1249 "}" +
1250 "Foo.inheritsFrom(Object);";
1251
1252 String externs = "" +
1253 "function Function(var_args) {}" +
1254 "/** @return {*} */Function.prototype.call = function(var_args) {};";
1255
1256 testSets(false, externs, js, js, "{}");
1257 }
1258
1259 public void testSkipNativeFunctionStaticProperty() {
1260 String js = ""
1261 + "/** @param {!Function} ctor */\n"
1262 + "function addSingletonGetter(ctor) { ctor.a; }\n"
1263 + "/** @constructor */ function Foo() {}\n"
1264 + "Foo.a = 0;"
1265 + "/** @constructor */ function Bar() {}\n"
1266 + "Bar.a = 0;";
1267
1268 String output = ""
1269 + "function addSingletonGetter(ctor){ctor.a}"
1270 + "function Foo(){}"
1271 + "Foo.a=0;"
1272 + "function Bar(){}"
1273 + "Bar.a=0";
1274
1275 testSets(false, js, output, "{}");
1276 }
1277
1278 public void testErrorOnProtectedProperty() {
1279 test("function addSingletonGetter(foo) { foo.foobar = 'a'; };", null,
1280 DisambiguateProperties.Warnings.INVALIDATION);
1281 assertTrue(getLastCompiler().getErrors()[0].toString().contains("foobar"));
1282 }
1283
1284 public void testMismatchForbiddenInvalidation() {
1285 test("/** @constructor */ function F() {}" +
1286 "/** @type {number} */ F.prototype.foobar = 3;" +
1287 "/** @return {number} */ function g() { return new F(); }",
1288 null,
1289 DisambiguateProperties.Warnings.INVALIDATION);
1290 assertTrue(getLastCompiler().getErrors()[0].toString()
1291 .contains("Consider fixing errors"));
1292 }
1293
1294 public void testUnionTypeInvalidationError() {
1295 String externs = ""
1296 + "/** @constructor */ function Baz() {}"
1297 + "Baz.prototype.foobar";
1298 String js = ""
1299 + "/** @constructor */ function Ind() {this.foobar=0}\n"
1300 + "/** @constructor */ function Foo() {}\n"
1301 + "Foo.prototype.foobar = 0;\n"
1302 + "/** @constructor */ function Bar() {}\n"
1303 + "Bar.prototype.foobar = 0;\n"
1304 + "/** @type {Foo|Bar} */\n"
1305 + "var F = new Foo;\n"
1306 + "F.foobar = 1\n;"
1307 + "F = new Bar;\n"
1308 + "/** @type {Baz} */\n"
1309 + "var Z = new Baz;\n"
1310 + "Z.foobar = 1\n;";
1311
1312 test(
1313 externs, js, "",
1314 DisambiguateProperties.Warnings.INVALIDATION_ON_TYPE, null);
1315 assertTrue(getLastCompiler().getErrors()[0].toString()
1316 .contains("foobar"));
1317 }
1318
1319 public void runFindHighestTypeInChain() {
1320 // Check that this doesn't go into an infinite loop.
1321 DisambiguateProperties.forJSTypeSystem(new Compiler(),
1322 Maps.<String, CheckLevel>newHashMap())
1323 .getTypeWithProperty("no",
1324 new JSTypeRegistry(new TestErrorReporter(null, null))
1325 .getNativeType(JSTypeNative.OBJECT_PROTOTYPE));
1326 }
1327
1328 @SuppressWarnings("unchecked")
1329 private void testSets(boolean runTightenTypes, String js, String expected,
1330 String fieldTypes) {
1331 this.runTightenTypes = runTightenTypes;
1332 test(js, expected);
1333 assertEquals(
1334 fieldTypes, mapToString(lastPass.getRenamedTypesForTesting()));
1335 }
1336
1337 @SuppressWarnings("unchecked")
1338 private void testSets(boolean runTightenTypes, String externs, String js,
1339 String expected, String fieldTypes) {
1340 testSets(runTightenTypes, externs, js, expected, fieldTypes, null, null);
1341 }
1342
1343 @SuppressWarnings("unchecked")
1344 private void testSets(boolean runTightenTypes, String externs, String js,
1345 String expected, String fieldTypes, DiagnosticType warning,
1346 String description) {
1347 this.runTightenTypes = runTightenTypes;
1348 test(externs, js, expected, null, warning, description);
1349 assertEquals(
1350 fieldTypes, mapToString(lastPass.getRenamedTypesForTesting()));
1351 }
1352
1353 /**
1354 * Compiles the code and checks that the set of types for each field matches
1355 * the expected value.
1356 *
1357 * <p>The format for the set of types for fields is:
1358 * {field=[[Type1, Type2]]}
1359 */
1360 private void testSets(boolean runTightenTypes, String js, String fieldTypes) {
1361 this.runTightenTypes = runTightenTypes;
1362 test(js, null, null, null);
1363 assertEquals(fieldTypes, mapToString(lastPass.getRenamedTypesForTesting()));
1364 }
1365
1366 /**
1367 * Compiles the code and checks that the set of types for each field matches
1368 * the expected value.
1369 *
1370 * <p>The format for the set of types for fields is:
1371 * {field=[[Type1, Type2]]}
1372 */
1373 private void testSets(boolean runTightenTypes, String js, String fieldTypes,
1374 DiagnosticType warning) {
1375 this.runTightenTypes = runTightenTypes;
1376 test(js, null, null, warning);
1377 assertEquals(fieldTypes, mapToString(lastPass.getRenamedTypesForTesting()));
1378 }
1379
1380 /** Sorts the map and converts to a string for comparison purposes. */
1381 private <T> String mapToString(Multimap<String, Collection<T>> map) {
1382 TreeMap<String, String> retMap = Maps.newTreeMap();
1383 for (String key : map.keySet()) {
1384 TreeSet<String> treeSet = Sets.newTreeSet();
1385 for (Collection<T> collection : map.get(key)) {
1386 Set<String> subSet = Sets.newTreeSet();
1387 for (T type : collection) {
1388 subSet.add(type.toString());
1389 }
1390 treeSet.add(subSet.toString());
1391 }
1392 retMap.put(key, treeSet.toString());
1393 }
1394 return retMap.toString();
1395 }
1396 }