Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 /**
20 * Tests for {@link CrossModuleCodeMotion}.
21 *
22 */
23 public class CrossModuleCodeMotionTest extends CompilerTestCase {
24
25 private static final String EXTERNS = "alert";
26
27 public CrossModuleCodeMotionTest() {
28 super(EXTERNS);
29 }
30
31 @Override
32 public void setUp() {
33 super.enableLineNumberCheck(true);
34 }
35
36 @Override
37 public CompilerPass getProcessor(Compiler compiler) {
38 return new CrossModuleCodeMotion(compiler, compiler.getModuleGraph());
39 }
40
41 public void testFunctionMovement1() {
42 // This tests lots of things:
43 // 1) f1 is declared in m1, and used in m2. Move it to m2
44 // 2) f2 is declared in m1, and used in m3 twice. Move it to m3
45 // 3) f3 is declared in m1, and used in m2+m3. It stays put
46 // 4) g declared in m1 and never used. It stays put
47 // 5) h declared in m2 and never used. It stays put
48 // 6) f4 declared in m1 and used in m2 as var. It moves to m2
49
50 JSModule[] modules = createModuleStar(
51 // m1
52 "function f1(a) { alert(a); }" +
53 "function f2(a) { alert(a); }" +
54 "function f3(a) { alert(a); }" +
55 "function f4() { alert(1); }" +
56 "function g() { alert('ciao'); }",
57 // m2
58 "f1('hi'); f3('bye'); var a = f4;" +
59 "function h(a) { alert('h:' + a); }",
60 // m3
61 "f2('hi'); f2('hi'); f3('bye');");
62
63 test(modules, new String[] {
64 // m1
65 "function f3(a) { alert(a); }" +
66 "function g() { alert('ciao'); }",
67 // m2
68 "function f4() { alert(1); }" +
69 "function f1(a) { alert(a); }" +
70 "f1('hi'); f3('bye'); var a = f4;" +
71 "function h(a) { alert('h:' + a); }",
72 // m3
73 "function f2(a) { alert(a); }" +
74 "f2('hi'); f2('hi'); f3('bye');",
75 });
76 }
77
78 public void testFunctionMovement2() {
79 // having f declared as a local variable should block the migration to m2
80 JSModule[] modules = createModuleStar(
81 // m1
82 "function f(a) { alert(a); }" +
83 "function g() {var f = 1; f++}",
84 // m2
85 "f(1);");
86
87 test(modules, new String[] {
88 // m1
89 "function g() {var f = 1; f++}",
90 // m2
91 "function f(a) { alert(a); }" +
92 "f(1);",
93 });
94 }
95
96 public void testFunctionMovement3() {
97 // having f declared as a arg should block the migration to m2
98 JSModule[] modules = createModuleStar(
99 // m1
100 "function f(a) { alert(a); }" +
101 "function g(f) {f++}",
102 // m2
103 "f(1);");
104
105 test(modules, new String[] {
106 // m1
107 "function g(f) {f++}",
108 // m2
109 "function f(a) { alert(a); }" +
110 "f(1);",
111 });
112 }
113
114 public void testFunctionMovement4() {
115 // Try out moving a function which returns a closure
116 JSModule[] modules = createModuleStar(
117 // m1
118 "function f(){return function(a){}}",
119 // m2
120 "var a = f();"
121 );
122
123 test(modules, new String[] {
124 // m1
125 "",
126 // m2
127 "function f(){return function(a){}}" +
128 "var a = f();",
129 });
130 }
131
132 public void testFunctionMovement5() {
133 // Try moving a recursive function [using factorials for kicks]
134 JSModule[] modules = createModuleStar(
135 // m1
136 "function f(n){return (n<1)?1:f(n-1)}",
137 // m2
138 "var a = f(4);"
139 );
140
141 test(modules, new String[] {
142 // m1
143 "",
144 // m2
145 "function f(n){return (n<1)?1:f(n-1)}" +
146 "var a = f(4);",
147 });
148 }
149
150 public void testFunctionMovement5b() {
151 // Try moving a recursive function declared differently.
152 JSModule[] modules = createModuleStar(
153 // m1
154 "var f = function(n){return (n<1)?1:f(n-1)};",
155 // m2
156 "var a = f(4);"
157 );
158
159 test(modules, new String[] {
160 // m1
161 "",
162 // m2
163 "var f = function(n){return (n<1)?1:f(n-1)};" +
164 "var a = f(4);",
165 });
166 }
167
168 public void testFunctionMovement6() {
169 // Try out moving to the common ancestor
170 JSModule[] modules = createModuleChain(
171 // m1
172 "function f(){return 1}",
173 // m2
174 "var a = f();",
175 // m3
176 "var b = f();"
177 );
178
179 test(modules, new String[] {
180 // m1
181 "",
182 // m2
183 "function f(){return 1}" +
184 "var a = f();",
185 // m3
186 "var b = f();",
187 });
188 }
189
190 public void testFunctionMovement7() {
191 // Try out moving to the common ancestor with deeper ancestry chain
192 JSModule[] modules = createModules(
193 // m1
194 "function f(){return 1}",
195 // m2
196 "",
197 // m3
198 "var a = f();",
199 // m4
200 "var b = f();",
201 // m5
202 "var c = f();"
203 );
204
205
206 modules[1].addDependency(modules[0]);
207 modules[2].addDependency(modules[1]);
208 modules[3].addDependency(modules[1]);
209 modules[4].addDependency(modules[1]);
210
211 test(modules, new String[] {
212 // m1
213 "",
214 // m2
215 "function f(){return 1}",
216 // m3
217 "var a = f();",
218 // m4
219 "var b = f();",
220 // m5
221 "var c = f();",
222 });
223 }
224
225 public void testFunctionMovement8() {
226 // Check what happens with named functions
227 JSModule[] modules = createModuleChain(
228 // m1
229 "var v = function f(){return 1}",
230 // m2
231 "v();"
232 );
233
234 test(modules, new String[] {
235 // m1
236 "",
237 // m2
238 "var v = function f(){return 1};" +
239 "v();",
240 });
241 }
242
243 public void testFunctionNonMovement1() {
244 // This tests lots of things:
245 // 1) we can't move it if it is a class with non-const attributes accessed
246 // 2) if it's in an if statement, we can't move it
247 // 3) if it's in an while statement, we can't move it [with some extra
248 // block elements]
249 testSame(createModuleStar(
250 // m1
251 "function f(){};f.prototype.bar=new f;" +
252 "if(a)function f2(){}" +
253 "{{while(a)function f3(){}}}",
254 // m2
255 "var a = new f();f2();f3();"));
256 }
257
258 public void testFunctionNonMovement2() {
259 // A generic case where 2 modules depend on the first one. But it's the
260 // common ancestor, so we can't move.
261 testSame(createModuleStar(
262 // m1
263 "function f(){return 1}",
264 // m2
265 "var a = f();",
266 // m3
267 "var b = f();"));
268 }
269
270 public void testClassMovement1() {
271 test(createModuleStar(
272 // m1
273 "function f(){} f.prototype.bar=function (){};",
274 // m2
275 "var a = new f();"),
276 new String[] {
277 "",
278 "function f(){} f.prototype.bar=function (){};" +
279 "var a = new f();"
280 });
281 }
282
283 public void testClassMovement2() {
284 // NOTE: this is the result of two iterations
285 test(createModuleChain(
286 // m1
287 "function f(){} f.prototype.bar=3; f.prototype.baz=5;",
288 // m2
289 "f.prototype.baq = 7;",
290 // m3
291 "f.prototype.baz = 9;",
292 // m4
293 "var a = new f();"),
294 new String[] {
295 // m1
296 "",
297 // m2
298 "",
299 // m3
300 "function f(){} f.prototype.bar=3; f.prototype.baz=5;" +
301 "f.prototype.baq = 7;" +
302 "f.prototype.baz = 9;",
303 // m4
304 "var a = new f();"
305 });
306 }
307
308 public void testClassMovement3() {
309 // NOTE: this is the result of two iterations
310 test(createModuleChain(
311 // m1
312 "var f = function() {}; f.prototype.bar=3; f.prototype.baz=5;",
313 // m2
314 "f = 7;",
315 // m3
316 "f = 9;",
317 // m4
318 "f = 11;"),
319 new String[] {
320 // m1
321 "",
322 // m2
323 "",
324 // m3
325 "var f = function() {}; f.prototype.bar=3; f.prototype.baz=5;" +
326 "f = 7;" +
327 "f = 9;",
328 // m4
329 "f = 11;"
330 });
331 }
332
333 public void testClassMovement4() {
334 testSame(createModuleStar(
335 // m1
336 "function f(){} f.prototype.bar=3; f.prototype.baz=5;",
337 // m2
338 "f.prototype.baq = 7;",
339 // m3
340 "var a = new f();"));
341 }
342
343 public void testClassMovement5() {
344 JSModule[] modules = createModules(
345 // m1
346 "function f(){} f.prototype.bar=3; f.prototype.baz=5;",
347 // m2
348 "",
349 // m3
350 "f.prototype.baq = 7;",
351 // m4
352 "var a = new f();");
353
354 modules[1].addDependency(modules[0]);
355 modules[2].addDependency(modules[1]);
356 modules[3].addDependency(modules[1]);
357
358 test(modules,
359 new String[] {
360 // m1
361 "",
362 // m2
363 "function f(){} f.prototype.bar=3; f.prototype.baz=5;",
364 // m3
365 "f.prototype.baq = 7;",
366 // m4 +
367 "var a = new f();"
368 });
369 }
370
371 public void testClassMovement6() {
372 test(createModuleChain(
373 // m1
374 "function Foo(){} function Bar(){} goog.inherits(Bar, Foo);" +
375 "new Foo();",
376 // m2
377 "new Bar();"),
378 new String[] {
379 // m1
380 "function Foo(){} new Foo();",
381 // m2
382 "function Bar(){} goog.inherits(Bar, Foo); new Bar();"
383 });
384 }
385
386 public void testClassMovement7() {
387 testSame(createModuleChain(
388 // m1
389 "function Foo(){} function Bar(){} goog.inherits(Bar, Foo);" +
390 "new Bar();",
391 // m2
392 "new Foo();"));
393 }
394
395 public void testStubMethodMovement1() {
396 test(createModuleChain(
397 // m1
398 "function Foo(){} " +
399 "Foo.prototype.bar = JSCompiler_stubMethod(x);",
400 // m2
401 "new Foo();"),
402 new String[] {
403 // m1
404 "",
405 "function Foo(){} " +
406 "Foo.prototype.bar = JSCompiler_stubMethod(x);" +
407 "new Foo();"
408 });
409 }
410
411 public void testStubMethodMovement2() {
412 test(createModuleChain(
413 // m1
414 "function Foo(){} " +
415 "Foo.prototype.bar = JSCompiler_unstubMethod(x);",
416 // m2
417 "new Foo();"),
418 new String[] {
419 // m1
420 "",
421 "function Foo(){} " +
422 "Foo.prototype.bar = JSCompiler_unstubMethod(x);" +
423 "new Foo();"
424 });
425 }
426
427 public void testNoMoveSideEffectProperty() {
428 testSame(createModuleChain(
429 // m1
430 "function Foo(){} " +
431 "Foo.prototype.bar = createSomething();",
432 // m2
433 "new Foo();"));
434 }
435
436 public void testAssignMovement() {
437 test(createModuleChain(
438 // m1
439 "var f = 3;" +
440 "f = 5;",
441 // m2
442 "var h = f;"),
443 new String[] {
444 // m1
445 "",
446 // m2
447 "var f = 3;" +
448 "f = 5;" +
449 "var h = f;"
450 });
451
452 // don't move nested assigns
453 testSame(createModuleChain(
454 // m1
455 "var f = 3;" +
456 "var g = f = 5;",
457 // m2
458 "var h = f;"));
459 }
460
461 public void testNoClassMovement2() {
462 test(createModuleChain(
463 // m1
464 "var f = {};" +
465 "f.h = 5;",
466 // m2
467 "var h = f;"),
468 new String[] {
469 // m1
470 "",
471 // m2
472 "var f = {};" +
473 "f.h = 5;" +
474 "var h = f;"
475 });
476
477 // don't move nested getprop assigns
478 testSame(createModuleChain(
479 // m1
480 "var f = {};" +
481 "var g = f.h = 5;",
482 // m2
483 "var h = f;"));
484 }
485
486 public void testLiteralMovement1() {
487 test(createModuleChain(
488 // m1
489 "var f = {'hi': 'mom', 'bye': function() {}};",
490 // m2
491 "var h = f;"),
492 new String[] {
493 // m1
494 "",
495 // m2
496 "var f = {'hi': 'mom', 'bye': function() {}};" +
497 "var h = f;"
498 });
499 }
500
501 public void testLiteralMovement2() {
502 testSame(createModuleChain(
503 // m1
504 "var f = {'hi': 'mom', 'bye': goog.nullFunction};",
505 // m2
506 "var h = f;"));
507 }
508
509 public void testLiteralMovement3() {
510 test(createModuleChain(
511 // m1
512 "var f = ['hi', function() {}];",
513 // m2
514 "var h = f;"),
515 new String[] {
516 // m1
517 "",
518 // m2
519 "var f = ['hi', function() {}];" +
520 "var h = f;"
521 });
522 }
523
524 public void testLiteralMovement4() {
525 testSame(createModuleChain(
526 // m1
527 "var f = ['hi', goog.nullFunction];",
528 // m2
529 "var h = f;"));
530 }
531
532 public void testVarMovement1() {
533 // test moving a variable
534 JSModule[] modules = createModuleStar(
535 // m1
536 "var a = 0;",
537 // m2
538 "var x = a;"
539 );
540
541 test(modules, new String[] {
542 // m1
543 "",
544 // m2
545 "var a = 0;" +
546 "var x = a;",
547 });
548 }
549
550 public void testVarMovement2() {
551 // Test moving 1 variable out of the block
552 JSModule[] modules = createModuleStar(
553 // m1
554 "var a = 0; var b = 1; var c = 2;",
555 // m2
556 "var x = b;"
557 );
558
559 test(modules, new String[] {
560 // m1
561 "var a = 0; var c = 2;",
562 // m2
563 "var b = 1;" +
564 "var x = b;"
565 });
566 }
567
568 public void testVarMovement3() {
569 // Test moving all variables out of the block
570 JSModule[] modules = createModuleStar(
571 // m1
572 "var a = 0; var b = 1;",
573 // m2
574 "var x = a + b;"
575 );
576
577 test(modules, new String[] {
578 // m1
579 "",
580 // m2
581 "var b = 1;" +
582 "var a = 0;" +
583 "var x = a + b;"
584 });
585 }
586
587
588 public void testVarMovement4() {
589 // Test moving a function
590 JSModule[] modules = createModuleStar(
591 // m1
592 "var a = function(){alert(1)};",
593 // m2
594 "var x = a;"
595 );
596
597 test(modules, new String[] {
598 // m1
599 "",
600 // m2
601 "var a = function(){alert(1)};" +
602 "var x = a;"
603 });
604 }
605
606
607 public void testVarMovement5() {
608 // Don't move a function outside of scope
609 testSame(createModuleStar(
610 // m1
611 "var a = alert;",
612 // m2
613 "var x = a;"));
614 }
615
616 public void testVarMovement6() {
617 // Test moving a var with no assigned value
618 JSModule[] modules = createModuleStar(
619 // m1
620 "var a;",
621 // m2
622 "var x = a;"
623 );
624
625 test(modules, new String[] {
626 // m1
627 "",
628 // m2
629 "var a;" +
630 "var x = a;"
631 });
632 }
633
634 public void testVarMovement7() {
635 // Don't move a variable higher in the dependency tree
636 testSame(createModuleStar(
637 // m1
638 "function f() {g();}",
639 // m2
640 "function g(){};"));
641 }
642
643 public void testVarMovement8() {
644 JSModule[] modules = createModuleBush(
645 // m1
646 "var a = 0;",
647 // m2 -> m1
648 "",
649 // m3 -> m2
650 "var x = a;",
651 // m4 -> m2
652 "var y = a;"
653 );
654
655 test(modules, new String[] {
656 // m1
657 "",
658 // m2
659 "var a = 0;",
660 // m3
661 "var x = a;",
662 // m4
663 "var y = a;"
664 });
665 }
666
667 public void testVarMovement9() {
668 JSModule[] modules = createModuleTree(
669 // m1
670 "var a = 0; var b = 1; var c = 3;",
671 // m2 -> m1
672 "",
673 // m3 -> m1
674 "",
675 // m4 -> m2
676 "a;",
677 // m5 -> m2
678 "a;c;",
679 // m6 -> m3
680 "b;",
681 // m7 -> m4
682 "b;c;"
683 );
684
685 test(modules, new String[] {
686 // m1
687 "var c = 3;",
688 // m2
689 "var a = 0;",
690 // m3
691 "var b = 1;",
692 // m4
693 "a;",
694 // m5
695 "a;c;",
696 // m6
697 "b;",
698 // m7
699 "b;c;"
700 });
701 }
702
703 public void testClone1() {
704 test(createModuleChain(
705 // m1
706 "function f(){} f.prototype.clone = function() { return new f };",
707 // m2
708 "var a = (new f).clone();"),
709 new String[] {
710 // m1
711 "",
712 "function f(){} f.prototype.clone = function() { return new f() };" +
713 // m2
714 "var a = (new f).clone();"
715 });
716 }
717
718 public void testClone2() {
719 test(createModuleChain(
720 // m1
721 "function f(){}" +
722 "f.prototype.cloneFun = function() {" +
723 " return function() {new f}" +
724 "};",
725 // m2
726 "var a = (new f).cloneFun();"),
727 new String[] {
728 // m1
729 "",
730 "function f(){}" +
731 "f.prototype.cloneFun = function() {" +
732 " return function() {new f}" +
733 "};" +
734 // m2
735 "var a = (new f).cloneFun();"
736 });
737 }
738
739 public void testBug4118005() {
740 testSame(createModuleChain(
741 // m1
742 "var m = 1;\n" +
743 "(function () {\n" +
744 " var x = 1;\n" +
745 " m = function() { return x };\n" +
746 "})();\n",
747 // m2
748 "m();"));
749 }
750
751 public void testEmptyModule() {
752 // When the dest module is empty, it might try to move the code to the
753 // one of the modules that the empty module depends on. In some cases
754 // this might ended up to be the same module as the definition of the code.
755 // When that happens, CrossModuleCodeMotion might report a code change
756 // while nothing is moved. This should not be a problem if we know all
757 // modules are non-empty.
758 JSModule m1 = new JSModule("m1");
759 m1.add(SourceFile.fromCode("m1", "function x() {}"));
760
761 JSModule empty = new JSModule("empty");
762 empty.addDependency(m1);
763
764 JSModule m2 = new JSModule("m2");
765 m2.add(SourceFile.fromCode("m2", "x()"));
766 m2.addDependency(empty);
767
768 JSModule m3 = new JSModule("m3");
769 m3.add(SourceFile.fromCode("m3", "x()"));
770 m3.addDependency(empty);
771
772 test(new JSModule[] {m1,empty,m2,m3},
773 new String[] {
774 "",
775 "function x() {}",
776 "x()",
777 "x()"
778 });
779 }
780 }