Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2006 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.javascript.rhino.Node;
20
21 /**
22 * Tests for {@link NameAnalyzer}
23 *
24 */
25 public class NameAnalyzerTest extends CompilerTestCase {
26
27 private static String kExterns =
28 "var window, top;" +
29 "var document;" +
30 "var Function;" +
31 "var Array;" +
32 "var externfoo; methods.externfoo;";
33
34 public NameAnalyzerTest() {
35 super(kExterns);
36 }
37
38 @Override
39 protected void setUp() {
40 super.enableNormalize();
41 super.enableLineNumberCheck(true);
42 }
43
44 @Override
45 protected int getNumRepetitions() {
46 // pass reaches steady state after 1 iteration.
47 return 1;
48 }
49
50 public void testRemoveVarDeclaration1() {
51 test("var foo = 3;", "");
52 }
53
54 public void testRemoveVarDeclaration2() {
55 test("var foo = 3, bar = 4; externfoo = foo;",
56 "var foo = 3; externfoo = foo;");
57 }
58
59 public void testRemoveVarDeclaration3() {
60 test("var a = f(), b = 1, c = 2; b; c", "f();var b = 1, c = 2; b; c");
61 }
62
63 public void testRemoveVarDeclaration4() {
64 test("var a = 0, b = f(), c = 2; a; c", "var a = 0;f();var c = 2; a; c");
65 }
66
67 public void testRemoveVarDeclaration5() {
68 test("var a = 0, b = 1, c = f(); a; b", "var a = 0, b = 1; f(); a; b");
69 }
70
71 public void testRemoveVarDeclaration6() {
72 test("var a = 0, b = a = 1; a", "var a = 0; a = 1; a");
73 }
74
75 public void testRemoveVarDeclaration7() {
76 test("var a = 0, b = a = 1", "");
77 }
78
79 public void testRemoveVarDeclaration8() {
80 test("var a;var b = 0, c = a = b = 1", "");
81 }
82
83
84 public void testRemoveDeclaration1() {
85 test("var a;var b = 0, c = a = b = 1", "");
86 }
87
88 public void testRemoveDeclaration2() {
89 test("var a,b,c; c = a = b = 1", "");
90 }
91
92 public void testRemoveDeclaration3() {
93 test("var a,b,c; c = a = b = {}; a.x = 1;", "");
94 }
95
96 public void testRemoveDeclaration4() {
97 testSame("var a,b,c; c = a = b = {}; a.x = 1;alert(c.x);");
98 }
99
100 public void testRemoveDeclaration5() {
101 test("var a,b,c; c = a = b = null; use(b)", "var b;b=null;use(b)");
102 }
103
104 public void testRemoveDeclaration6() {
105 test("var a,b,c; c = a = b = 'str';use(b)", "var b;b='str';use(b)");
106 }
107
108 public void testRemoveDeclaration7() {
109 test("var a,b,c; c = a = b = true;use(b)", "var b;b=true;use(b)");
110 }
111
112 public void testRemoveFunction1() {
113 test("var foo = function(){};", "");
114 }
115
116 public void testRemoveFunction2() {
117 test("var foo; foo = function(){};", "");
118 }
119
120 public void testRemoveFunction3() {
121 test("var foo = {}; foo.bar = function() {};", "");
122 }
123
124 public void testRemoveFunction4() {
125 test("var a = {}; a.b = {}; a.b.c = function() {};", "");
126 }
127
128 public void testReferredToByWindow() {
129 testSame("var foo = {}; foo.bar = function() {}; window['fooz'] = foo.bar");
130 }
131
132 public void testExtern() {
133 testSame("externfoo = 5");
134 }
135
136 public void testRemoveNamedFunction() {
137 test("function foo(){}", "");
138 }
139
140 public void testRemoveRecursiveFunction1() {
141 test("function f(){f()}", "");
142 }
143
144 public void testRemoveRecursiveFunction2() {
145 test("var f = function (){f()}", "");
146 }
147
148 public void testRemoveRecursiveFunction2a() {
149 test("var f = function g(){g()}", "");
150 }
151
152 public void testRemoveRecursiveFunction3() {
153 test("var f;f = function (){f()}", "");
154 }
155
156 public void testRemoveRecursiveFunction4() {
157 // don't removed if name definition doesn't exist.
158 testSame("f = function (){f()}");
159 }
160
161 public void testRemoveRecursiveFunction5() {
162 test("function g(){f()}function f(){g()}", "");
163 }
164
165 public void testRemoveRecursiveFunction6() {
166 test("var f=function(){g()};function g(){f()}", "");
167 }
168
169 public void testRemoveRecursiveFunction7() {
170 test("var g = function(){f()};var f = function(){g()}", "");
171 }
172
173 public void testRemoveRecursiveFunction8() {
174 test("var o = {};o.f = function(){o.f()}", "");
175 }
176
177 public void testRemoveRecursiveFunction9() {
178 testSame("var o = {};o.f = function(){o.f()};o.f()");
179 }
180
181 public void testSideEffectClassification1() {
182 test("foo();", "foo();");
183 }
184
185 public void testSideEffectClassification2() {
186 test("var a = foo();", "foo();");
187 }
188
189 public void testSideEffectClassification3() {
190 testSame("var a = foo();window['b']=a;");
191 }
192
193 public void testSideEffectClassification4() {
194 testSame("function sef(){} sef();");
195 }
196
197 public void testSideEffectClassification5() {
198 testSame("function nsef(){} var a = nsef();window['b']=a;");
199 }
200
201 public void testSideEffectClassification6() {
202 test("function sef(){} sef();", "function sef(){} sef();");
203 }
204
205 public void testSideEffectClassification7() {
206 testSame("function sef(){} var a = sef();window['b']=a;");
207 }
208
209 public void testNoSideEffectAnnotation1() {
210 test("function f(){} var a = f();",
211 "function f(){} f()");
212 }
213
214 public void testNoSideEffectAnnotation2() {
215 test("/**@nosideeffects*/function f(){}", "var a = f();",
216 "", null, null);
217 }
218
219 public void testNoSideEffectAnnotation3() {
220 test("var f = function(){}; var a = f();",
221 "var f = function(){}; f();");
222 }
223
224 public void testNoSideEffectAnnotation4() {
225 test("var f = /**@nosideeffects*/function(){};", "var a = f();",
226 "", null, null);
227 }
228
229 public void testNoSideEffectAnnotation5() {
230 test("var f; f = function(){}; var a = f();",
231 "var f; f = function(){}; f();");
232 }
233
234 public void testNoSideEffectAnnotation6() {
235 test("var f; f = /**@nosideeffects*/function(){};", "var a = f();",
236 "", null, null);
237 }
238
239 public void testNoSideEffectAnnotation7() {
240 test("var f;" +
241 "f = /**@nosideeffects*/function(){};",
242 "f = function(){};" +
243 "var a = f();",
244 "f = function(){}; f();", null, null);
245 }
246
247 public void testNoSideEffectAnnotation8() {
248 test("var f;" +
249 "f = function(){};" +
250 "f = /**@nosideeffects*/function(){};",
251 "var a = f();",
252 "f();", null, null);
253 }
254
255 public void testNoSideEffectAnnotation9() {
256 test("var f;" +
257 "f = /**@nosideeffects*/function(){};" +
258 "f = /**@nosideeffects*/function(){};",
259 "var a = f();",
260 "", null, null);
261
262 test("var f; f = /**@nosideeffects*/function(){};", "var a = f();",
263 "", null, null);
264 }
265
266 public void testNoSideEffectAnnotation10() {
267 test("var o = {}; o.f = function(){}; var a = o.f();",
268 "var o = {}; o.f = function(){}; o.f();");
269 }
270
271 public void testNoSideEffectAnnotation11() {
272 test("var o = {}; o.f = /**@nosideeffects*/function(){};",
273 "var a = o.f();", "", null, null);
274 }
275
276 public void testNoSideEffectAnnotation12() {
277 test("function c(){} var a = new c",
278 "function c(){} new c");
279 }
280
281 public void testNoSideEffectAnnotation13() {
282 test("/**@nosideeffects*/function c(){}", "var a = new c",
283 "", null, null);
284 }
285
286 public void testNoSideEffectAnnotation14() {
287 String common = "function c(){};" +
288 "c.prototype.f = /**@nosideeffects*/function(){};";
289 test(common, "var o = new c; var a = o.f()", "new c", null, null);
290 }
291
292 public void testNoSideEffectAnnotation15() {
293 test("function c(){}; c.prototype.f = function(){}; var a = (new c).f()",
294 "function c(){}; c.prototype.f = function(){}; (new c).f()");
295 }
296
297 public void testNoSideEffectAnnotation16() {
298 test("/**@nosideeffects*/function c(){}" +
299 "c.prototype.f = /**@nosideeffects*/function(){};",
300 "var a = (new c).f()",
301 "",
302 null, null);
303 }
304
305 public void testFunctionPrototype() {
306 testSame("var a = 5; Function.prototype.foo = function() {return a;}");
307 }
308
309 public void testTopLevelClass1() {
310 test("var Point = function() {}; Point.prototype.foo = function() {}", "");
311 }
312
313 public void testTopLevelClass2() {
314 testSame("var Point = {}; Point.prototype.foo = function() {};" +
315 "externfoo = new Point()");
316 }
317
318 public void testTopLevelClass3() {
319 test("function Point() {this.me_ = Point}", "");
320 }
321
322 public void testTopLevelClass4() {
323 test("function f(){} function A(){} A.prototype = {x: function() {}}; f();",
324 "function f(){} f();");
325 }
326
327 public void testTopLevelClass5() {
328 testSame("function f(){} function A(){}" +
329 "A.prototype = {x: function() { f(); }}; new A();");
330 }
331
332 public void testTopLevelClass6() {
333 testSame("function f(){} function A(){}" +
334 "A.prototype = {x: function() { f(); }}; new A().x();");
335 }
336
337 public void testTopLevelClass7() {
338 test("A.prototype.foo = function(){}; function A() {}", "");
339 }
340
341 public void testNamespacedClass1() {
342 test("var foo = {};foo.bar = {};foo.bar.prototype.baz = {}", "");
343 }
344
345 public void testNamespacedClass2() {
346 testSame("var foo = {};foo.bar = {};foo.bar.prototype.baz = {};" +
347 "window.z = new foo.bar()");
348 }
349
350 public void testNamespacedClass3() {
351 test("var a = {}; a.b = function() {}; a.b.prototype = {x: function() {}};",
352 "");
353 }
354
355 public void testNamespacedClass4() {
356 testSame("function f(){} var a = {}; a.b = function() {};" +
357 "a.b.prototype = {x: function() { f(); }}; new a.b();");
358 }
359
360 public void testNamespacedClass5() {
361 testSame("function f(){} var a = {}; a.b = function() {};" +
362 "a.b.prototype = {x: function() { f(); }}; new a.b().x();");
363 }
364
365 public void testAssignmentToThisPrototype() {
366 testSame("Function.prototype.inherits = function(parentCtor) {" +
367 " function tempCtor() {};" +
368 " tempCtor.prototype = parentCtor.prototype;" +
369 " this.superClass_ = parentCtor.prototype;" +
370 " this.prototype = new tempCtor();" +
371 " this.prototype.constructor = this;" +
372 "};");
373 }
374
375 public void testAssignmentToCallResultPrototype() {
376 testSame("function f() { return function(){}; } f().prototype = {};");
377 }
378
379 public void testAssignmentToExternPrototype() {
380 testSame("externfoo.prototype = {};");
381 }
382
383 public void testAssignmentToUnknownPrototype() {
384 testSame(
385 "/** @suppress {duplicate} */ var window;" +
386 "window['a'].prototype = {};");
387 }
388
389 public void testBug2099540() {
390 testSame(
391 "/** @suppress {duplicate} */ var document;\n" +
392 "/** @suppress {duplicate} */ var window;\n" +
393 "var klass;\n" +
394 "window[klass].prototype = " +
395 "document.createElement(tagName)['__proto__'];");
396 }
397
398 public void testOtherGlobal() {
399 testSame("goog.global.foo = bar(); function bar(){}");
400 }
401
402 public void testExternName1() {
403 testSame("top.z = bar(); function bar(){}");
404 }
405
406 public void testExternName2() {
407 testSame("top['z'] = bar(); function bar(){}");
408 }
409
410 public void testInherits1() {
411 test("var a = {}; var b = {}; b.inherits(a)", "");
412 }
413
414 public void testInherits2() {
415 test("var a = {}; var b = {}; var goog = {}; goog.inherits(b, a)", "");
416 }
417
418 public void testInherits3() {
419 testSame("var a = {}; this.b = {}; b.inherits(a);");
420 }
421
422 public void testInherits4() {
423 testSame("var a = {}; this.b = {}; var goog = {}; goog.inherits(b, a);");
424 }
425
426 public void testInherits5() {
427 test("this.a = {}; var b = {}; b.inherits(a);",
428 "this.a = {}");
429 }
430
431 public void testInherits6() {
432 test("this.a = {}; var b = {}; var goog = {}; goog.inherits(b, a);",
433 "this.a = {}");
434 }
435
436 public void testInherits7() {
437 testSame("var a = {}; this.b = {}; var goog = {};" +
438 " goog.inherits = function() {}; goog.inherits(b, a);");
439 }
440
441 public void testInherits8() {
442 // Make sure that exceptions aren't thrown if inherits() is used as
443 // an R-value
444 test("this.a = {}; var b = {}; var c = b.inherits(a);", "this.a = {};");
445 }
446
447 public void testMixin1() {
448 testSame("var goog = {}; goog.mixin = function() {};" +
449 "Function.prototype.mixin = function(base) {" +
450 " goog.mixin(this.prototype, base); " +
451 "};");
452 }
453
454 public void testMixin2() {
455 testSame("var a = {}; this.b = {}; var goog = {};" +
456 " goog.mixin = function() {}; goog.mixin(b.prototype, a.prototype);");
457 }
458
459 public void testMixin3() {
460 test("this.a = {}; var b = {}; var goog = {};" +
461 " goog.mixin = function() {}; goog.mixin(b.prototype, a.prototype);",
462 "this.a = {};");
463 }
464
465 public void testMixin4() {
466 testSame("this.a = {}; var b = {}; var goog = {};" +
467 "goog.mixin = function() {};" +
468 "goog.mixin(b.prototype, a.prototype);" +
469 "new b()");
470 }
471
472 public void testMixin5() {
473 test("this.a = {}; var b = {}; var c = {}; var goog = {};" +
474 "goog.mixin = function() {};" +
475 "goog.mixin(b.prototype, a.prototype);" +
476 "goog.mixin(c.prototype, a.prototype);" +
477 "new b()",
478 "this.a = {}; var b = {}; var goog = {};" +
479 "goog.mixin = function() {};" +
480 "goog.mixin(b.prototype, a.prototype);" +
481 "new b()");
482 }
483
484 public void testMixin6() {
485 testSame("this.a = {}; var b = {}; var c = {}; var goog = {};" +
486 "goog.mixin = function() {};" +
487 "goog.mixin(c.prototype, a.prototype) + " +
488 "goog.mixin(b.prototype, a.prototype);" +
489 "new b()");
490 }
491
492 public void testMixin7() {
493 test("this.a = {}; var b = {}; var c = {}; var goog = {};" +
494 "goog.mixin = function() {};" +
495 "var d = goog.mixin(c.prototype, a.prototype) + " +
496 "goog.mixin(b.prototype, a.prototype);" +
497 "new b()",
498 "this.a = {}; var b = {}; var goog = {};" +
499 "goog.mixin = function() {};" +
500 "goog.mixin(b.prototype, a.prototype);" +
501 "new b()");
502 }
503
504 public void testConstants1() {
505 testSame("var bar = function(){}; var EXP_FOO = true; if (EXP_FOO) bar();");
506 }
507
508 public void testConstants2() {
509 test("var bar = function(){}; var EXP_FOO = true; var EXP_BAR = true;" +
510 "if (EXP_FOO) bar();",
511 "var bar = function(){}; var EXP_FOO = true; if (EXP_FOO) bar();");
512 }
513
514 public void testExpressions1() {
515 test("var foo={}; foo.A='A'; foo.AB=foo.A+'B'; foo.ABC=foo.AB+'C'",
516 "");
517 }
518
519 public void testExpressions2() {
520 testSame("var foo={}; foo.A='A'; foo.AB=foo.A+'B'; this.ABC=foo.AB+'C'");
521 }
522
523 public void testExpressions3() {
524 testSame("var foo = 2; window.bar(foo + 3)");
525 }
526
527 public void testSetCreatingReference() {
528 testSame("var foo; var bar = function(){foo=6;}; bar();");
529 }
530
531 public void testAnonymous1() {
532 testSame("function foo() {}; function bar() {}; foo(function() {bar()})");
533 }
534
535 public void testAnonymous2() {
536 test("var foo;(function(){foo=6;})()", "(function(){})()");
537 }
538
539 public void testAnonymous3() {
540 testSame("var foo; (function(){ if(!foo)foo=6; })()");
541 }
542
543 public void testAnonymous4() {
544 testSame("var foo; (function(){ foo=6; })(); externfoo=foo;");
545 }
546
547 public void testAnonymous5() {
548 testSame("var foo;" +
549 "(function(){ foo=function(){ bar() }; function bar(){} })();" +
550 "foo();");
551 }
552
553 public void testAnonymous6() {
554 testSame("function foo(){}" +
555 "function bar(){}" +
556 "foo(function(){externfoo = bar});");
557 }
558
559 public void testAnonymous7() {
560 testSame("var foo;" +
561 "(function (){ function bar(){ externfoo = foo; } bar(); })();");
562 }
563
564 public void testAnonymous8() {
565 testSame("var foo;" +
566 "(function (){ var g=function(){ externfoo = foo; }; g(); })();");
567 }
568
569 public void testAnonymous9() {
570 testSame("function foo(){}" +
571 "function bar(){}" +
572 "foo(function(){ function baz(){ externfoo = bar; } baz(); });");
573 }
574
575 public void testFunctions1() {
576 testSame("var foo = null; function baz() {}" +
577 "function bar() {foo=baz();} bar();");
578 }
579
580 public void testFunctions2() {
581 testSame("var foo; foo = function() {var a = bar()};" +
582 "var bar = function(){}; foo();");
583 }
584
585 public void testGetElem1() {
586 testSame("var foo = {}; foo.bar = {}; foo.bar.baz = {a: 5, b: 10};" +
587 "var fn = function() {window[foo.bar.baz.a] = 5;}; fn()");
588 }
589
590 public void testGetElem2() {
591 testSame("var foo = {}; foo.bar = {}; foo.bar.baz = {a: 5, b: 10};" +
592 "var fn = function() {this[foo.bar.baz.a] = 5;}; fn()");
593 }
594
595 public void testGetElem3() {
596 testSame("var foo = {'i': 0, 'j': 1}; foo['k'] = 2; top.foo = foo;");
597 }
598
599 public void testIf1() {
600 test("var foo = {};if(e)foo.bar=function(){};", "if(e);");
601 }
602
603 public void testIf2() {
604 test("var e = false;var foo = {};if(e)foo.bar=function(){};",
605 "var e = false;if(e);");
606 }
607
608 public void testIf3() {
609 test("var e = false;var foo = {};if(e + 1)foo.bar=function(){};",
610 "var e = false;if(e + 1);");
611 }
612
613 public void testIf4() {
614 test("var e = false, f;var foo = {};if(f=e)foo.bar=function(){};",
615 "var e = false;if(e);");
616 }
617
618 public void testIf4a() {
619 // TODO(johnlenz): fix this.
620 testSame("var e = [], f;if(f=e);f[0] = 1;");
621 }
622
623 public void testIf4b() {
624 // TODO(johnlenz): fix this.
625 test("var e = [], f;if(e=f);f[0] = 1;",
626 "var f;if(f);f[0] = 1;");
627 }
628
629 public void testIf4c() {
630 test("var e = [], f;if(f=e);e[0] = 1;",
631 "var e = [];if(e);e[0] = 1;");
632 }
633
634 public void testIf5() {
635 test("var e = false, f;var foo = {};if(f = e + 1)foo.bar=function(){};",
636 "var e = false;if(e + 1);");
637 }
638
639 public void testIfElse() {
640 test("var foo = {};if(e)foo.bar=function(){};else foo.bar=function(){};",
641 "if(e);else;");
642 }
643
644 public void testWhile() {
645 test("var foo = {};while(e)foo.bar=function(){};", "while(e);");
646 }
647
648 public void testFor() {
649 test("var foo = {};for(e in x)foo.bar=function(){};", "for(e in x);");
650 }
651
652 public void testDo() {
653 test("var cond = false;do {var a = 1} while (cond)",
654 "var cond = false;do {} while (cond)");
655 }
656
657 public void testSetterInForStruct1() {
658 test("var j = 0; for (var i = 1; i = 0; j++);",
659 "var j = 0; for (; 0; j++);");
660 }
661
662 public void testSetterInForStruct2() {
663 test("var Class = function() {}; " +
664 "for (var i = 1; Class.prototype.property_ = 0; i++);",
665 "for (var i = 1; 0; i++);");
666 }
667
668 public void testSetterInForStruct3() {
669 test("var j = 0; for (var i = 1 + f() + g() + h(); i = 0; j++);",
670 "var j = 0; f(); g(); h(); for (; 0; j++);");
671 }
672
673 public void testSetterInForStruct4() {
674 test("var i = 0;var j = 0; for (i = 1 + f() + g() + h(); i = 0; j++);",
675 "var j = 0; f(); g(); h(); for (; 0; j++);");
676 }
677
678 public void testSetterInForStruct5() {
679 test("var i = 0, j = 0; for (i = f(), j = g(); 0;);",
680 "for (f(), g(); 0;);");
681 }
682
683 public void testSetterInForStruct6() {
684 test("var i = 0, j = 0, k = 0; for (i = f(), j = g(), k = h(); i = 0;);",
685 "for (f(), g(), h(); 0;);");
686 }
687
688 public void testSetterInForStruct7() {
689 test("var i = 0, j = 0, k = 0; for (i = 1, j = 2, k = 3; i = 0;);",
690 "for (1, 2, 3; 0;);");
691 }
692
693 public void testSetterInForStruct8() {
694 test("var i = 0, j = 0, k = 0; for (i = 1, j = i, k = 2; i = 0;);",
695 "var i = 0; for(i = 1, i , 2; i = 0;);");
696 }
697
698 public void testSetterInForStruct9() {
699 test("var Class = function() {}; " +
700 "for (var i = 1; Class.property_ = 0; i++);",
701 "for (var i = 1; 0; i++);");
702 }
703
704 public void testSetterInForStruct10() {
705 test("var Class = function() {}; " +
706 "for (var i = 1; Class.property_ = 0; i = 2);",
707 "for (; 0;);");
708 }
709
710 public void testSetterInForStruct11() {
711 test("var Class = function() {}; " +
712 "for (;Class.property_ = 0;);",
713 "for (;0;);");
714 }
715
716 public void testSetterInForStruct12() {
717 test("var a = 1; var Class = function() {}; " +
718 "for (;Class.property_ = a;);",
719 "var a = 1; for (; a;);");
720 }
721
722 public void testSetterInForStruct13() {
723 test("var a = 1; var Class = function() {}; " +
724 "for (Class.property_ = a; 0 ;);",
725 "for (; 0;);");
726 }
727
728 public void testSetterInForStruct14() {
729 test("var a = 1; var Class = function() {}; " +
730 "for (; 0; Class.property_ = a);",
731 "for (; 0;);");
732 }
733
734 public void testSetterInForStruct15() {
735 test("var Class = function() {}; " +
736 "for (var i = 1; 0; Class.prototype.property_ = 0);",
737 "for (; 0; 0);");
738 }
739
740 public void testSetterInForStruct16() {
741 test("var Class = function() {}; " +
742 "for (var i = 1; i = 0; Class.prototype.property_ = 0);",
743 "for (; 0; 0);");
744 }
745
746 public void testSetterInForIn1() {
747 test("var foo = {}; var bar; for(e in bar = foo.a);",
748 "var foo = {}; for(e in foo.a);");
749 }
750
751 public void testSetterInForIn2() {
752 testSame("var foo = {}; var bar; for(e in bar = foo.a); bar");
753 }
754
755 public void testSetterInForIn3() {
756 testSame("var foo = {}; var bar; for(e in bar = foo.a); bar.b = 3");
757 }
758
759 public void testSetterInForIn4() {
760 testSame("var foo = {}; var bar; for (e in bar = foo.a); bar.b = 3; foo.a");
761 }
762
763 public void testSetterInForIn5() {
764 // TODO(user) Fix issue similar to b/2316773: bar should be preserved
765 // but isn't due to missing references between e and foo.a
766 test("var foo = {}; var bar; for (e in foo.a) { bar = e } bar.b = 3; foo.a",
767 "var foo={};for(e in foo.a);foo.a");
768 }
769
770 public void testSetterInForIn6() {
771 testSame("var foo = {};for(e in foo);");
772 }
773
774 public void testSetterInIfPredicate() {
775 // TODO(user) Make NameAnalyzer smarter so it can remove "Class".
776 testSame("var a = 1;" +
777 "var Class = function() {}; " +
778 "if (Class.property_ = a);");
779 }
780
781 public void testSetterInWhilePredicate() {
782 test("var a = 1;" +
783 "var Class = function() {}; " +
784 "while (Class.property_ = a);",
785 "var a = 1; for (;a;) {}");
786 }
787
788 public void testSetterInDoWhilePredicate() {
789 // TODO(user) Make NameAnalyzer smarter so it can remove "Class".
790 testSame("var a = 1;" +
791 "var Class = function() {}; " +
792 "do {} while(Class.property_ = a);");
793 }
794
795 public void testSetterInSwitchInput() {
796 // TODO(user) Make NameAnalyzer smarter so it can remove "Class".
797 testSame("var a = 1;" +
798 "var Class = function() {}; " +
799 "switch (Class.property_ = a) {" +
800 " default:" +
801 "}");
802 }
803
804 public void testComplexAssigns() {
805 // Complex assigns are not removed by the current pass.
806 testSame("var x = 0; x += 3; x *= 5;");
807 }
808
809 public void testNestedAssigns1() {
810 test("var x = 0; var y = x = 3; window.alert(y);",
811 "var y = 3; window.alert(y);");
812 }
813
814 public void testNestedAssigns2() {
815 testSame("var x = 0; var y = x = {}; x.b = 3; window.alert(y);");
816 }
817
818 public void testComplexNestedAssigns1() {
819 // TODO(nicksantos): Make NameAnalyzer smarter, so that we can eliminate y.
820 testSame("var x = 0; var y = 2; y += x = 3; window.alert(x);");
821 }
822
823 public void testComplexNestedAssigns2() {
824 test("var x = 0; var y = 2; y += x = 3; window.alert(y);",
825 "var y = 2; y += 3; window.alert(y);");
826 }
827
828 public void testComplexNestedAssigns3() {
829 test("var x = 0; var y = x += 3; window.alert(x);",
830 "var x = 0; x += 3; window.alert(x);");
831 }
832
833 public void testComplexNestedAssigns4() {
834 testSame("var x = 0; var y = x += 3; window.alert(y);");
835 }
836
837 public void testUnintendedUseOfInheritsInLocalScope1() {
838 testSame("goog.mixin = function() {}; " +
839 "(function() { var x = {}; var y = {}; goog.mixin(x, y); })();");
840 }
841
842 public void testUnintendedUseOfInheritsInLocalScope2() {
843 testSame("goog.mixin = function() {}; " +
844 "var x = {}; var y = {}; (function() { goog.mixin(x, y); })();");
845 }
846
847 public void testUnintendedUseOfInheritsInLocalScope3() {
848 testSame("goog.mixin = function() {}; " +
849 "var x = {}; var y = {}; (function() { goog.mixin(x, y); })(); " +
850 "window.alert(x);");
851 }
852
853 public void testUnintendedUseOfInheritsInLocalScope4() {
854 // Ensures that the "goog$mixin" variable doesn't get stripped out,
855 // even when it's only used in a local scope.
856 testSame("var goog$mixin = function() {}; " +
857 "(function() { var x = {}; var y = {}; goog$mixin(x, y); })();");
858 }
859
860 public void testPrototypePropertySetInLocalScope1() {
861 testSame("(function() { var x = function(){}; x.prototype.bar = 3; })();");
862 }
863
864 public void testPrototypePropertySetInLocalScope2() {
865 testSame("var x = function(){}; (function() { x.prototype.bar = 3; })();");
866 }
867
868 public void testPrototypePropertySetInLocalScope3() {
869 test("var x = function(){ x.prototype.bar = 3; };", "");
870 }
871
872 public void testPrototypePropertySetInLocalScope4() {
873 test("var x = {}; x.foo = function(){ x.foo.prototype.bar = 3; };", "");
874 }
875
876 public void testPrototypePropertySetInLocalScope5() {
877 test("var x = {}; x.prototype.foo = 3;", "");
878 }
879
880 public void testPrototypePropertySetInLocalScope6() {
881 testSame("var x = {}; x.prototype.foo = 3; bar(x.prototype.foo)");
882 }
883
884 public void testPrototypePropertySetInLocalScope7() {
885 testSame("var x = {}; x.foo = 3; bar(x.foo)");
886 }
887
888 public void testRValueReference1() {
889 testSame("var a = 1; a");
890 }
891
892 public void testRValueReference2() {
893 testSame("var a = 1; 1+a");
894 }
895
896 public void testRValueReference3() {
897 testSame("var x = {}; x.prototype.foo = 3; var a = x.prototype.foo; 1+a");
898 }
899
900 public void testRValueReference4() {
901 testSame("var x = {}; x.prototype.foo = 3; x.prototype.foo");
902 }
903
904 public void testRValueReference5() {
905 testSame("var x = {}; x.prototype.foo = 3; 1+x.prototype.foo");
906 }
907
908 public void testRValueReference6() {
909 testSame("var x = {}; var idx = 2; x[idx]");
910 }
911
912 public void testUnhandledTopNode() {
913 testSame("function Foo() {}; Foo.prototype.isBar = function() {};" +
914 "function Bar() {}; Bar.prototype.isFoo = function() {};" +
915 "var foo = new Foo(); var bar = new Bar();" +
916 // The boolean AND here is currently unhandled by this pass, but
917 // it should not cause it to blow up.
918 "var cond = foo.isBar() && bar.isFoo();" +
919 "if (cond) {window.alert('hello');}");
920 }
921
922 public void testPropertyDefinedInGlobalScope() {
923 testSame("function Foo() {}; var x = new Foo(); x.cssClass = 'bar';" +
924 "window.alert(x);");
925 }
926
927 public void testConditionallyDefinedFunction1() {
928 testSame("var g; externfoo.x || (externfoo.x = function() { g; })");
929 }
930
931 public void testConditionallyDefinedFunction2() {
932 testSame("var g; 1 || (externfoo.x = function() { g; })");
933 }
934
935 public void testConditionallyDefinedFunction3() {
936 testSame("var a = {};" +
937 "rand() % 2 || (a.f = function() { externfoo = 1; } || alert());");
938 }
939
940 public void testGetElemOnThis() {
941 testSame("var a = 3; this['foo'] = a;");
942 testSame("this['foo'] = 3;");
943 }
944
945 public void testRemoveInstanceOfOnly() {
946 test("function Foo() {}; Foo.prototype.isBar = function() {};" +
947 "var x; if (x instanceof Foo) { window.alert(x); }",
948 ";var x; if (false) { window.alert(x); }");
949 }
950
951 public void testRemoveLocalScopedInstanceOfOnly() {
952 test("function Foo() {}; function Bar(x) { this.z = x instanceof Foo; };" +
953 "externfoo.x = new Bar({});",
954 ";function Bar(x) { this.z = false }; externfoo.x = new Bar({});");
955 }
956
957 public void testRemoveInstanceOfWithReferencedMethod() {
958 test("function Foo() {}; Foo.prototype.isBar = function() {};" +
959 "var x; if (x instanceof Foo) { window.alert(x.isBar()); }",
960 ";var x; if (false) { window.alert(x.isBar()); }");
961 }
962
963 public void testDoNotChangeReferencedInstanceOf() {
964 testSame("function Foo() {}; Foo.prototype.isBar = function() {};" +
965 "var x = new Foo(); if (x instanceof Foo) { window.alert(x); }");
966 }
967
968 public void testDoNotChangeReferencedLocalScopedInstanceOf() {
969 testSame("function Foo() {}; externfoo.x = new Foo();" +
970 "function Bar() { if (x instanceof Foo) { window.alert(x); } };" +
971 "externfoo.y = new Bar();");
972 }
973
974 public void testDoNotChangeLocalScopeReferencedInstanceOf() {
975 testSame("function Foo() {}; Foo.prototype.isBar = function() {};" +
976 "function Bar() { this.z = new Foo(); }; externfoo.x = new Bar();" +
977 "if (x instanceof Foo) { window.alert(x); }");
978 }
979
980 public void testDoNotChangeLocalScopeReferencedLocalScopedInstanceOf() {
981 testSame("function Foo() {}; Foo.prototype.isBar = function() {};" +
982 "function Bar() { this.z = new Foo(); };" +
983 "Bar.prototype.func = function(x) {" +
984 "if (x instanceof Foo) { window.alert(x); }" +
985 "}; new Bar().func();");
986 }
987
988 public void testDoNotChangeLocalScopeReferencedLocalScopedInstanceOf2() {
989 test(
990 "function Foo() {}" +
991 "var createAxis = function(f) { return window.passThru(f); };" +
992 "var axis = createAxis(function(test) {" +
993 " return test instanceof Foo;" +
994 "});",
995 "var createAxis = function(f) { return window.passThru(f); };" +
996 "createAxis(function(test) {" +
997 " return false;" +
998 "});");
999 }
1000
1001 public void testDoNotChangeInstanceOfGetElem() {
1002 testSame("var goog = {};" +
1003 "function f(obj, name) {" +
1004 " if (obj instanceof goog[name]) {" +
1005 " return name;" +
1006 " }" +
1007 "}" +
1008 "window['f'] = f;");
1009 }
1010
1011 public void testWeirdnessOnLeftSideOfPrototype() {
1012 // This checks a bug where 'x' was removed, but the function referencing
1013 // it was not, causing problems.
1014 testSame("var x = 3; " +
1015 "(function() { this.bar = 3; }).z = function() {" +
1016 " return x;" +
1017 "};");
1018 }
1019
1020 public void testShortCircuit1() {
1021 test("var a = b() || 1", "b()");
1022 }
1023
1024 public void testShortCircuit2() {
1025 test("var a = 1 || c()", "1 || c()");
1026 }
1027
1028 public void testShortCircuit3() {
1029 test("var a = b() || c()", "b() || c()");
1030 }
1031
1032 public void testShortCircuit4() {
1033 test("var a = b() || 3 || c()", "b() || 3 || c()");
1034 }
1035
1036 public void testShortCircuit5() {
1037 test("var a = b() && 1", "b()");
1038 }
1039
1040 public void testShortCircuit6() {
1041 test("var a = 1 && c()", "1 && c()");
1042 }
1043
1044 public void testShortCircuit7() {
1045 test("var a = b() && c()", "b() && c()");
1046 }
1047
1048 public void testShortCircuit8() {
1049 test("var a = b() && 3 && c()", "b() && 3 && c()");
1050 }
1051
1052 public void testRhsReference1() {
1053 testSame("var a = 1; a");
1054 }
1055
1056 public void testRhsReference2() {
1057 testSame("var a = 1; a || b()");
1058 }
1059
1060 public void testRhsReference3() {
1061 testSame("var a = 1; 1 || a");
1062 }
1063
1064 public void testRhsReference4() {
1065 test("var a = 1; var b = a || foo()", "var a = 1; a || foo()");
1066 }
1067
1068 public void testRhsReference5() {
1069 test("var a = 1, b = 5; a; foo(b)", "var a = 1, b = 5; a; foo(b)");
1070 }
1071
1072 public void testRhsAssign1() {
1073 test("var foo, bar; foo || (bar = 1)",
1074 "var foo; foo || 1");
1075 }
1076
1077 public void testRhsAssign2() {
1078 test("var foo, bar, baz; foo || (baz = bar = 1)",
1079 "var foo; foo || 1");
1080 }
1081
1082 public void testRhsAssign3() {
1083 testSame("var foo = null; foo || (foo = 1)");
1084 }
1085
1086 public void testRhsAssign4() {
1087 test("var foo = null; foo = (foo || 1)", "");
1088 }
1089
1090 public void testRhsAssign5() {
1091 test("var a = 3, foo, bar; foo || (bar = a)", "var a = 3, foo; foo || a");
1092 }
1093
1094 public void testRhsAssign6() {
1095 test("function Foo(){} var foo = null;" +
1096 "var f = function () {foo || (foo = new Foo()); return foo}",
1097 "");
1098 }
1099
1100 public void testRhsAssign7() {
1101 testSame("function Foo(){} var foo = null;" +
1102 "var f = function () {foo || (foo = new Foo())}; f()");
1103 }
1104
1105 public void testRhsAssign8() {
1106 testSame("function Foo(){} var foo = null;" +
1107 "var f = function () {(foo = new Foo()) || g()}; f()");
1108 }
1109
1110 public void testRhsAssign9() {
1111 test("function Foo(){} var foo = null;" +
1112 "var f = function () {1 + (foo = new Foo()); return foo}",
1113 "");
1114 }
1115
1116 public void testAssignWithOr1() {
1117 testSame("var foo = null;" +
1118 "var f = window.a || function () {return foo}; f()");
1119 }
1120
1121 public void testAssignWithOr2() {
1122 test("var foo = null;" +
1123 "var f = window.a || function () {return foo};",
1124 "var foo = null"); // why is this left?
1125 }
1126
1127 public void testAssignWithAnd1() {
1128 testSame("var foo = null;" +
1129 "var f = window.a && function () {return foo}; f()");
1130 }
1131
1132 public void testAssignWithAnd2() {
1133 test("var foo = null;" +
1134 "var f = window.a && function () {return foo};",
1135 "var foo = null;"); // why is this left?
1136 }
1137
1138 public void testAssignWithHook1() {
1139 testSame("function Foo(){} var foo = null;" +
1140 "var f = window.a ? " +
1141 " function () {return new Foo()} : function () {return foo}; f()");
1142 }
1143
1144 public void testAssignWithHook2() {
1145 test("function Foo(){} var foo = null;" +
1146 "var f = window.a ? " +
1147 " function () {return new Foo()} : function () {return foo};",
1148 "");
1149 }
1150
1151 public void testAssignWithHook2a() {
1152 test("function Foo(){} var foo = null;" +
1153 "var f; f = window.a ? " +
1154 " function () {return new Foo()} : function () {return foo};",
1155 "");
1156 }
1157
1158 public void testAssignWithHook3() {
1159 testSame("function Foo(){} var foo = null; var f = {};" +
1160 "f.b = window.a ? " +
1161 " function () {return new Foo()} : function () {return foo}; f.b()");
1162 }
1163
1164 public void testAssignWithHook4() {
1165 test("function Foo(){} var foo = null; var f = {};" +
1166 "f.b = window.a ? " +
1167 " function () {return new Foo()} : function () {return foo};",
1168 "");
1169 }
1170
1171 public void testAssignWithHook5() {
1172 testSame("function Foo(){} var foo = null; var f = {};" +
1173 "f.b = window.a ? function () {return new Foo()} :" +
1174 " window.b ? function () {return foo} :" +
1175 " function() { return Foo }; f.b()");
1176 }
1177
1178 public void testAssignWithHook6() {
1179 test("function Foo(){} var foo = null; var f = {};" +
1180 "f.b = window.a ? function () {return new Foo()} :" +
1181 " window.b ? function () {return foo} :" +
1182 " function() { return Foo };",
1183 "");
1184 }
1185
1186 public void testAssignWithHook7() {
1187 testSame("function Foo(){} var foo = null;" +
1188 "var f = window.a ? new Foo() : foo;" +
1189 "f()");
1190 }
1191
1192 public void testAssignWithHook8() {
1193 test("function Foo(){} var foo = null;" +
1194 "var f = window.a ? new Foo() : foo;",
1195 "function Foo(){}" +
1196 "window.a && new Foo()");
1197 }
1198
1199 public void testAssignWithHook9() {
1200 test("function Foo(){} var foo = null; var f = {};" +
1201 "f.b = window.a ? new Foo() : foo;",
1202 "function Foo(){} window.a && new Foo()");
1203 }
1204
1205 public void testAssign1() {
1206 test("function Foo(){} var foo = null; var f = {};" +
1207 "f.b = window.a;",
1208 "");
1209 }
1210
1211 public void testAssign2() {
1212 test("function Foo(){} var foo = null; var f = {};" +
1213 "f.b = window;",
1214 "");
1215 }
1216
1217 public void testAssign3() {
1218 test("var f = {};" +
1219 "f.b = window;",
1220 "");
1221 }
1222
1223 public void testAssign4() {
1224 test("function Foo(){} var foo = null; var f = {};" +
1225 "f.b = new Foo();",
1226 "function Foo(){} new Foo()");
1227 }
1228
1229 public void testAssign5() {
1230 test("function Foo(){} var foo = null; var f = {};" +
1231 "f.b = foo;",
1232 "");
1233 }
1234
1235 public void testNestedAssign1() {
1236 test("var a, b = a = 1, c = 2", "");
1237 }
1238
1239 public void testNestedAssign2() {
1240 test("var a, b = a = 1; foo(b)",
1241 "var b = 1; foo(b)");
1242 }
1243
1244 public void testNestedAssign3() {
1245 test("var a, b = a = 1; a = b = 2; foo(b)",
1246 "var b = 1; b = 2; foo(b)");
1247 }
1248
1249 public void testNestedAssign4() {
1250 test("var a, b = a = 1; b = a = 2; foo(b)",
1251 "var b = 1; b = 2; foo(b)");
1252 }
1253
1254 public void testNestedAssign5() {
1255 test("var a, b = a = 1; b = a = 2", "");
1256 }
1257
1258 public void testNestedAssign15() {
1259 test("var a, b, c; c = b = a = 2", "");
1260 }
1261
1262 public void testNestedAssign6() {
1263 testSame("var a, b, c; a = b = c = 1; foo(a, b, c)");
1264 }
1265
1266 public void testNestedAssign7() {
1267 testSame("var a = 0; a = i[j] = 1; b(a, i[j])");
1268 }
1269
1270 public void testNestedAssign8() {
1271 testSame("function f(){" +
1272 "this.lockedToken_ = this.lastToken_ = " +
1273 "SETPROP_value(this.hiddenInput_, a)}f()");
1274 }
1275
1276 public void testRefChain1() {
1277 test("var a = 1; var b = a; var c = b; var d = c", "");
1278 }
1279
1280 public void testRefChain2() {
1281 test("var a = 1; var b = a; var c = b; var d = c || f()",
1282 "var a = 1; var b = a; var c = b; c || f()");
1283 }
1284
1285 public void testRefChain3() {
1286 test("var a = 1; var b = a; var c = b; var d = c + f()", "f()");
1287 }
1288
1289 public void testRefChain4() {
1290 test("var a = 1; var b = a; var c = b; var d = f() || c",
1291 "f()");
1292 }
1293
1294 public void testRefChain5() {
1295 test("var a = 1; var b = a; var c = b; var d = f() ? g() : c",
1296 "f() && g()");
1297 }
1298
1299 public void testRefChain6() {
1300 test("var a = 1; var b = a; var c = b; var d = c ? f() : g()",
1301 "var a = 1; var b = a; var c = b; c ? f() : g()");
1302 }
1303
1304 public void testRefChain7() {
1305 test("var a = 1; var b = a; var c = b; var d = (b + f()) ? g() : c",
1306 "var a = 1; var b = a; (b+f()) && g()");
1307 }
1308
1309 public void testRefChain8() {
1310 test("var a = 1; var b = a; var c = b; var d = f()[b] ? g() : 0",
1311 "var a = 1; var b = a; f()[b] && g()");
1312 }
1313
1314 public void testRefChain9() {
1315 test("var a = 1; var b = a; var c = 5; var d = f()[b+c] ? g() : 0",
1316 "var a = 1; var b = a; var c = 5; f()[b+c] && g()");
1317 }
1318
1319 public void testRefChain10() {
1320 test("var a = 1; var b = a; var c = b; var d = f()[b] ? g() : 0",
1321 "var a = 1; var b = a; f()[b] && g()");
1322 }
1323
1324 public void testRefChain11() {
1325 test("var a = 1; var b = a; var d = f()[b] ? g() : 0",
1326 "var a = 1; var b = a; f()[b] && g()");
1327 }
1328
1329 public void testRefChain12() {
1330 testSame("var a = 1; var b = a; f()[b] ? g() : 0");
1331 }
1332
1333
1334 public void testRefChain13() {
1335 test("function f(){}var a = 1; var b = a; var d = f()[b] ? g() : 0",
1336 "function f(){}var a = 1; var b = a; f()[b] && g()");
1337 }
1338
1339 public void testRefChain14() {
1340 testSame("function f(){}var a = 1; var b = a; f()[b] ? g() : 0");
1341 }
1342
1343 public void testRefChain15() {
1344 test("function f(){}var a = 1, b = a; var c = f(); var d = c[b] ? g() : 0",
1345 "function f(){}var a = 1, b = a; var c = f(); c[b] && g()");
1346 }
1347
1348 public void testRefChain16() {
1349 testSame("function f(){}var a = 1; var b = a; var c = f(); c[b] ? g() : 0");
1350 }
1351
1352 public void testRefChain17() {
1353 test("function f(){}var a = 1; var b = a; var c = f(); var d = c[b]",
1354 "function f(){} f()");
1355 }
1356
1357 public void testRefChain18() {
1358 testSame("var a = 1; f()[a] && g()");
1359 }
1360
1361
1362 public void testRefChain19() {
1363 test("var a = 1; var b = [a]; var c = b; b[f()] ? g() : 0",
1364 "var a=1; var b=[a]; b[f()] ? g() : 0");
1365 }
1366
1367 public void testRefChain20() {
1368 test("var a = 1; var b = [a]; var c = b; var d = b[f()] ? g() : 0",
1369 "var a=1; var b=[a]; b[f()]&&g()");
1370 }
1371
1372 public void testRefChain21() {
1373 testSame("var a = 1; var b = 2; var c = a + b; f(c)");
1374 }
1375
1376 public void testRefChain22() {
1377 test("var a = 2; var b = a = 4; f(a)", "var a = 2; a = 4; f(a)");
1378 }
1379
1380 public void testRefChain23() {
1381 test("var a = {}; var b = a[1] || f()", "var a = {}; a[1] || f()");
1382 }
1383
1384 /**
1385 * Expressions that cannot be attributed to any enclosing dependency
1386 * scope should be treated as global references.
1387 * @bug 1739062
1388 */
1389 public void testAssignmentWithComplexLhs() {
1390 testSame("function f() { return this; }" +
1391 "var o = {'key': 'val'};" +
1392 "f().x_ = o['key'];");
1393 }
1394
1395 public void testAssignmentWithComplexLhs2() {
1396 testSame("function f() { return this; }" +
1397 "var o = {'key': 'val'};" +
1398 "f().foo = function() {" +
1399 " o" +
1400 "};");
1401 }
1402
1403 public void testAssignmentWithComplexLhs3() {
1404 String source =
1405 "var o = {'key': 'val'};" +
1406 "function init_() {" +
1407 " this.x = o['key']" +
1408 "}";
1409
1410 test(source, "");
1411 testSame(source + ";init_()");
1412 }
1413
1414 public void testAssignmentWithComplexLhs4() {
1415 testSame("function f() { return this; }" +
1416 "var o = {'key': 'val'};" +
1417 "f().foo = function() {" +
1418 " this.x = o['key']" +
1419 "};");
1420 }
1421
1422 /**
1423 * Do not "prototype" property of variables that are not being
1424 * tracked (because they are local).
1425 * @bug 1809442
1426 */
1427 public void testNoRemovePrototypeDefinitionsOutsideGlobalScope1() {
1428 testSame("function f(arg){}" +
1429 "" +
1430 "(function(){" +
1431 " var O = {};" +
1432 " O.prototype = 'foo';" +
1433 " f(O);" +
1434 "})()");
1435 }
1436
1437 public void testNoRemovePrototypeDefinitionsOutsideGlobalScope2() {
1438 testSame("function f(arg){}" +
1439 "(function h(){" +
1440 " var L = {};" +
1441 " L.prototype = 'foo';" +
1442 " f(L);" +
1443 "})()");
1444 }
1445
1446 public void testNoRemovePrototypeDefinitionsOutsideGlobalScope4() {
1447 testSame("function f(arg){}" +
1448 "function g(){" +
1449 " var N = {};" +
1450 " N.prototype = 'foo';" +
1451 " f(N);" +
1452 "}" +
1453 "g()");
1454 }
1455
1456 public void testNoRemovePrototypeDefinitionsOutsideGlobalScope5() {
1457 // function body not removed due to @bug 1898561
1458 testSame("function g(){ var R = {}; R.prototype = 'foo' } g()");
1459 }
1460
1461 public void testRemovePrototypeDefinitionsInGlobalScope1() {
1462 testSame("function f(arg){}" +
1463 "var M = {};" +
1464 "M.prototype = 'foo';" +
1465 "f(M);");
1466 }
1467
1468 public void testRemovePrototypeDefinitionsInGlobalScope2() {
1469 test("var Q = {}; Q.prototype = 'foo'", "");
1470 }
1471
1472 public void testRemoveLabeledStatment() {
1473 test("LBL: var x = 1;", "LBL: {}");
1474 }
1475
1476 public void testRemoveLabeledStatment2() {
1477 test("var x; LBL: x = f() + g()", "LBL: { f() ; g()}");
1478 }
1479
1480 public void testRemoveLabeledStatment3() {
1481 test("var x; LBL: x = 1;", "LBL: {}");
1482 }
1483
1484 public void testRemoveLabeledStatment4() {
1485 test("var a; LBL: a = f()", "LBL: f()");
1486 }
1487
1488 public void testPreservePropertyMutationsToAlias1() {
1489 // Test for issue b/2316773 - property get case
1490 // Since a is referenced, property mutations via a's alias b must
1491 // be preserved.
1492 testSame("var a = {}; var b = a; b.x = 1; a");
1493 }
1494
1495 public void testPreservePropertyMutationsToAlias2() {
1496 // Test for issue b/2316773 - property get case, don't keep 'c'
1497 test("var a = {}; var b = a; var c = a; b.x = 1; a",
1498 "var a = {}; var b = a; b.x = 1; a");
1499 }
1500
1501 public void testPreservePropertyMutationsToAlias3() {
1502 // Test for issue b/2316773 - property get case, chain
1503 testSame("var a = {}; var b = a; var c = b; c.x = 1; a");
1504 }
1505
1506 public void testPreservePropertyMutationsToAlias4() {
1507 // Test for issue b/2316773 - element get case
1508 testSame("var a = {}; var b = a; b['x'] = 1; a");
1509 }
1510
1511 public void testPreservePropertyMutationsToAlias5() {
1512 // From issue b/2316773 description
1513 testSame("function testCall(o){}" +
1514 "var DATA = {'prop': 'foo','attr': {}};" +
1515 "var SUBDATA = DATA['attr'];" +
1516 "SUBDATA['subprop'] = 'bar';" +
1517 "testCall(DATA);");
1518 }
1519
1520 public void testPreservePropertyMutationsToAlias6() {
1521 // Longer GETELEM chain
1522 testSame("function testCall(o){}" +
1523 "var DATA = {'prop': 'foo','attr': {}};" +
1524 "var SUBDATA = DATA['attr'];" +
1525 "var SUBSUBDATA = SUBDATA['subprop'];" +
1526 "SUBSUBDATA['subsubprop'] = 'bar';" +
1527 "testCall(DATA);");
1528 }
1529
1530 public void testPreservePropertyMutationsToAlias7() {
1531 // Make sure that the base class does not depend on the derived class.
1532 test("var a = {}; var b = {}; b.x = 0;" +
1533 "var goog = {}; goog.inherits(b, a); a",
1534 "var a = {}; a");
1535 }
1536
1537 public void testPreservePropertyMutationsToAlias8() {
1538 // Make sure that the derived classes don't end up depending on each other.
1539 test("var a = {};" +
1540 "var b = {}; b.x = 0;" +
1541 "var c = {}; c.y = 0;" +
1542 "var goog = {}; goog.inherits(b, a); goog.inherits(c, a); c",
1543 "var a = {}; var c = {}; c.y = 0;" +
1544 "var goog = {}; goog.inherits(c, a); c");
1545 }
1546
1547 public void testPreservePropertyMutationsToAlias9() {
1548 testSame("var a = {b: {}};" +
1549 "var c = a.b; c.d = 3;" +
1550 "a.d = 3; a.d;");
1551 }
1552
1553 public void testRemoveAlias() {
1554 test("var a = {b: {}};" +
1555 "var c = a.b;" +
1556 "a.d = 3; a.d;",
1557 "var a = {b: {}}; a.d = 3; a.d;");
1558 }
1559
1560 public void testSingletonGetter1() {
1561 test("function Foo() {} goog.addSingletonGetter(Foo);", "");
1562 }
1563
1564 public void testSingletonGetter2() {
1565 test("function Foo() {} goog$addSingletonGetter(Foo);", "");
1566 }
1567
1568 public void testSingletonGetter3() {
1569 // addSingletonGetter adds a getInstance method to a class.
1570 testSame("function Foo() {} goog$addSingletonGetter(Foo);" +
1571 "this.x = Foo.getInstance();");
1572 }
1573
1574
1575
1576 public void testNoRemoveWindowPropertyAlias1() {
1577 testSame(
1578 "var self_ = window.gbar;\n" +
1579 "self_.qs = function() {};");
1580 }
1581
1582 public void testNoRemoveWindowPropertyAlias2() {
1583 testSame(
1584 "var self_ = window;\n" +
1585 "self_.qs = function() {};");
1586 }
1587
1588 public void testNoRemoveWindowPropertyAlias3() {
1589 testSame(
1590 "var self_ = window;\n" +
1591 "self_['qs'] = function() {};");
1592 }
1593
1594 public void testNoRemoveWindowPropertyAlias4() {
1595 // TODO(johnlenz): fix this. "self_" should remain.
1596 test(
1597 "var self_ = window['gbar'] || {};\n" +
1598 "self_.qs = function() {};",
1599 "");
1600 }
1601
1602 public void testNoRemoveWindowPropertyAlias4a() {
1603 // TODO(johnlenz): fix this. "self_" should remain.
1604 test(
1605 "var self_; self_ = window.gbar || {};\n" +
1606 "self_.qs = function() {};",
1607 "");
1608 }
1609
1610 public void testNoRemoveWindowPropertyAlias5() {
1611 // TODO(johnlenz): fix this. "self_" should remain.
1612 test(
1613 "var self_ = window || {};\n" +
1614 "self_['qs'] = function() {};",
1615 "");
1616 }
1617
1618 public void testNoRemoveWindowPropertyAlias5a() {
1619 // TODO(johnlenz): fix this.
1620 test(
1621 "var self_; self_ = window || {};\n" +
1622 "self_['qs'] = function() {};",
1623 "");
1624 }
1625
1626 public void testNoRemoveWindowPropertyAlias6() {
1627 testSame(
1628 "var self_ = (window.gbar = window.gbar || {});\n" +
1629 "self_.qs = function() {};");
1630 }
1631
1632 public void testNoRemoveWindowPropertyAlias6a() {
1633 testSame(
1634 "var self_; self_ = (window.gbar = window.gbar || {});\n" +
1635 "self_.qs = function() {};");
1636 }
1637
1638 public void testNoRemoveWindowPropertyAlias7() {
1639 testSame(
1640 "var self_ = (window = window || {});\n" +
1641 "self_['qs'] = function() {};");
1642 }
1643
1644 public void testNoRemoveWindowPropertyAlias7a() {
1645 testSame(
1646 "var self_; self_ = (window = window || {});\n" +
1647 "self_['qs'] = function() {};");
1648 }
1649
1650 public void testNoRemoveAlias0() {
1651 testSame(
1652 "var x = {}; function f() { return x; }; " +
1653 "f().style.display = 'block';" +
1654 "alert(x.style)");
1655 }
1656
1657 public void testNoRemoveAlias1() {
1658 testSame(
1659 "var x = {}; function f() { return x; };" +
1660 "var map = f();\n" +
1661 "map.style.display = 'block';" +
1662 "alert(x.style)");
1663 }
1664
1665 public void testNoRemoveAlias2() {
1666 testSame(
1667 "var x = {};" +
1668 "var map = (function () { return x; })();\n" +
1669 "map.style = 'block';" +
1670 "alert(x.style)");
1671 }
1672
1673 public void testNoRemoveAlias3() {
1674 testSame(
1675 "var x = {}; function f() { return x; };" +
1676 "var map = {}\n" +
1677 "map[1] = f();\n" +
1678 "map[1].style.display = 'block';");
1679 }
1680
1681 public void testNoRemoveAliasOfExternal0() {
1682 testSame(
1683 "document.getElementById('foo').style.display = 'block';");
1684 }
1685
1686 public void testNoRemoveAliasOfExternal1() {
1687 testSame(
1688 "var map = document.getElementById('foo');\n" +
1689 "map.style.display = 'block';");
1690 }
1691
1692 public void testNoRemoveAliasOfExternal2() {
1693 testSame(
1694 "var map = {}\n" +
1695 "map[1] = document.getElementById('foo');\n" +
1696 "map[1].style.display = 'block';");
1697 }
1698
1699 public void testNoRemoveThrowReference1() {
1700 testSame(
1701 "var e = {}\n" +
1702 "throw e;");
1703 }
1704
1705 public void testNoRemoveThrowReference2() {
1706 testSame(
1707 "function e() {}\n" +
1708 "throw new e();");
1709 }
1710
1711 public void testClassDefinedInObjectLit1() {
1712 test(
1713 "var data = {Foo: function() {}};" +
1714 "data.Foo.prototype.toString = function() {};",
1715 "");
1716 }
1717
1718 public void testClassDefinedInObjectLit2() {
1719 test(
1720 "var data = {}; data.bar = {Foo: function() {}};" +
1721 "data.bar.Foo.prototype.toString = function() {};",
1722 "");
1723 }
1724
1725 public void testClassDefinedInObjectLit3() {
1726 test(
1727 "var data = {bar: {Foo: function() {}}};" +
1728 "data.bar.Foo.prototype.toString = function() {};",
1729 "");
1730 }
1731
1732 public void testClassDefinedInObjectLit4() {
1733 test(
1734 "var data = {};" +
1735 "data.baz = {bar: {Foo: function() {}}};" +
1736 "data.baz.bar.Foo.prototype.toString = function() {};",
1737 "");
1738 }
1739
1740 public void testVarReferencedInClassDefinedInObjectLit1() {
1741 testSame(
1742 "var ref = 3;" +
1743 "var data = {Foo: function() { this.x = ref; }};" +
1744 "window.Foo = data.Foo;");
1745 }
1746
1747 public void testVarReferencedInClassDefinedInObjectLit2() {
1748 testSame(
1749 "var ref = 3;" +
1750 "var data = {Foo: function() { this.x = ref; }," +
1751 " Bar: function() {}};" +
1752 "window.Bar = data.Bar;");
1753 }
1754
1755 public void testArrayExt() {
1756 testSame(
1757 "Array.prototype.foo = function() { return 1 };" +
1758 "var y = [];" +
1759 "switch (y.foo()) {" +
1760 "}");
1761 }
1762
1763 public void testArrayAliasExt() {
1764 testSame(
1765 "Array$X = Array;" +
1766 "Array$X.prototype.foo = function() { return 1 };" +
1767 "function Array$X() {}" +
1768 "var y = [];" +
1769 "switch (y.foo()) {" +
1770 "}");
1771 }
1772
1773 public void testExternalAliasInstanceof1() {
1774 test(
1775 "Array$X = Array;" +
1776 "function Array$X() {}" +
1777 "var y = [];" +
1778 "if (y instanceof Array) {}",
1779 "var y = [];" +
1780 "if (y instanceof Array) {}"
1781 );
1782 }
1783
1784 public void testExternalAliasInstanceof2() {
1785 testSame(
1786 "Array$X = Array;" +
1787 "function Array$X() {}" +
1788 "var y = [];" +
1789 "if (y instanceof Array$X) {}");
1790 }
1791
1792 public void testExternalAliasInstanceof3() {
1793 testSame(
1794 "var b = Array;" +
1795 "var y = [];" +
1796 "if (y instanceof b) {}");
1797 }
1798
1799 public void testAliasInstanceof4() {
1800 testSame(
1801 "function Foo() {};" +
1802 "var b = Foo;" +
1803 "var y = new Foo();" +
1804 "if (y instanceof b) {}");
1805 }
1806
1807 public void testAliasInstanceof5() {
1808 // TODO(johnlenz): fix this. "b" should remain.
1809 test(
1810 "function Foo() {}" +
1811 "function Bar() {}" +
1812 "var b = x ? Foo : Bar;" +
1813 "var y = new Foo();" +
1814 "if (y instanceof b) {}",
1815 "function Foo() {}" +
1816 "var y = new Foo;" +
1817 "if (false){}");
1818 }
1819
1820 // We cannot leave x.a.prototype there because it will
1821 // fail sanity var check.
1822 public void testBrokenNamespaceWithPrototypeAssignment() {
1823 test("var x = {}; x.a.prototype = 1", "");
1824 }
1825
1826 public void testRemovePrototypeAliases() {
1827 test(
1828 "function g() {}" +
1829 "function F() {} F.prototype.bar = g;" +
1830 "window.g = g;",
1831 "function g() {}" +
1832 "window.g = g;");
1833 }
1834
1835 public void testIssue284() {
1836 test(
1837 "var goog = {};" +
1838 "goog.inherits = function(x, y) {};" +
1839 "var ns = {};" +
1840 "/** @constructor */" +
1841 "ns.PageSelectionModel = function() {};" +
1842 "/** @constructor */" +
1843 "ns.PageSelectionModel.FooEvent = function() {};" +
1844 "/** @constructor */" +
1845 "ns.PageSelectionModel.SelectEvent = function() {};" +
1846 "goog.inherits(ns.PageSelectionModel.ChangeEvent," +
1847 " ns.PageSelectionModel.FooEvent);",
1848 "");
1849 }
1850
1851 public void testIssue838a() {
1852 testSame("var z = window['z'] || (window['z'] = {});\n" +
1853 "z['hello'] = 'Hello';\n" +
1854 "z['world'] = 'World';");
1855 }
1856
1857 public void testIssue838b() {
1858 testSame(
1859 "var z;" +
1860 "window['z'] = z || (z = {});\n" +
1861 "z['hello'] = 'Hello';\n" +
1862 "z['world'] = 'World';");
1863 }
1864
1865
1866 public void testIssue874a() {
1867 testSame(
1868 "var a = a || {};\n" +
1869 "var b = a;\n" +
1870 "b.View = b.View || {}\n" +
1871 "var c = b.View;\n" +
1872 "c.Editor = function f(d, e) {\n" +
1873 " return d + e\n" +
1874 "};\n" +
1875 "window.ImageEditor.View.Editor = a.View.Editor;");
1876 }
1877
1878 public void testIssue874b() {
1879 testSame(
1880 "var b;\n" +
1881 "var c = b = {};\n" +
1882 "c.Editor = function f(d, e) {\n" +
1883 " return d + e\n" +
1884 "};\n" +
1885 "window['Editor'] = b.Editor;");
1886 }
1887
1888 public void testIssue874c() {
1889 testSame(
1890 "var b, c;\n" +
1891 "c = b = {};\n" +
1892 "c.Editor = function f(d, e) {\n" +
1893 " return d + e\n" +
1894 "};\n" +
1895 "window['Editor'] = b.Editor;");
1896 }
1897
1898 public void testIssue874d() {
1899 testSame(
1900 "var b = {}, c;\n" +
1901 "c = b;\n" +
1902 "c.Editor = function f(d, e) {\n" +
1903 " return d + e\n" +
1904 "};\n" +
1905 "window['Editor'] = b.Editor;");
1906 }
1907
1908 public void testIssue874e() {
1909 testSame(
1910 "var a;\n" +
1911 "var b = a || (a = {});\n" +
1912 "var c = b.View || (b.View = {});\n" +
1913 "c.Editor = function f(d, e) {\n" +
1914 " return d + e\n" +
1915 "};\n" +
1916 "window.ImageEditor.View.Editor = a.View.Editor;");
1917 }
1918
1919 public void testBug6575051() {
1920 testSame(
1921 "var hackhack = window['__o_o_o__'] = window['__o_o_o__'] || {};\n" +
1922 "window['__o_o_o__']['va'] = 1;\n" +
1923 "hackhack['Vb'] = 1;");
1924 }
1925
1926 @Override
1927 protected CompilerPass getProcessor(Compiler compiler) {
1928 return new MarkNoSideEffectCallsAndNameAnalyzerRunner(compiler);
1929 }
1930
1931 private class MarkNoSideEffectCallsAndNameAnalyzerRunner
1932 implements CompilerPass {
1933 MarkNoSideEffectCalls markNoSideEffectCalls;
1934 NameAnalyzer analyzer;
1935 MarkNoSideEffectCallsAndNameAnalyzerRunner(Compiler compiler) {
1936 this.markNoSideEffectCalls = new MarkNoSideEffectCalls(compiler);
1937 this.analyzer = new NameAnalyzer(compiler, true);
1938 }
1939
1940 @Override
1941 public void process(Node externs, Node root) {
1942 markNoSideEffectCalls.process(externs, root);
1943 analyzer.process(externs, root);
1944 }
1945 }
1946 }