Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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 static com.google.javascript.jscomp.CheckGlobalNames.NAME_DEFINED_LATE_WARNING;
20 import static com.google.javascript.jscomp.CheckGlobalNames.UNDEFINED_NAME_WARNING;
21 import static com.google.javascript.jscomp.CheckGlobalNames.STRICT_MODULE_DEP_QNAME;
22
23 import com.google.javascript.rhino.Node;
24
25 /**
26 * Tests for {@code CheckGlobalNames.java}.
27 *
28 * @author [email protected] (Nick Santos)
29 */
30 public class CheckGlobalNamesTest extends CompilerTestCase {
31
32 private boolean injectNamespace = false;
33
34 public CheckGlobalNamesTest() {
35 super("function alert() {}" +
36 "/** @constructor */ function Object(){}" +
37 "Object.prototype.hasOwnProperty = function() {};" +
38 "/** @constructor */ function Function(){}" +
39 "Function.prototype.call = function() {};");
40 }
41
42 @Override
43 protected CompilerPass getProcessor(final Compiler compiler) {
44 final CheckGlobalNames checkGlobalNames = new CheckGlobalNames(
45 compiler, CheckLevel.WARNING);
46 if (injectNamespace) {
47 return new CompilerPass() {
48 @Override
49 public void process(Node externs, Node js) {
50 checkGlobalNames.injectNamespace(
51 new GlobalNamespace(compiler, externs, js))
52 .process(externs, js);
53 }
54 };
55 } else {
56 return checkGlobalNames;
57 }
58 }
59
60 @Override
61 public void setUp() {
62 injectNamespace = false;
63 STRICT_MODULE_DEP_QNAME.level = CheckLevel.WARNING;
64 }
65
66 private static final String GET_NAMES =
67 "var a = {get d() {return 1}}; a.b = 3; a.c = {get e() {return 5}};";
68 private static final String SET_NAMES =
69 "var a = {set d(x) {}}; a.b = 3; a.c = {set e(y) {}};";
70 private static final String NAMES = "var a = {d: 1}; a.b = 3; a.c = {e: 5};";
71
72 public void testRefToDefinedProperties1() {
73 testSame(NAMES + "alert(a.b); alert(a.c.e);");
74 testSame(GET_NAMES + "alert(a.b); alert(a.c.e);");
75 testSame(SET_NAMES + "alert(a.b); alert(a.c.e);");
76 }
77
78 public void testRefToDefinedProperties2() {
79 testSame(NAMES + "a.x={}; alert(a.c);");
80 testSame(GET_NAMES + "a.x={}; alert(a.c);");
81 testSame(SET_NAMES + "a.x={}; alert(a.c);");
82 }
83
84 public void testRefToDefinedProperties3() {
85 testSame(NAMES + "alert(a.d);");
86 testSame(GET_NAMES + "alert(a.d);");
87 testSame(SET_NAMES + "alert(a.d);");
88 }
89
90 public void testRefToMethod1() {
91 testSame("function foo() {}; foo.call();");
92 }
93
94 public void testRefToMethod2() {
95 testSame("function foo() {}; foo.call.call();");
96 }
97
98 public void testCallUndefinedFunctionGivesNoWaring() {
99 // We don't bother checking undeclared variables--there's another
100 // pass that does this already.
101 testSame("foo();");
102 }
103
104 public void testRefToPropertyOfAliasedName() {
105 // this is OK, because "a" was aliased
106 testSame(NAMES + "alert(a); alert(a.x);");
107 }
108
109 public void testRefToUndefinedProperty1() {
110 testSame(NAMES + "alert(a.x);", UNDEFINED_NAME_WARNING);
111 }
112
113 public void testRefToUndefinedProperty2() {
114 testSame(NAMES + "a.x();", UNDEFINED_NAME_WARNING);
115 }
116
117 public void testRefToUndefinedProperty3() {
118 testSame(NAMES + "alert(a.c.x);", UNDEFINED_NAME_WARNING);
119 testSame(GET_NAMES + "alert(a.c.x);", UNDEFINED_NAME_WARNING);
120 testSame(SET_NAMES + "alert(a.c.x);", UNDEFINED_NAME_WARNING);
121 }
122
123 public void testRefToUndefinedProperty4() {
124 testSame(NAMES + "alert(a.d.x);");
125 testSame(GET_NAMES + "alert(a.d.x);");
126 testSame(SET_NAMES + "alert(a.d.x);");
127 }
128
129 public void testRefToDescendantOfUndefinedProperty1() {
130 testSame(NAMES + "var c = a.x.b;", UNDEFINED_NAME_WARNING);
131 }
132
133 public void testRefToDescendantOfUndefinedProperty2() {
134 testSame(NAMES + "a.x.b();", UNDEFINED_NAME_WARNING);
135 }
136
137 public void testRefToDescendantOfUndefinedProperty3() {
138 testSame(NAMES + "a.x.b = 3;", UNDEFINED_NAME_WARNING);
139 }
140
141 public void testUndefinedPrototypeMethodRefGivesNoWarning() {
142 testSame("function Foo() {} var a = new Foo(); a.bar();");
143 }
144
145 public void testComplexPropAssignGivesNoWarning() {
146 testSame("var a = {}; var b = a.b = 3;");
147 }
148
149 public void testTypedefGivesNoWarning() {
150 testSame("var a = {}; /** @typedef {number} */ a.b;");
151 }
152
153 public void testRefToDescendantOfUndefinedPropertyGivesCorrectWarning() {
154 testSame("", NAMES + "a.x.b = 3;", UNDEFINED_NAME_WARNING,
155 UNDEFINED_NAME_WARNING.format("a.x"));
156 }
157
158 public void testNamespaceInjection() {
159 injectNamespace = true;
160 testSame(NAMES + "var c = a.x.b;", UNDEFINED_NAME_WARNING);
161 }
162
163 public void testSuppressionOfUndefinedNamesWarning() {
164 testSame(new String[] {
165 NAMES +
166 "/** @constructor */ function Foo() { };" +
167 "/** @suppress {undefinedNames} */" +
168 "Foo.prototype.bar = function() {" +
169 " alert(a.x);" +
170 " alert(a.x.b());" +
171 " a.x();" +
172 " var c = a.x.b;" +
173 " var c = a.x.b();" +
174 " a.x.b();" +
175 " a.x.b = 3;" +
176 "};",
177 });
178 }
179
180 public void testNoWarningForSimpleVarModuleDep1() {
181 testSame(createModuleChain(
182 NAMES,
183 "var c = a;"
184 ));
185 }
186
187 public void testNoWarningForSimpleVarModuleDep2() {
188 testSame(createModuleChain(
189 "var c = a;",
190 NAMES
191 ));
192 }
193 public void testNoWarningForGoodModuleDep1() {
194 testSame(createModuleChain(
195 NAMES,
196 "var c = a.b;"
197 ));
198 }
199
200 public void testBadModuleDep1() {
201 testSame(createModuleChain(
202 "var c = a.b;",
203 NAMES
204 ), STRICT_MODULE_DEP_QNAME);
205 }
206
207 public void testBadModuleDep2() {
208 testSame(createModuleStar(
209 NAMES,
210 "a.xxx = 3;",
211 "var x = a.xxx;"
212 ), STRICT_MODULE_DEP_QNAME);
213 }
214
215 public void testSelfModuleDep() {
216 testSame(createModuleChain(
217 NAMES + "var c = a.b;"
218 ));
219 }
220
221 public void testUndefinedModuleDep1() {
222 testSame(createModuleChain(
223 "var c = a.xxx;",
224 NAMES
225 ), UNDEFINED_NAME_WARNING);
226 }
227
228 public void testLateDefinedName1() {
229 testSame("x.y = {}; var x = {};", NAME_DEFINED_LATE_WARNING);
230 }
231
232 public void testLateDefinedName2() {
233 testSame("var x = {}; x.y.z = {}; x.y = {};", NAME_DEFINED_LATE_WARNING);
234 }
235
236 public void testLateDefinedName3() {
237 testSame("var x = {}; x.y.z = {}; x.y = {z: {}};",
238 NAME_DEFINED_LATE_WARNING);
239 }
240
241 public void testLateDefinedName4() {
242 testSame("var x = {}; x.y.z.bar = {}; x.y = {z: {}};",
243 NAME_DEFINED_LATE_WARNING);
244 }
245
246 public void testLateDefinedName5() {
247 testSame("var x = {}; /** @typedef {number} */ x.y.z; x.y = {};",
248 NAME_DEFINED_LATE_WARNING);
249 }
250
251 public void testLateDefinedName6() {
252 testSame(
253 "var x = {}; x.y.prototype.z = 3;" +
254 "/** @constructor */ x.y = function() {};",
255 NAME_DEFINED_LATE_WARNING);
256 }
257
258 public void testOkLateDefinedName1() {
259 testSame("function f() { x.y = {}; } var x = {};");
260 }
261
262 public void testOkLateDefinedName2() {
263 testSame("var x = {}; function f() { x.y.z = {}; } x.y = {};");
264 }
265
266 public void testPathologicalCaseThatsOkAnyway() {
267 testSame(
268 "var x = {};" +
269 "switch (x) { " +
270 " default: x.y.z = {}; " +
271 " case (x.y = {}): break;" +
272 "}", NAME_DEFINED_LATE_WARNING);
273 }
274
275 public void testOkGlobalDeclExpr() {
276 testSame("var x = {}; /** @type {string} */ x.foo;");
277 }
278
279 public void testBadInterfacePropRef() {
280 testSame(
281 "/** @interface */ function F() {}" +
282 "F.bar();",
283 UNDEFINED_NAME_WARNING);
284 }
285
286 public void testInterfaceFunctionPropRef() {
287 testSame(
288 "/** @interface */ function F() {}" +
289 "F.call(); F.hasOwnProperty('z');");
290 }
291
292 public void testObjectPrototypeProperties() {
293 testSame("var x = {}; var y = x.hasOwnProperty('z');");
294 }
295
296 public void testCustomObjectPrototypeProperties() {
297 testSame("Object.prototype.seal = function() {};" +
298 "var x = {}; x.seal();");
299 }
300
301 public void testFunctionPrototypeProperties() {
302 testSame("var x = {}; var y = x.hasOwnProperty('z');");
303 }
304
305 public void testIndirectlyDeclaredProperties() {
306 testSame(
307 "Function.prototype.inherits = function(ctor) {" +
308 " this.superClass_ = ctor;" +
309 "};" +
310 "/** @constructor */ function Foo() {}" +
311 "Foo.prototype.bar = function() {};" +
312 "/** @constructor */ function SubFoo() {}" +
313 "SubFoo.inherits(Foo);" +
314 "SubFoo.superClass_.bar();");
315 }
316
317 public void testGoogInheritsAlias() {
318 testSame(
319 "Function.prototype.inherits = function(ctor) {" +
320 " this.superClass_ = ctor;" +
321 "};" +
322 "/** @constructor */ function Foo() {}" +
323 "Foo.prototype.bar = function() {};" +
324 "/** @constructor */ function SubFoo() {}" +
325 "SubFoo.inherits(Foo);" +
326 "SubFoo.superClass_.bar();");
327 }
328
329 public void testGoogInheritsAlias2() {
330 testSame(
331 CompilerTypeTestCase.CLOSURE_DEFS +
332 "/** @constructor */ function Foo() {}" +
333 "Foo.prototype.bar = function() {};" +
334 "/** @constructor */ function SubFoo() {}" +
335 "goog.inherits(SubFoo, Foo);" +
336 "SubFoo.superClazz();",
337 UNDEFINED_NAME_WARNING);
338 }
339 }