Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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 com.google.common.base.Joiner;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.Lists;
22 import com.google.javascript.jscomp.CheckLevel;
23 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
24 import com.google.javascript.rhino.Node;
25 import com.google.javascript.rhino.Token;
26 import com.google.javascript.rhino.jstype.JSType;
27
28 import java.util.List;
29
30 /**
31 * Tests for {@link DevirtualizePrototypeMethods}
32 *
33 */
34 public class DevirtualizePrototypeMethodsTest extends CompilerTestCase {
35 private static final String EXTERNAL_SYMBOLS =
36 "var extern;extern.externalMethod";
37 private final List<String> typeInformation;
38
39 public DevirtualizePrototypeMethodsTest() {
40 super(EXTERNAL_SYMBOLS);
41 typeInformation = Lists.newArrayList();
42 }
43
44 @Override
45 protected int getNumRepetitions() {
46 // run pass once.
47 return 1;
48 }
49
50 @Override
51 protected void setUp() throws Exception {
52 super.setUp();
53 super.enableLineNumberCheck(true);
54 disableTypeCheck();
55 }
56
57 /**
58 * Combine source strings using '\n' as the separator.
59 */
60 private static String newlineJoin(String ... parts) {
61 return Joiner.on("\n").join(parts);
62 }
63
64 /**
65 * Combine source strings using ';' as the separator.
66 */
67 private static String semicolonJoin(String ... parts) {
68 return Joiner.on(";").join(parts);
69 }
70
71 /**
72 * Inputs for prototype method tests.
73 */
74 private static class RewritePrototypeMethodTestInput {
75 static final String INPUT = newlineJoin(
76 "/** @constructor */",
77 "function a(){ this.x = 3; }",
78 "/** @return {number} */",
79 "a.prototype.foo = function() {return this.x};",
80 "/** @param {number} p\n@return {number} */",
81 "a.prototype.bar = function(p) {return this.x};",
82 "a.prototype.baz = function() {};",
83 "var o = new a;",
84 "o.foo();",
85 "o.bar(2);",
86 "o.baz()");
87
88 static final String EXPECTED = newlineJoin(
89 "function a(){ this.x = 3; }",
90 "var JSCompiler_StaticMethods_foo = ",
91 "function(JSCompiler_StaticMethods_foo$self) {",
92 " return JSCompiler_StaticMethods_foo$self.x",
93 "};",
94 "var JSCompiler_StaticMethods_bar = ",
95 "function(JSCompiler_StaticMethods_bar$self, p) {",
96 " return JSCompiler_StaticMethods_bar$self.x",
97 "};",
98 "var JSCompiler_StaticMethods_baz = ",
99 "function(JSCompiler_StaticMethods_baz$self) {",
100 "};",
101 "var o = new a;",
102 "JSCompiler_StaticMethods_foo(o);",
103 "JSCompiler_StaticMethods_bar(o, 2);",
104 "JSCompiler_StaticMethods_baz(o)");
105
106 static final List<String> EXPECTED_TYPE_CHECKING_OFF = ImmutableList.of(
107 "FUNCTION a = null",
108 "NAME JSCompiler_StaticMethods_foo$self = null",
109 "FUNCTION JSCompiler_StaticMethods_foo = null",
110 "NAME JSCompiler_StaticMethods_bar$self = null",
111 "FUNCTION JSCompiler_StaticMethods_bar = null",
112 "FUNCTION JSCompiler_StaticMethods_baz = null",
113 "NEW a = null",
114 "CALL JSCompiler_StaticMethods_foo = null",
115 "CALL JSCompiler_StaticMethods_bar = null",
116 "CALL JSCompiler_StaticMethods_baz = null");
117
118 static final List<String> EXPECTED_TYPE_CHECKING_ON = ImmutableList.of(
119 "FUNCTION a = function (new:a): undefined",
120 "NAME JSCompiler_StaticMethods_foo$self = a",
121 "FUNCTION JSCompiler_StaticMethods_foo = function (a): number",
122 "NAME JSCompiler_StaticMethods_bar$self = a",
123 "FUNCTION JSCompiler_StaticMethods_bar = function (a, number): number",
124 "FUNCTION JSCompiler_StaticMethods_baz = function (a): undefined",
125 "NEW a = a",
126 "CALL JSCompiler_StaticMethods_foo = number",
127 "CALL JSCompiler_StaticMethods_bar = number",
128 "CALL JSCompiler_StaticMethods_baz = undefined");
129
130 private RewritePrototypeMethodTestInput() {}
131 }
132
133 public void testRewritePrototypeMethods1() throws Exception {
134 // type checking off
135 disableTypeCheck();
136 checkTypes(RewritePrototypeMethodTestInput.INPUT,
137 RewritePrototypeMethodTestInput.EXPECTED,
138 RewritePrototypeMethodTestInput.EXPECTED_TYPE_CHECKING_OFF);
139 }
140
141 public void testRewritePrototypeMethods2() throws Exception {
142 // type checking on
143 enableTypeCheck(CheckLevel.ERROR);
144 checkTypes(RewritePrototypeMethodTestInput.INPUT,
145 RewritePrototypeMethodTestInput.EXPECTED,
146 RewritePrototypeMethodTestInput.EXPECTED_TYPE_CHECKING_ON);
147 }
148
149 public void testRewriteChained() throws Exception {
150 String source = newlineJoin(
151 "A.prototype.foo = function(){return this.b};",
152 "B.prototype.bar = function(){};",
153 "o.foo().bar()");
154
155 String expected = newlineJoin(
156 "var JSCompiler_StaticMethods_foo = ",
157 "function(JSCompiler_StaticMethods_foo$self) {",
158 " return JSCompiler_StaticMethods_foo$self.b",
159 "};",
160 "var JSCompiler_StaticMethods_bar = ",
161 "function(JSCompiler_StaticMethods_bar$self) {",
162 "};",
163 "JSCompiler_StaticMethods_bar(JSCompiler_StaticMethods_foo(o))");
164 test(source, expected);
165 }
166
167 /**
168 * Inputs for declaration used as an r-value tests.
169 */
170 private static class NoRewriteDeclarationUsedAsRValue {
171 static final String DECL = "a.prototype.foo = function() {}";
172 static final String CALL = "o.foo()";
173
174 private NoRewriteDeclarationUsedAsRValue() {}
175 }
176
177 public void testRewriteDeclIsExpressionStatement() throws Exception {
178 test(semicolonJoin(NoRewriteDeclarationUsedAsRValue.DECL,
179 NoRewriteDeclarationUsedAsRValue.CALL),
180 "var JSCompiler_StaticMethods_foo =" +
181 "function(JSCompiler_StaticMethods_foo$self) {};" +
182 "JSCompiler_StaticMethods_foo(o)");
183 }
184
185 public void testNoRewriteDeclUsedAsAssignmentRhs() throws Exception {
186 testSame(semicolonJoin("var c = " + NoRewriteDeclarationUsedAsRValue.DECL,
187 NoRewriteDeclarationUsedAsRValue.CALL));
188 }
189
190 public void testNoRewriteDeclUsedAsCallArgument() throws Exception {
191 testSame(semicolonJoin("f(" + NoRewriteDeclarationUsedAsRValue.DECL + ")",
192 NoRewriteDeclarationUsedAsRValue.CALL));
193 }
194
195 /**
196 * Inputs for restrict-to-global-scope tests.
197 */
198 private static class NoRewriteIfNotInGlobalScopeTestInput {
199 static final String INPUT = newlineJoin(
200 "function a(){}",
201 "a.prototype.foo = function() {return this.x};",
202 "var o = new a;",
203 "o.foo()");
204
205 private NoRewriteIfNotInGlobalScopeTestInput() {}
206 }
207
208 public void testRewriteInGlobalScope() throws Exception {
209 String expected = newlineJoin(
210 "function a(){}",
211 "var JSCompiler_StaticMethods_foo = ",
212 "function(JSCompiler_StaticMethods_foo$self) {",
213 " return JSCompiler_StaticMethods_foo$self.x",
214 "};",
215 "var o = new a;",
216 "JSCompiler_StaticMethods_foo(o);");
217
218 test(NoRewriteIfNotInGlobalScopeTestInput.INPUT, expected);
219 }
220
221 public void testNoRewriteIfNotInGlobalScope1() throws Exception {
222 testSame("if(true){" + NoRewriteIfNotInGlobalScopeTestInput.INPUT + "}");
223 }
224
225 public void testNoRewriteIfNotInGlobalScope2() throws Exception {
226 testSame("function enclosingFunction() {" +
227 NoRewriteIfNotInGlobalScopeTestInput.INPUT +
228 "}");
229 }
230
231 public void testNoRewriteNamespaceFunctions() throws Exception {
232 String source = newlineJoin(
233 "function a(){}",
234 "a.foo = function() {return this.x};",
235 "a.foo()");
236 testSame(source);
237 }
238
239 /**
240 * Inputs for multiple definition tests.
241 */
242 private static class NoRewriteMultipleDefinitionTestInput {
243 static final String TEMPLATE = ".prototype.foo = function() {}";
244 static final String SOURCE_A = "a" + TEMPLATE;
245 static final String SOURCE_B = "b" + TEMPLATE;
246 static final String CALL = "o.foo()";
247
248 static final String SINGLE_DEFINITION_EXPECTED =
249 "var JSCompiler_StaticMethods_foo = " +
250 " function(JSCompiler_StaticMethods_foo$self) {};" +
251 "JSCompiler_StaticMethods_foo(o)";
252
253 private NoRewriteMultipleDefinitionTestInput() {}
254 }
255
256 public void testRewriteSingleDefinition1() throws Exception {
257 test(semicolonJoin(NoRewriteMultipleDefinitionTestInput.SOURCE_A,
258 NoRewriteMultipleDefinitionTestInput.CALL),
259 NoRewriteMultipleDefinitionTestInput.SINGLE_DEFINITION_EXPECTED);
260 }
261
262 public void testRewriteSingleDefinition2() throws Exception {
263 test(semicolonJoin(NoRewriteMultipleDefinitionTestInput.SOURCE_B,
264 NoRewriteMultipleDefinitionTestInput.CALL),
265 NoRewriteMultipleDefinitionTestInput.SINGLE_DEFINITION_EXPECTED);
266 }
267
268 public void testNoRewriteMultipleDefinition1() throws Exception {
269 testSame(semicolonJoin(NoRewriteMultipleDefinitionTestInput.SOURCE_A,
270 NoRewriteMultipleDefinitionTestInput.SOURCE_A,
271 NoRewriteMultipleDefinitionTestInput.CALL));
272 }
273
274 public void testNoRewriteMultipleDefinition2() throws Exception {
275 testSame(semicolonJoin(NoRewriteMultipleDefinitionTestInput.SOURCE_B,
276 NoRewriteMultipleDefinitionTestInput.SOURCE_B,
277 NoRewriteMultipleDefinitionTestInput.CALL));
278 }
279
280 public void testNoRewriteMultipleDefinition3() throws Exception {
281 testSame(semicolonJoin(NoRewriteMultipleDefinitionTestInput.SOURCE_A,
282 NoRewriteMultipleDefinitionTestInput.SOURCE_B,
283 NoRewriteMultipleDefinitionTestInput.CALL));
284 }
285
286 /**
287 * Inputs for object literal tests.
288 */
289 private static class NoRewritePrototypeObjectLiteralsTestInput {
290 static final String REGULAR = "b.prototype.foo = function() {}";
291 static final String OBJ_LIT = "a.prototype = {foo : function() {}}";
292 static final String CALL = "o.foo()";
293
294 private NoRewritePrototypeObjectLiteralsTestInput() {}
295 }
296
297 public void testRewritePrototypeNoObjectLiterals() throws Exception {
298 test(semicolonJoin(NoRewritePrototypeObjectLiteralsTestInput.REGULAR,
299 NoRewritePrototypeObjectLiteralsTestInput.CALL),
300 "var JSCompiler_StaticMethods_foo = " +
301 "function(JSCompiler_StaticMethods_foo$self) {};" +
302 "JSCompiler_StaticMethods_foo(o)");
303 }
304
305 public void testRewritePrototypeObjectLiterals1() throws Exception {
306 test(semicolonJoin(NoRewritePrototypeObjectLiteralsTestInput.OBJ_LIT,
307 NoRewritePrototypeObjectLiteralsTestInput.CALL),
308 "a.prototype={};" +
309 "var JSCompiler_StaticMethods_foo=" +
310 "function(JSCompiler_StaticMethods_foo$self){};" +
311 "JSCompiler_StaticMethods_foo(o)");
312 }
313
314 public void testNoRewritePrototypeObjectLiterals2() throws Exception {
315 testSame(semicolonJoin(NoRewritePrototypeObjectLiteralsTestInput.OBJ_LIT,
316 NoRewritePrototypeObjectLiteralsTestInput.REGULAR,
317 NoRewritePrototypeObjectLiteralsTestInput.CALL));
318 }
319
320 public void testNoRewriteExternalMethods1() throws Exception {
321 testSame("a.externalMethod()");
322 }
323
324 public void testNoRewriteExternalMethods2() throws Exception {
325 testSame("A.prototype.externalMethod = function(){}; o.externalMethod()");
326 }
327
328 public void testNoRewriteCodingConvention() throws Exception {
329 // no rename, leading _ indicates exported symbol
330 testSame("a.prototype._foo = function() {};");
331 }
332
333 public void testRewriteNoVarArgs() throws Exception {
334 String source = newlineJoin(
335 "function a(){}",
336 "a.prototype.foo = function(args) {return args};",
337 "var o = new a;",
338 "o.foo()");
339
340 String expected = newlineJoin(
341 "function a(){}",
342 "var JSCompiler_StaticMethods_foo = ",
343 " function(JSCompiler_StaticMethods_foo$self, args) {return args};",
344 "var o = new a;",
345 "JSCompiler_StaticMethods_foo(o)");
346
347 test(source, expected);
348 }
349
350 public void testNoRewriteVarArgs() throws Exception {
351 String source = newlineJoin(
352 "function a(){}",
353 "a.prototype.foo = function(var_args) {return arguments};",
354 "var o = new a;",
355 "o.foo()");
356 testSame(source);
357 }
358
359 /**
360 * Inputs for invalidating reference tests.
361 */
362 private static class NoRewriteNonCallReferenceTestInput {
363 static final String BASE = newlineJoin(
364 "function a(){}",
365 "a.prototype.foo = function() {return this.x};",
366 "var o = new a;");
367
368 private NoRewriteNonCallReferenceTestInput() {}
369 }
370
371 public void testRewriteCallReference() throws Exception {
372 String expected = newlineJoin(
373 "function a(){}",
374 "var JSCompiler_StaticMethods_foo = ",
375 "function(JSCompiler_StaticMethods_foo$self) {",
376 " return JSCompiler_StaticMethods_foo$self.x",
377 "};",
378 "var o = new a;",
379 "JSCompiler_StaticMethods_foo(o);");
380
381 test(NoRewriteNonCallReferenceTestInput.BASE + "o.foo()", expected);
382 }
383
384 public void testNoRewriteNoReferences() throws Exception {
385 testSame(NoRewriteNonCallReferenceTestInput.BASE);
386 }
387
388 public void testNoRewriteNonCallReference() throws Exception {
389 testSame(NoRewriteNonCallReferenceTestInput.BASE + "o.foo && o.foo()");
390 }
391
392 /**
393 * Inputs for nested definition tests.
394 */
395 private static class NoRewriteNestedFunctionTestInput {
396 static final String PREFIX = "a.prototype.foo = function() {";
397 static final String SUFFIX = "o.foo()";
398 static final String INNER = "a.prototype.bar = function() {}; o.bar()";
399 static final String EXPECTED_PREFIX =
400 "var JSCompiler_StaticMethods_foo=" +
401 "function(JSCompiler_StaticMethods_foo$self){";
402 static final String EXPECTED_SUFFIX =
403 "JSCompiler_StaticMethods_foo(o)";
404
405 private NoRewriteNestedFunctionTestInput() {}
406 }
407
408 public void testRewriteNoNestedFunction() throws Exception {
409 test(semicolonJoin(
410 NoRewriteNestedFunctionTestInput.PREFIX + "}",
411 NoRewriteNestedFunctionTestInput.SUFFIX,
412 NoRewriteNestedFunctionTestInput.INNER),
413 semicolonJoin(
414 NoRewriteNestedFunctionTestInput.EXPECTED_PREFIX + "}",
415 NoRewriteNestedFunctionTestInput.EXPECTED_SUFFIX,
416 "var JSCompiler_StaticMethods_bar=" +
417 "function(JSCompiler_StaticMethods_bar$self){}",
418 "JSCompiler_StaticMethods_bar(o)"));
419 }
420
421 public void testNoRewriteNestedFunction() throws Exception {
422 test(NoRewriteNestedFunctionTestInput.PREFIX +
423 NoRewriteNestedFunctionTestInput.INNER + "};" +
424 NoRewriteNestedFunctionTestInput.SUFFIX,
425 NoRewriteNestedFunctionTestInput.EXPECTED_PREFIX +
426 NoRewriteNestedFunctionTestInput.INNER + "};" +
427 NoRewriteNestedFunctionTestInput.EXPECTED_SUFFIX);
428 }
429
430 public void testRewriteImplementedMethod() throws Exception {
431 String source = newlineJoin(
432 "function a(){}",
433 "a.prototype.foo = function(args) {return args};",
434 "var o = new a;",
435 "o.foo()");
436 String expected = newlineJoin(
437 "function a(){}",
438 "var JSCompiler_StaticMethods_foo = ",
439 " function(JSCompiler_StaticMethods_foo$self, args) {return args};",
440 "var o = new a;",
441 "JSCompiler_StaticMethods_foo(o)");
442 test(source, expected);
443 }
444
445 public void testRewriteImplementedMethod2() throws Exception {
446 String source = newlineJoin(
447 "function a(){}",
448 "a.prototype['foo'] = function(args) {return args};",
449 "var o = new a;",
450 "o.foo()");
451 testSame(source);
452 }
453
454 public void testRewriteImplementedMethod3() throws Exception {
455 String source = newlineJoin(
456 "function a(){}",
457 "a.prototype.foo = function(args) {return args};",
458 "var o = new a;",
459 "o['foo']");
460 testSame(source);
461 }
462
463 public void testRewriteImplementedMethod4() throws Exception {
464 String source = newlineJoin(
465 "function a(){}",
466 "a.prototype['foo'] = function(args) {return args};",
467 "var o = new a;",
468 "o['foo']");
469 testSame(source);
470 }
471
472 public void testRewriteImplementedMethodInObj() throws Exception {
473 String source = newlineJoin(
474 "function a(){}",
475 "a.prototype = {foo: function(args) {return args}};",
476 "var o = new a;",
477 "o.foo()");
478 test(source,
479 "function a(){}" +
480 "a.prototype={};" +
481 "var JSCompiler_StaticMethods_foo=" +
482 "function(JSCompiler_StaticMethods_foo$self,args){return args};" +
483 "var o=new a;" +
484 "JSCompiler_StaticMethods_foo(o)");
485 }
486
487 public void testNoRewriteGet1() throws Exception {
488 // Getters and setter require special handling.
489 String source = newlineJoin(
490 "function a(){}",
491 "a.prototype = {get foo(){return f}};",
492 "var o = new a;",
493 "o.foo()");
494 testSame(source);
495 }
496
497 public void testNoRewriteGet2() throws Exception {
498 // Getters and setter require special handling.
499 String source = newlineJoin(
500 "function a(){}",
501 "a.prototype = {get foo(){return 1}};",
502 "var o = new a;",
503 "o.foo");
504 testSame(source);
505 }
506
507 public void testNoRewriteSet1() throws Exception {
508 // Getters and setter require special handling.
509 String source = newlineJoin(
510 "function a(){}",
511 "a.prototype = {set foo(a){}};",
512 "var o = new a;",
513 "o.foo()");
514 testSame(source);
515 }
516
517 public void testNoRewriteSet2() throws Exception {
518 // Getters and setter require special handling.
519 String source = newlineJoin(
520 "function a(){}",
521 "a.prototype = {set foo(a){}};",
522 "var o = new a;",
523 "o.foo = 1");
524 testSame(source);
525 }
526
527 public void testNoRewriteNotImplementedMethod() throws Exception {
528 testSame(newlineJoin("function a(){}",
529 "var o = new a;",
530 "o.foo()"));
531 }
532
533 public void testWrapper() {
534 testSame("(function() {})()");
535 }
536
537 private static class ModuleTestInput {
538 static final String DEFINITION = "a.prototype.foo = function() {}";
539 static final String USE = "x.foo()";
540
541 static final String REWRITTEN_DEFINITION =
542 "var JSCompiler_StaticMethods_foo=" +
543 "function(JSCompiler_StaticMethods_foo$self){}";
544 static final String REWRITTEN_USE =
545 "JSCompiler_StaticMethods_foo(x)";
546
547 private ModuleTestInput() {}
548 }
549
550 public void testRewriteSameModule1() throws Exception {
551 JSModule[] modules = createModuleStar(
552 // m1
553 semicolonJoin(ModuleTestInput.DEFINITION,
554 ModuleTestInput.USE),
555 // m2
556 "");
557
558 test(modules, new String[] {
559 // m1
560 semicolonJoin(ModuleTestInput.REWRITTEN_DEFINITION,
561 ModuleTestInput.REWRITTEN_USE),
562 // m2
563 "",
564 });
565 }
566
567 public void testRewriteSameModule2() throws Exception {
568 JSModule[] modules = createModuleStar(
569 // m1
570 "",
571 // m2
572 semicolonJoin(ModuleTestInput.DEFINITION,
573 ModuleTestInput.USE));
574
575 test(modules, new String[] {
576 // m1
577 "",
578 // m2
579 semicolonJoin(ModuleTestInput.REWRITTEN_DEFINITION,
580 ModuleTestInput.REWRITTEN_USE)
581 });
582 }
583
584 public void testRewriteSameModule3() throws Exception {
585 JSModule[] modules = createModuleStar(
586 // m1
587 semicolonJoin(ModuleTestInput.USE,
588 ModuleTestInput.DEFINITION),
589 // m2
590 "");
591
592 test(modules, new String[] {
593 // m1
594 semicolonJoin(ModuleTestInput.REWRITTEN_USE,
595 ModuleTestInput.REWRITTEN_DEFINITION),
596 // m2
597 ""
598 });
599 }
600
601 public void testRewriteDefinitionBeforeUse() throws Exception {
602 JSModule[] modules = createModuleStar(
603 // m1
604 ModuleTestInput.DEFINITION,
605 // m2
606 ModuleTestInput.USE);
607
608 test(modules, new String[] {
609 // m1
610 ModuleTestInput.REWRITTEN_DEFINITION,
611 // m2
612 ModuleTestInput.REWRITTEN_USE
613 });
614 }
615
616 public void testNoRewriteUseBeforeDefinition() throws Exception {
617 JSModule[] modules = createModuleStar(
618 // m1
619 ModuleTestInput.USE,
620 // m2
621 ModuleTestInput.DEFINITION);
622
623 testSame(modules);
624 }
625
626 /**
627 * Verifies that the compiler pass's output matches the expected
628 * output, and that nodes are annotated with the expected jstype
629 * information.
630 */
631 private void checkTypes(String source,
632 String expected,
633 List<String> expectedTypes) {
634 typeInformation.clear();
635 test(source, expected);
636 assertEquals(expectedTypes, typeInformation);
637 }
638
639 @Override
640 protected CompilerPass getProcessor(Compiler compiler) {
641 return new TypeInformationGatherer(
642 compiler, new DevirtualizePrototypeMethods(compiler), typeInformation);
643 }
644
645 /**
646 * Wrapper that gathers function, call, and self variable type strings after
647 * the pass under test runs. For use to test passes that modify JSType
648 * annotations.
649 */
650 private static class TypeInformationGatherer
651 implements CompilerPass {
652 private final Compiler compiler;
653 private final CompilerPass passUnderTest;
654 private final List<String> typeInformation;
655
656 TypeInformationGatherer(Compiler compiler,
657 CompilerPass passUnderTest,
658 List<String> typeInformation) {
659 this.compiler = compiler;
660 this.passUnderTest = passUnderTest;
661 this.typeInformation = typeInformation;
662 }
663
664 @Override
665 public void process(Node externs, Node root) {
666 passUnderTest.process(externs, root);
667 NodeTraversal.traverse(compiler, externs, new GatherCallback());
668 NodeTraversal.traverse(compiler, root, new GatherCallback());
669 }
670
671 public String getNameString(Node n) {
672 int type = n.getType();
673 if (type == Token.NAME) {
674 return n.getString();
675 } else if (type == Token.GETPROP) {
676 String left = getNameString(n.getFirstChild());
677 if (left == null) {
678 return null;
679 }
680 return left + "." + n.getLastChild().getString();
681 } else if (type == Token.GETELEM) {
682 String left = getNameString(n.getFirstChild());
683 if (left == null) {
684 return null;
685 }
686 return left + "[" + n.getLastChild().getString() + "]";
687 } else if (type == Token.THIS) {
688 return "this";
689 } else if (type == Token.FUNCTION){
690 return "{ANON FUNCTION}";
691 } else {
692 // I wonder if we should just die on this.
693 return null;
694 }
695 }
696
697 private class GatherCallback extends AbstractPostOrderCallback {
698 @Override
699 public void visit(NodeTraversal traversal, Node node, Node parent) {
700 Node nameNode = null;
701 if (node.isFunction()) {
702 if (parent.isName()) {
703 nameNode = parent;
704 } else if (parent.isAssign()) {
705 nameNode = parent.getFirstChild();
706 } else {
707 nameNode = node.getFirstChild();
708 }
709 } else if (node.isCall() || node.isNew()) {
710 nameNode = node.getFirstChild();
711 }
712
713 if (nameNode != null) {
714 JSType type = node.getJSType();
715 typeInformation.add(
716 Joiner.on("").join(
717 Token.name(node.getType()),
718 " ",
719 getNameString(nameNode),
720 " = ",
721 (type != null) ? type.toString() : "null"));
722 }
723
724 if (node.isGetProp()) {
725 Node child = node.getFirstChild();
726 if (child.isName() && child.getString().endsWith("$self")) {
727 JSType type = child.getJSType();
728 typeInformation.add(
729 Joiner.on("").join(
730 Token.name(child.getType()),
731 " ",
732 child.getString(),
733 " = ",
734 (type != null) ? type.toString() : "null"));
735 }
736 }
737 }
738 }
739 }
740 }