| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.javascript.rhino.Node; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class CreateSyntheticBlocksTest extends CompilerTestCase { |
| 25 |
private static final String START_MARKER = "startMarker"; |
| 26 |
private static final String END_MARKER = "endMarker"; |
| 27 |
|
| 28 |
public CreateSyntheticBlocksTest() { |
| 29 |
|
| 30 |
super("", false); |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public void setUp() { |
| 35 |
super.enableLineNumberCheck(false); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 40 |
return new CompilerPass() { |
| 41 |
@Override |
| 42 |
public void process(Node externs, Node js) { |
| 43 |
new CreateSyntheticBlocks(compiler, START_MARKER, END_MARKER).process( |
| 44 |
externs, js); |
| 45 |
NodeTraversal.traverse(compiler, js, new MinimizeExitPoints(compiler)); |
| 46 |
|
| 47 |
new PeepholeOptimizationsPass(compiler, |
| 48 |
new PeepholeRemoveDeadCode(), |
| 49 |
new PeepholeSubstituteAlternateSyntax(true), |
| 50 |
new PeepholeFoldConstants(true)) |
| 51 |
.process(externs, js); |
| 52 |
new MinimizeExitPoints(compiler).process(externs, js); |
| 53 |
|
| 54 |
new Denormalize(compiler).process(externs, js); |
| 55 |
} |
| 56 |
}; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
protected int getNumRepetitions() { |
| 61 |
return 1; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public void testFold1() { |
| 67 |
test("function f() { if (x) return; y(); }", |
| 68 |
"function f(){x||y()}"); |
| 69 |
} |
| 70 |
|
| 71 |
public void testFoldWithMarkers1() { |
| 72 |
testSame("function f(){startMarker();if(x)return;endMarker();y()}"); |
| 73 |
} |
| 74 |
|
| 75 |
public void testFoldWithMarkers1a() { |
| 76 |
testSame("function f(){startMarker();if(x)return;endMarker()}"); |
| 77 |
} |
| 78 |
|
| 79 |
public void testFold2() { |
| 80 |
test("function f() { if (x) return; y(); if (a) return; b(); }", |
| 81 |
"function f(){if(!x){y();a||b()}}"); |
| 82 |
} |
| 83 |
|
| 84 |
public void testFoldWithMarkers2() { |
| 85 |
testSame("function f(){startMarker(\"FOO\");startMarker(\"BAR\");" + |
| 86 |
"if(x)return;endMarker(\"BAR\");y();if(a)return;" + |
| 87 |
"endMarker(\"FOO\");b()}"); |
| 88 |
} |
| 89 |
|
| 90 |
public void testUnmatchedStartMarker() { |
| 91 |
testSame("startMarker()", CreateSyntheticBlocks.UNMATCHED_START_MARKER); |
| 92 |
} |
| 93 |
|
| 94 |
public void testUnmatchedEndMarker1() { |
| 95 |
testSame("endMarker()", CreateSyntheticBlocks.UNMATCHED_END_MARKER); |
| 96 |
} |
| 97 |
|
| 98 |
public void testUnmatchedEndMarker2() { |
| 99 |
test("if(y){startMarker();x()}endMarker()", |
| 100 |
"if(y){startMarker();x()}endMarker()", null, |
| 101 |
CreateSyntheticBlocks.UNMATCHED_END_MARKER); |
| 102 |
} |
| 103 |
|
| 104 |
public void testInvalid1() { |
| 105 |
test("startMarker() && true", |
| 106 |
"startMarker()", null, |
| 107 |
CreateSyntheticBlocks.INVALID_MARKER_USAGE); |
| 108 |
} |
| 109 |
|
| 110 |
public void testInvalid2() { |
| 111 |
test("false && endMarker()", |
| 112 |
"", null, |
| 113 |
CreateSyntheticBlocks.INVALID_MARKER_USAGE); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
public void testDenormalize() { |
| 118 |
testSame("startMarker();for(;;);endMarker()"); |
| 119 |
} |
| 120 |
|
| 121 |
public void testNonMarkingUse() { |
| 122 |
testSame("function foo(endMarker){}"); |
| 123 |
testSame("function foo(){startMarker:foo()}"); |
| 124 |
} |
| 125 |
|
| 126 |
public void testContainingBlockPreservation() { |
| 127 |
testSame("if(y){startMarker();x();endMarker()}"); |
| 128 |
} |
| 129 |
} |