Debian Codegraph @

test/com/google/javascript/jscomp/DefinitionsRemoverTest.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.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 * Test for {@link DefinitionsRemover}. Basically test for the simple removal
28 * cases. More complicated cases will be tested by the clients of
29 * {@link DefinitionsRemover}.
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 // Create a pass that removes all the definitions.
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 * Gather all possible definition objects.
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 }