Debian Codegraph @

test/com/google/javascript/jscomp/ExpressionDecomposerTest.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.collect.Sets;
20 import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
21 import com.google.javascript.jscomp.ExpressionDecomposer.DecompositionType;
22 import com.google.javascript.rhino.Node;
23 import junit.framework.TestCase;
24
25 import java.util.Set;
26
27 import javax.annotation.Nullable;
28
29 /**
30 * Unit tests for ExpressionDecomposer
31 * @author [email protected] (John Lenz)
32 */
33 public class ExpressionDecomposerTest extends TestCase {
34 // Note: functions "foo" and "goo" are external functions
35 // in the helper.
36
37 public void testCanExposeExpression1() {
38 // Can't move or decompose some classes of expressions.
39 helperCanExposeExpression(
40 DecompositionType.UNDECOMPOSABLE, "while(foo());", "foo");
41 helperCanExposeExpression(
42 DecompositionType.UNDECOMPOSABLE, "while(x = goo()&&foo()){}", "foo");
43 helperCanExposeExpression(
44 DecompositionType.UNDECOMPOSABLE, "while(x += goo()&&foo()){}", "foo");
45
46 helperCanExposeExpression(
47 DecompositionType.UNDECOMPOSABLE, "do{}while(foo());", "foo");
48 helperCanExposeExpression(
49 DecompositionType.UNDECOMPOSABLE, "for(;foo(););", "foo");
50 // This case could be supported for loops without conditional continues
51 // by moving the increment into the loop body.
52 helperCanExposeExpression(
53 DecompositionType.UNDECOMPOSABLE, "for(;;foo());", "foo");
54 // FOR initializer could be supported but they never occur
55 // as they are normalized away.
56
57 // This is potentially doable but a bit too complex currently.
58 helperCanExposeExpression(
59 DecompositionType.UNDECOMPOSABLE, "switch(1){case foo():;}", "foo");
60 }
61
62 public void testCanExposeExpression2() {
63 helperCanExposeExpression(
64 DecompositionType.MOVABLE, "foo()", "foo");
65 helperCanExposeExpression(
66 DecompositionType.MOVABLE, "x = foo()", "foo");
67 helperCanExposeExpression(
68 DecompositionType.MOVABLE, "var x = foo()", "foo");
69 helperCanExposeExpression(
70 DecompositionType.MOVABLE, "if(foo()){}", "foo");
71 helperCanExposeExpression(
72 DecompositionType.MOVABLE, "switch(foo()){}", "foo");
73 helperCanExposeExpression(
74 DecompositionType.MOVABLE, "switch(foo()){}", "foo");
75 helperCanExposeExpression(
76 DecompositionType.MOVABLE, "function f(){ return foo();}", "foo");
77
78 helperCanExposeExpression(
79 DecompositionType.MOVABLE, "x = foo() && 1", "foo");
80 helperCanExposeExpression(
81 DecompositionType.MOVABLE, "x = foo() || 1", "foo");
82 helperCanExposeExpression(
83 DecompositionType.MOVABLE, "x = foo() ? 0 : 1", "foo");
84 helperCanExposeExpression(
85 DecompositionType.MOVABLE, "(function(a){b = a})(foo())", "foo");
86 }
87
88 public void testCanExposeExpression3() {
89 helperCanExposeExpression(
90 DecompositionType.DECOMPOSABLE, "x = 0 && foo()", "foo");
91 helperCanExposeExpression(
92 DecompositionType.DECOMPOSABLE, "x = 1 || foo()", "foo");
93 helperCanExposeExpression(
94 DecompositionType.DECOMPOSABLE, "var x = 1 ? foo() : 0", "foo");
95
96 helperCanExposeExpression(
97 DecompositionType.DECOMPOSABLE, "goo() && foo()", "foo");
98 helperCanExposeExpression(
99 DecompositionType.DECOMPOSABLE, "x = goo() && foo()", "foo");
100 helperCanExposeExpression(
101 DecompositionType.DECOMPOSABLE, "x += goo() && foo()", "foo");
102 helperCanExposeExpression(
103 DecompositionType.DECOMPOSABLE, "var x = goo() && foo()", "foo");
104 helperCanExposeExpression(
105 DecompositionType.DECOMPOSABLE, "if(goo() && foo()){}", "foo");
106 helperCanExposeExpression(
107 DecompositionType.DECOMPOSABLE, "switch(goo() && foo()){}", "foo");
108 helperCanExposeExpression(
109 DecompositionType.DECOMPOSABLE, "switch(goo() && foo()){}", "foo");
110 helperCanExposeExpression(
111 DecompositionType.DECOMPOSABLE, "switch(x = goo() && foo()){}", "foo");
112 helperCanExposeExpression(
113 DecompositionType.DECOMPOSABLE,
114 "function f(){ return goo() && foo();}", "foo");
115 }
116
117 public void testCanExposeExpression4() {
118 // 'this' must be preserved in call.
119 helperCanExposeExpression(
120 DecompositionType.UNDECOMPOSABLE, "if (goo.a(1, foo()));", "foo");
121 }
122
123 public void testCanExposeExpression5() {
124 // 'this' must be preserved in call.
125 helperCanExposeExpression(
126 DecompositionType.UNDECOMPOSABLE, "if (goo['a'](foo()));", "foo");
127 }
128
129 public void testCanExposeExpression6() {
130 // 'this' must be preserved in call.
131 helperCanExposeExpression(
132 DecompositionType.UNDECOMPOSABLE, "z:if (goo.a(1, foo()));", "foo");
133 }
134
135 public void testCanExposeExpression7() {
136 // Verify calls to function expressions are movable.
137 helperCanExposeFunctionExpression(
138 DecompositionType.MOVABLE,
139 "(function(map){descriptions_=map})(\n" +
140 "function(){\n" +
141 "var ret={};\n" +
142 "ret[INIT]='a';\n" +
143 "ret[MIGRATION_BANNER_DISMISS]='b';\n" +
144 "return ret\n" +
145 "}()\n" +
146 ");", 2);
147 }
148
149 public void testCanExposeExpression8() {
150 // Can it be decompose?
151 helperCanExposeExpression(
152 DecompositionType.DECOMPOSABLE,
153 "HangoutStarter.prototype.launchHangout = function() {\n" +
154 " var self = a.b;\n" +
155 " var myUrl = new goog.Uri(getDomServices_(self).getDomHelper()." +
156 "getWindow().location.href);\n" +
157 "};",
158 "getDomServices_");
159
160 // Verify it is properly expose the target expression.
161 helperExposeExpression(
162 "HangoutStarter.prototype.launchHangout = function() {\n" +
163 " var self = a.b;\n" +
164 " var myUrl = new goog.Uri(getDomServices_(self).getDomHelper()." +
165 "getWindow().location.href);\n" +
166 "};",
167 "getDomServices_",
168 "HangoutStarter.prototype.launchHangout = function() {" +
169 " var self = a.b;" +
170 " var temp_const$$0 = goog.Uri;" +
171 " var myUrl = new temp_const$$0(getDomServices_(self)." +
172 " getDomHelper().getWindow().location.href)}");
173
174 // Verify the results can be properly moved.
175 helperMoveExpression(
176 "HangoutStarter.prototype.launchHangout = function() {" +
177 " var self = a.b;" +
178 " var temp_const$$0 = goog.Uri;" +
179 " var myUrl = new temp_const$$0(getDomServices_(self)." +
180 " getDomHelper().getWindow().location.href)}",
181 "getDomServices_",
182 "HangoutStarter.prototype.launchHangout = function() {" +
183 " var self=a.b;" +
184 " var temp_const$$0=goog.Uri;" +
185 " var result$$0=getDomServices_(self);" +
186 " var myUrl=new temp_const$$0(result$$0.getDomHelper()." +
187 " getWindow().location.href)}");
188 }
189
190 public void testMoveExpression1() {
191 // There isn't a reason to do this, but it works.
192 helperMoveExpression("foo()", "foo", "var result$$0 = foo(); result$$0;");
193 }
194
195 public void testMoveExpression2() {
196 helperMoveExpression(
197 "x = foo()",
198 "foo",
199 "var result$$0 = foo(); x = result$$0;");
200 }
201
202 public void testMoveExpression3() {
203 helperMoveExpression(
204 "var x = foo()",
205 "foo",
206 "var result$$0 = foo(); var x = result$$0;");
207 }
208
209 public void testMoveExpression4() {
210 helperMoveExpression(
211 "if(foo()){}",
212 "foo",
213 "var result$$0 = foo(); if (result$$0);");
214 }
215
216 public void testMoveExpression5() {
217 helperMoveExpression(
218 "switch(foo()){}",
219 "foo",
220 "var result$$0 = foo(); switch(result$$0){}");
221 }
222
223 public void testMoveExpression6() {
224 helperMoveExpression(
225 "switch(1 + foo()){}",
226 "foo",
227 "var result$$0 = foo(); switch(1 + result$$0){}");
228 }
229
230 public void testMoveExpression7() {
231 helperMoveExpression(
232 "function f(){ return foo();}",
233 "foo",
234 "function f(){ var result$$0 = foo(); return result$$0;}");
235 }
236
237 public void testMoveExpression8() {
238 helperMoveExpression(
239 "x = foo() && 1",
240 "foo",
241 "var result$$0 = foo(); x = result$$0 && 1");
242 }
243
244 public void testMoveExpression9() {
245 helperMoveExpression(
246 "x = foo() || 1",
247 "foo",
248 "var result$$0 = foo(); x = result$$0 || 1");
249 }
250
251 public void testMoveExpression10() {
252 helperMoveExpression(
253 "x = foo() ? 0 : 1",
254 "foo",
255 "var result$$0 = foo(); x = result$$0 ? 0 : 1");
256 }
257
258 /* Decomposition tests. */
259
260 public void testExposeExpression1() {
261 helperExposeExpression(
262 "x = 0 && foo()",
263 "foo",
264 "var temp$$0; if (temp$$0 = 0) temp$$0 = foo(); x = temp$$0;");
265 }
266
267 public void testExposeExpression2() {
268 helperExposeExpression(
269 "x = 1 || foo()",
270 "foo",
271 "var temp$$0; if (temp$$0 = 1); else temp$$0 = foo(); x = temp$$0;");
272 }
273
274 public void testExposeExpression3() {
275 helperExposeExpression(
276 "var x = 1 ? foo() : 0",
277 "foo",
278 "var temp$$0;" +
279 " if (1) temp$$0 = foo(); else temp$$0 = 0;var x = temp$$0;");
280 }
281
282 public void testExposeExpression4() {
283 helperExposeExpression(
284 "goo() && foo()",
285 "foo",
286 "if (goo()) foo();");
287 }
288
289 public void testExposeExpression5() {
290 helperExposeExpression(
291 "x = goo() && foo()",
292 "foo",
293 "var temp$$0; if (temp$$0 = goo()) temp$$0 = foo(); x = temp$$0;");
294 }
295
296 public void testExposeExpression6() {
297 helperExposeExpression(
298 "var x = 1 + (goo() && foo())",
299 "foo",
300 "var temp$$0; if (temp$$0 = goo()) temp$$0 = foo();" +
301 "var x = 1 + temp$$0;");
302 }
303
304 public void testExposeExpression7() {
305 helperExposeExpression(
306 "if(goo() && foo());",
307 "foo",
308 "var temp$$0;" +
309 "if (temp$$0 = goo()) temp$$0 = foo();" +
310 "if(temp$$0);");
311 }
312
313 public void testExposeExpression8() {
314 helperExposeExpression(
315 "switch(goo() && foo()){}",
316 "foo",
317 "var temp$$0;" +
318 "if (temp$$0 = goo()) temp$$0 = foo();" +
319 "switch(temp$$0){}");
320 }
321
322 public void testExposeExpression9() {
323 helperExposeExpression(
324 "switch(1 + goo() + foo()){}",
325 "foo",
326 "var temp_const$$0 = 1 + goo();" +
327 "switch(temp_const$$0 + foo()){}");
328 }
329
330 public void testExposeExpression10() {
331 helperExposeExpression(
332 "function f(){ return goo() && foo();}",
333 "foo",
334 "function f(){" +
335 "var temp$$0; if (temp$$0 = goo()) temp$$0 = foo();" +
336 "return temp$$0;" +
337 "}");
338 }
339
340 public void testExposeExpression11() {
341 // TODO(johnlenz): We really want a constant marking pass.
342 // The value "goo" should be constant, but it isn't known to be so.
343 helperExposeExpression(
344 "if (goo(1, goo(2), (1 ? foo() : 0)));",
345 "foo",
346 "var temp_const$$1 = goo;" +
347 "var temp_const$$0 = goo(2);" +
348 "var temp$$2;" +
349 "if (1) temp$$2 = foo(); else temp$$2 = 0;" +
350 "if (temp_const$$1(1, temp_const$$0, temp$$2));");
351 }
352
353 // Simple name on LHS of assignment-op.
354 public void testExposePlusEquals1() {
355 helperExposeExpression(
356 "var x = 0; x += foo() + 1",
357 "foo",
358 "var x = 0; var temp_const$$0 = x;" +
359 "x = temp_const$$0 + (foo() + 1);");
360
361 helperExposeExpression(
362 "var x = 0; y = (x += foo()) + x",
363 "foo",
364 "var x = 0; var temp_const$$0 = x;" +
365 "y = (x = temp_const$$0 + foo()) + x");
366 }
367
368 // Structure on LHS of assignment-op.
369 public void testExposePlusEquals2() {
370 helperExposeExpression(
371 "var x = {}; x.a += foo() + 1",
372 "foo",
373 "var x = {}; var temp_const$$0 = x;" +
374 "var temp_const$$1 = temp_const$$0.a;" +
375 "temp_const$$0.a = temp_const$$1 + (foo() + 1);");
376
377 helperExposeExpression(
378 "var x = {}; y = (x.a += foo()) + x.a",
379 "foo",
380 "var x = {}; var temp_const$$0 = x;" +
381 "var temp_const$$1 = temp_const$$0.a;" +
382 "y = (temp_const$$0.a = temp_const$$1 + foo()) + x.a");
383 }
384
385 // Constant object on LHS of assignment-op.
386 public void testExposePlusEquals3() {
387 helperExposeExpression(
388 "/** @const */ var XX = {};\n" +
389 "XX.a += foo() + 1",
390 "foo",
391 "var XX = {}; var temp_const$$0 = XX.a;" +
392 "XX.a = temp_const$$0 + (foo() + 1);");
393
394 helperExposeExpression(
395 "var XX = {}; y = (XX.a += foo()) + XX.a",
396 "foo",
397 "var XX = {}; var temp_const$$0 = XX.a;" +
398 "y = (XX.a = temp_const$$0 + foo()) + XX.a");
399 }
400
401 // Function all on LHS of assignment-op.
402 public void testExposePlusEquals4() {
403 helperExposeExpression(
404 "var x = {}; goo().a += foo() + 1",
405 "foo",
406 "var x = {};" +
407 "var temp_const$$0 = goo();" +
408 "var temp_const$$1 = temp_const$$0.a;" +
409 "temp_const$$0.a = temp_const$$1 + (foo() + 1);");
410
411 helperExposeExpression(
412 "var x = {}; y = (goo().a += foo()) + goo().a",
413 "foo",
414 "var x = {};" +
415 "var temp_const$$0 = goo();" +
416 "var temp_const$$1 = temp_const$$0.a;" +
417 "y = (temp_const$$0.a = temp_const$$1 + foo()) + goo().a");
418 }
419
420 // Test multiple levels
421 public void testExposePlusEquals5() {
422 helperExposeExpression(
423 "var x = {}; goo().a.b += foo() + 1",
424 "foo",
425 "var x = {};" +
426 "var temp_const$$0 = goo().a;" +
427 "var temp_const$$1 = temp_const$$0.b;" +
428 "temp_const$$0.b = temp_const$$1 + (foo() + 1);");
429
430 helperExposeExpression(
431 "var x = {}; y = (goo().a.b += foo()) + goo().a",
432 "foo",
433 "var x = {};" +
434 "var temp_const$$0 = goo().a;" +
435 "var temp_const$$1 = temp_const$$0.b;" +
436 "y = (temp_const$$0.b = temp_const$$1 + foo()) + goo().a");
437 }
438
439 public void testExposeObjectLit1() {
440 // Validate that getter and setters methods are see as side-effect
441 // free and that values can move past them. We don't need to be
442 // concerned with exposing the getter or setter here but the
443 // decomposer does not have a method of exposing properties only variables.
444 helperMoveExpression(
445 "var x = {get a() {}, b: foo()};",
446 "foo",
447 "var result$$0=foo();var x = {get a() {}, b: result$$0};");
448
449 helperMoveExpression(
450 "var x = {set a(p) {}, b: foo()};",
451 "foo",
452 "var result$$0=foo();var x = {set a(p) {}, b: result$$0};");
453 }
454
455 /** Test case helpers. */
456
457 private void helperCanExposeExpression(
458 DecompositionType expectedResult,
459 String code,
460 String fnName
461 ) {
462 helperCanExposeExpression(expectedResult, code, fnName, null);
463 }
464
465 private void helperCanExposeFunctionExpression(
466 DecompositionType expectedResult, String code, int call) {
467 Compiler compiler = getCompiler();
468 Set<String> knownConstants = Sets.newHashSet();
469 ExpressionDecomposer decomposer = new ExpressionDecomposer(
470 compiler, compiler.getUniqueNameIdSupplier(), knownConstants);
471 Node tree = parse(compiler, code);
472 assertNotNull(tree);
473
474 Node externsRoot = parse(compiler,
475 "function goo() {}" +
476 "function foo() {}");
477 assertNotNull(externsRoot);
478
479 Node callSite = findCall(tree, null, 2);
480 assertNotNull("Call " + call + " was not found.", callSite);
481
482 compiler.resetUniqueNameId();
483 DecompositionType result = decomposer.canExposeExpression(
484 callSite);
485 assertEquals(expectedResult, result);
486 }
487
488 private void helperCanExposeExpression(
489 DecompositionType expectedResult,
490 String code,
491 String fnName,
492 Set<String> knownConstants
493 ) {
494 Compiler compiler = getCompiler();
495 if (knownConstants == null) {
496 knownConstants = Sets.newHashSet();
497 }
498 ExpressionDecomposer decomposer = new ExpressionDecomposer(
499 compiler, compiler.getUniqueNameIdSupplier(), knownConstants);
500 Node tree = parse(compiler, code);
501 assertNotNull(tree);
502
503 Node externsRoot = parse(compiler,
504 "function goo() {}" +
505 "function foo() {}");
506 assertNotNull(externsRoot);
507
508 Node callSite = findCall(tree, fnName);
509 assertNotNull("Call to " + fnName + " was not found.", callSite);
510
511 compiler.resetUniqueNameId();
512 DecompositionType result = decomposer.canExposeExpression(
513 callSite);
514 assertEquals(expectedResult, result);
515 }
516
517 private void helperExposeExpression(
518 String code,
519 String fnName,
520 String expectedResult
521 ) {
522 helperExposeExpression(
523 code, fnName, expectedResult, null);
524 }
525
526 private void validateSourceInfo(Compiler compiler, Node subtree) {
527 (new LineNumberCheck(compiler)).setCheckSubTree(subtree);
528 // Source information problems are reported as compiler errors.
529 if (compiler.getErrorCount() != 0) {
530 String msg = "Error encountered: ";
531 for (JSError err : compiler.getErrors()) {
532 msg += err.toString() + "\n";
533 }
534 assertTrue(msg, compiler.getErrorCount() == 0);
535 }
536 }
537
538 private void helperExposeExpression(
539 String code,
540 String fnName,
541 String expectedResult,
542 Set<String> knownConstants
543 ) {
544 Compiler compiler = getCompiler();
545 if (knownConstants == null) {
546 knownConstants = Sets.newHashSet();
547 }
548 ExpressionDecomposer decomposer = new ExpressionDecomposer(
549 compiler, compiler.getUniqueNameIdSupplier(), knownConstants);
550 decomposer.setTempNamePrefix("temp");
551 decomposer.setResultNamePrefix("result");
552 Node expectedRoot = parse(compiler, expectedResult);
553 Node tree = parse(compiler, code);
554 assertNotNull(tree);
555
556 Node callSite = findCall(tree, fnName);
557 assertNotNull("Call to " + fnName + " was not found.", callSite);
558
559 DecompositionType result = decomposer.canExposeExpression(callSite);
560 assertTrue(result == DecompositionType.DECOMPOSABLE);
561
562 compiler.resetUniqueNameId();
563 decomposer.exposeExpression(callSite);
564 validateSourceInfo(compiler, tree);
565 String explanation = expectedRoot.checkTreeEquals(tree);
566 assertNull("\nExpected: " + compiler.toSource(expectedRoot) +
567 "\nResult: " + compiler.toSource(tree) +
568 "\n" + explanation, explanation);
569 }
570
571 private void helperMoveExpression(
572 String code,
573 String fnName,
574 String expectedResult
575 ) {
576 helperMoveExpression(
577 code, fnName, expectedResult, null);
578 }
579
580 private void helperMoveExpression(
581 String code,
582 String fnName,
583 String expectedResult,
584 Set<String> knownConstants
585 ) {
586 Compiler compiler = getCompiler();
587 if (knownConstants == null) {
588 knownConstants = Sets.newHashSet();
589 }
590
591 ExpressionDecomposer decomposer = new ExpressionDecomposer(
592 compiler, compiler.getUniqueNameIdSupplier(), knownConstants);
593 decomposer.setTempNamePrefix("temp");
594 decomposer.setResultNamePrefix("result");
595 Node expectedRoot = parse(compiler, expectedResult);
596 Node tree = parse(compiler, code);
597 assertNotNull(tree);
598
599 Node callSite = findCall(tree, fnName);
600 assertNotNull("Call to " + fnName + " was not found.", callSite);
601
602 compiler.resetUniqueNameId();
603 decomposer.moveExpression(callSite);
604 validateSourceInfo(compiler, tree);
605 String explanation = expectedRoot.checkTreeEquals(tree);
606 assertNull("\nExpected: " + compiler.toSource(expectedRoot) +
607 "\nResult: " + compiler.toSource(tree) +
608 "\n" + explanation, explanation);
609 }
610
611 private static Compiler getCompiler() {
612 Compiler compiler = new Compiler();
613 CompilerOptions options = new CompilerOptions();
614 options.setLanguageIn(LanguageMode.ECMASCRIPT5);
615 options.setCodingConvention(new GoogleCodingConvention());
616 compiler.initOptions(options);
617 return compiler;
618 }
619
620 private static Node findCall(Node n, String name) {
621 return findCall(n, name, 1);
622 }
623
624 /**
625 * @param name The name to look for.
626 * @param call The call to look for.
627 * @return The return the Nth CALL node to name found in a pre-order
628 * traversal.
629 */
630 private static Node findCall(
631 Node root, @Nullable final String name, final int call) {
632 class Find {
633 int found = 0;
634 Node find(Node n) {
635 if (n.isCall()) {
636 Node callee = n.getFirstChild();
637 if (name == null || (callee.isName()
638 && callee.getString().equals(name))) {
639 found++;
640 if (found == call) {
641 return n;
642 }
643 }
644 }
645
646 for (Node c : n.children()) {
647 Node result = find(c);
648 if (result != null) {
649 return result;
650 }
651 }
652
653 return null;
654 }
655 }
656
657 return (new Find()).find(root);
658 }
659
660 private static Node parse(Compiler compiler, String js) {
661 Node n = Normalize.parseAndNormalizeTestCode(compiler, js);
662 assertEquals(0, compiler.getErrorCount());
663 return n;
664 }
665 }