| 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.common.collect.Lists; |
| 20 |
import com.google.javascript.jscomp.DefinitionsRemover.Definition; |
| 21 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 22 |
import com.google.javascript.rhino.Node; |
| 23 |
|
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class DefinitionsRemoverTest extends CompilerTestCase { |
| 33 |
public void testRemoveFunction() { |
| 34 |
testSame("{(function (){bar()})}"); |
| 35 |
test("{function a(){bar()}}", "{}"); |
| 36 |
test("foo(); function a(){} bar()", "foo(); bar();"); |
| 37 |
test("foo(); function a(){} function b(){} bar()", "foo(); bar();"); |
| 38 |
} |
| 39 |
|
| 40 |
public void testRemoveAssignment() { |
| 41 |
test("x = 0;", "0"); |
| 42 |
test("{x = 0}", "{0}"); |
| 43 |
test("x = 0; y = 0;", "0; 0;"); |
| 44 |
test("for (x = 0;x;x) {};", "for(0;x;x) {};"); |
| 45 |
} |
| 46 |
|
| 47 |
public void testRemoveVarAssignment() { |
| 48 |
test("var x = 0;", "0"); |
| 49 |
test("{var x = 0}", "{0}"); |
| 50 |
test("var x = 0; var y = 0;", "0;0"); |
| 51 |
test("var x = 0; var y = 0;", "0;0"); |
| 52 |
} |
| 53 |
|
| 54 |
public void testRemoveLiteral() { |
| 55 |
test("foo({ 'one' : 1 })", "foo({ })"); |
| 56 |
test("foo({ 'one' : 1 , 'two' : 2 })", "foo({ })"); |
| 57 |
} |
| 58 |
|
| 59 |
public void testRemoveFunctionExpressionName() { |
| 60 |
test("foo(function f(){})", "foo(function (){})"); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 65 |
|
| 66 |
return new CompilerPass() { |
| 67 |
@Override |
| 68 |
public void process(Node externs, Node root) { |
| 69 |
DefinitionsGatherer g = new DefinitionsGatherer(); |
| 70 |
(new NodeTraversal(compiler, g)).traverse(root); |
| 71 |
for (Definition def : g.definitions) { |
| 72 |
def.remove(); |
| 73 |
compiler.reportCodeChange(); |
| 74 |
} |
| 75 |
} |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private static class DefinitionsGatherer extends AbstractPostOrderCallback { |
| 83 |
final List<Definition> definitions = Lists.newArrayList(); |
| 84 |
@Override |
| 85 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 86 |
Definition def = DefinitionsRemover.getDefinition(n, false); |
| 87 |
if (def != null) { |
| 88 |
definitions.add(def); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |