Debian Codegraph @

test/com/google/javascript/jscomp/SimpleFunctionAliasAnalysisTest.java

raw symbols SCIP
1 /*
2 * Copyright 2010 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 package com.google.javascript.jscomp;
17
18 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
19 import com.google.javascript.rhino.Node;
20
21 /**
22 * Tests for {@link SimpleFunctionAliasAnalysis}.
23 *
24 * @author [email protected] (Devin Coughlin)
25 */
26 public class SimpleFunctionAliasAnalysisTest extends CompilerTestCase {
27
28 private SimpleFunctionAliasAnalysis analysis;
29
30 private Compiler lastCompiler;
31
32 @Override
33 protected CompilerPass getProcessor(final Compiler compiler) {
34 return new CompilerPass() {
35
36 @Override
37 public void process(Node externs, Node root) {
38 SimpleDefinitionFinder finder = new SimpleDefinitionFinder(compiler);
39 finder.process(externs, root);
40
41 analysis = new SimpleFunctionAliasAnalysis();
42
43 analysis.analyze(finder);
44
45 lastCompiler = compiler;
46 }
47 };
48 }
49
50 public void testFunctionGetIsAliased() {
51 // Aliased by VAR assignment
52 String source =
53 "function A(){};\n" +
54 "var ns = {};\n" +
55 "ns.B = function() {};\n" +
56 "var C = function() {}\n" +
57 "var D = function() {}\n" +
58 "var aliasA = A;\n" +
59 "var aliasB = ns.B;\n" +
60 "var aliasC = C;\n" +
61 "D();";
62
63 compileAndRun(source);
64
65 assertFunctionAliased(true, "A");
66 assertFunctionAliased(true, "ns.B");
67 assertFunctionAliased(true, "C");
68 assertFunctionAliased(false, "D");
69
70 // Aliased by normal assignment
71 source =
72 "function A(){};\n" +
73 "var ns = {};\n" +
74 "ns.B = function() {};\n" +
75 "var C = function() {}\n" +
76 "ns.D = function() {}\n" +
77 "var aliasA;\n" +
78 "aliasA = A;\n" +
79 "var aliasB = {};\n" +
80 "aliasB.foo = ns.B;\n" +
81 "var aliasC;\n" +
82 "aliasC = C;\n" +
83 "ns.D();";
84
85 compileAndRun(source);
86
87 assertFunctionAliased(true, "A");
88 assertFunctionAliased(true, "ns.B");
89 assertFunctionAliased(true, "C");
90 assertFunctionAliased(false, "ns.D");
91
92 // Aliased by passing as parameter
93 source =
94 "function A(){};\n" +
95 "var ns = {};\n" +
96 "ns.B = function() {};\n" +
97 "var C = function() {}\n" +
98 "function D() {}\n" +
99 "var foo = function(a) {}\n" +
100 "foo(A);\n" +
101 "foo(ns.B)\n" +
102 "foo(C);\n" +
103 "D();";
104
105 compileAndRun(source);
106
107 assertFunctionAliased(true, "A");
108 assertFunctionAliased(true, "ns.B");
109 assertFunctionAliased(true, "C");
110 assertFunctionAliased(false, "D");
111
112 // Not aliased by being target of call
113 source =
114 "function A(){};\n" +
115 "var ns = {};\n" +
116 "ns.B = function() {};\n" +
117 "var C = function() {}\n" +
118 "A();\n" +
119 "ns.B();\n" +
120 "C();\n";
121
122 compileAndRun(source);
123
124 assertFunctionAliased(false, "A");
125 assertFunctionAliased(false, "ns.B");
126 assertFunctionAliased(false, "C");
127
128 // Not aliased by GET{PROP,ELEM}
129 source =
130 "function A(){};\n" +
131 "var ns = {};\n" +
132 "ns.B = function() {};\n" +
133 "var C = function() {}\n" +
134 "A.foo;\n" +
135 "ns.B.prototype;\n" +
136 "C[0];\n";
137
138 compileAndRun(source);
139
140 assertFunctionAliased(false, "A");
141 assertFunctionAliased(false, "ns.B");
142 assertFunctionAliased(false, "C");
143 }
144
145 public void testFunctionGetIsExposedToCallOrApply() {
146 // Exposed to call
147 String source =
148 "function A(){};\n" +
149 "function B(){};\n" +
150 "function C(){};\n" +
151 "var x;\n" +
152 "A.call(x);\n" +
153 "B.apply(x);\n" +
154 "C();\n";
155
156 compileAndRun(source);
157
158 assertFunctionExposedToCallOrApply(true, "A");
159 assertFunctionExposedToCallOrApply(true, "B");
160 assertFunctionExposedToCallOrApply(false, "C");
161
162 source =
163 "var ns = {};" +
164 "ns.A = function(){};\n" +
165 "ns.B = function(){};\n" +
166 "ns.C = function(){};\n" +
167 "var x;\n" +
168 "ns.A.call(x);\n" +
169 "ns.B.apply(x);\n" +
170 "ns.C();\n";
171
172 compileAndRun(source);
173
174 assertFunctionExposedToCallOrApply(true, "ns.A");
175 assertFunctionExposedToCallOrApply(true, "ns.B");
176 assertFunctionExposedToCallOrApply(false, "ns.C");
177 }
178
179 private void assertFunctionAliased(boolean aliasStatus,
180 String functionName) {
181 Node function = findFunction(functionName);
182
183 assertEquals(aliasStatus, analysis.isAliased(function));
184 }
185
186 private void assertFunctionExposedToCallOrApply(boolean exposure,
187 String functionName) {
188 Node function = findFunction(functionName);
189
190 assertEquals(exposure, analysis.isExposedToCallOrApply(function));
191 }
192
193 private void compileAndRun(String source) {
194 testSame(source, source, null);
195 }
196
197 private Node findFunction(String name) {
198 FunctionFinder f = new FunctionFinder(name);
199 new NodeTraversal(lastCompiler, f).traverse(lastCompiler.jsRoot);
200 assertNotNull("Couldn't find " + name, f.found);
201 return f.found;
202 }
203
204 /**
205 * Quick Traversal to find a given function in the AST.
206 */
207 private class FunctionFinder extends AbstractPostOrderCallback {
208 Node found = null;
209 final String target;
210
211 FunctionFinder(String target) {
212 this.target = target;
213 }
214
215 @Override
216 public void visit(NodeTraversal t, Node n, Node parent) {
217 if (n.isFunction()
218 && target.equals(NodeUtil.getFunctionName(n))) {
219 found = n;
220 }
221 }
222 }
223 }