| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.javascript.jscomp.Normalize.NormalizeStatements; |
| 20 |
import com.google.javascript.rhino.Node; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class DenormalizeTest extends CompilerTestCase { |
| 27 |
@Override |
| 28 |
public CompilerPass getProcessor(final Compiler compiler) { |
| 29 |
return new NormalizeAndDenormalizePass(compiler); |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected int getNumRepetitions() { |
| 34 |
|
| 35 |
return 1; |
| 36 |
} |
| 37 |
|
| 38 |
public void testFor() { |
| 39 |
|
| 40 |
test("a = 0; for(; a < 2 ; a++) foo()", |
| 41 |
"for(a = 0; a < 2 ; a++) foo();"); |
| 42 |
|
| 43 |
test("var a = 0; for(; c < b ; c++) foo()", |
| 44 |
"for(var a = 0; c < b ; c++) foo()"); |
| 45 |
|
| 46 |
|
| 47 |
testSame("var a = 0; a:for(; c < b ; c++) foo()"); |
| 48 |
testSame("var a = 0; a:b:for(; c < b ; c++) foo()"); |
| 49 |
|
| 50 |
|
| 51 |
test("if(x){var a = 0; for(; c < b; c++) foo()}", |
| 52 |
"if(x){for(var a = 0; c < b; c++) foo()}"); |
| 53 |
|
| 54 |
|
| 55 |
test("init(); for(; a < 2 ; a++) foo()", |
| 56 |
"for(init(); a < 2 ; a++) foo();"); |
| 57 |
|
| 58 |
|
| 59 |
test("function f(){ var a; for(; a < 2 ; a++) foo() }", |
| 60 |
"function f(){ for(var a; a < 2 ; a++) foo() }"); |
| 61 |
testSame("function f(){ return; for(; a < 2 ; a++) foo() }"); |
| 62 |
} |
| 63 |
|
| 64 |
public void testForIn() { |
| 65 |
test("var a; for(a in b) foo()", "for (var a in b) foo()"); |
| 66 |
testSame("a = 0; for(a in b) foo()"); |
| 67 |
testSame("var a = 0; for(a in b) foo()"); |
| 68 |
|
| 69 |
|
| 70 |
testSame("var a; a:for(a in b) foo()"); |
| 71 |
testSame("var a; a:b:for(a in b) foo()"); |
| 72 |
|
| 73 |
|
| 74 |
test("if(x){var a; for(a in b) foo()}", |
| 75 |
"if(x){for(var a in b) foo()}"); |
| 76 |
|
| 77 |
|
| 78 |
testSame("init(); for(a in b) foo()"); |
| 79 |
|
| 80 |
|
| 81 |
testSame("function f(){ return; for(a in b) foo() }"); |
| 82 |
} |
| 83 |
|
| 84 |
public void testInOperatorNotInsideFor() { |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
testSame("function f(){ var a; var i=\"length\" in a;" + |
| 92 |
"for(; a < 2 ; a++) foo() }"); |
| 93 |
|
| 94 |
testSame("function f(){ var a; var i=(\"length\" in a);" + |
| 95 |
"for(; a < 2 ; a++) foo() }"); |
| 96 |
|
| 97 |
|
| 98 |
test("function f(){" + |
| 99 |
"var b,a=0; for (var i=(\"length\" in b);a<2; a++) foo()}", |
| 100 |
"function f(){var b; var a=0;var i=(\"length\" in b);" + |
| 101 |
"for (;a<2;a++) foo()}"); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public class NormalizeAndDenormalizePass implements CompilerPass { |
| 111 |
Denormalize denormalizePass; |
| 112 |
NormalizeStatements normalizePass; |
| 113 |
AbstractCompiler compiler; |
| 114 |
|
| 115 |
public NormalizeAndDenormalizePass(AbstractCompiler compiler) { |
| 116 |
this.compiler = compiler; |
| 117 |
denormalizePass = new Denormalize(compiler); |
| 118 |
normalizePass = new NormalizeStatements(compiler, false); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public void process(Node externs, Node root) { |
| 123 |
NodeTraversal.traverse(compiler, root, normalizePass); |
| 124 |
NodeTraversal.traverse(compiler, root, denormalizePass); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
} |