Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2006 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.CollapseProperties.UNSAFE_THIS;
20
21 import com.google.javascript.rhino.Node;
22
23 /**
24 * Tests for {@link CollapseProperties}.
25 *
26 */
27 public class CollapsePropertiesTest extends CompilerTestCase {
28
29 private static String EXTERNS =
30 "var window; function alert(s) {} function parseInt(s) {}" +
31 "/** @constructor */ function String() {}";
32
33 private boolean collapsePropertiesOnExternTypes = false;
34
35 public CollapsePropertiesTest() {
36 super(EXTERNS);
37 }
38
39 @Override public CompilerPass getProcessor(Compiler compiler) {
40 return new CollapseProperties(
41 compiler, collapsePropertiesOnExternTypes, true);
42 }
43
44 @Override
45 public void setUp() {
46 enableLineNumberCheck(true);
47 enableNormalize(true);
48 }
49
50 @Override public int getNumRepetitions() {
51 return 1;
52 }
53
54 public void testCollapse() {
55 test("var a = {}; a.b = {}; var c = a.b;",
56 "var a$b = {}; var c = a$b");
57 }
58
59 public void testMultiLevelCollapse() {
60 test("var a = {}; a.b = {}; a.b.c = {}; var d = a.b.c;",
61 "var a$b$c = {}; var d = a$b$c;");
62 }
63
64 public void testDecrement() {
65 test("var a = {}; a.b = 5; a.b--; a.b = 5",
66 "var a$b = 5; a$b--; a$b = 5");
67 }
68
69 public void testIncrement() {
70 test("var a = {}; a.b = 5; a.b++; a.b = 5",
71 "var a$b = 5; a$b++; a$b = 5");
72 }
73
74 public void testObjLitDeclaration() {
75 test("var a = {b: {}, c: {}}; var d = a.b; var e = a.c",
76 "var a$b = {}; var a$c = {}; var d = a$b; var e = a$c");
77 }
78
79 public void testObjLitDeclarationWithGet1() {
80 testSame("var a = {get b(){}};");
81 }
82
83 public void testObjLitDeclarationWithGet2() {
84 test("var a = {b: {}, get c(){}}; var d = a.b; var e = a.c",
85 "var a$b = {};var a = {get c(){}};var d = a$b; var e = a.c");
86 }
87
88 public void testObjLitDeclarationWithGet3() {
89 test("var a = {b: {get c() { return 3; }}};",
90 "var a$b = {get c() { return 3; }};");
91 }
92
93 public void testObjLitDeclarationWithSet1() {
94 testSame("var a = {set b(a){}};");
95 }
96
97 public void testObjLitDeclarationWithSet2() {
98 test("var a = {b: {}, set c(a){}}; var d = a.b; var e = a.c",
99 "var a$b = {};var a = {set c(a){}};var d = a$b; var e = a.c");
100 }
101
102 public void testObjLitDeclarationWithSet3() {
103 test("var a = {b: {set c(d) {}}};",
104 "var a$b = {set c(d) {}};");
105 }
106
107 public void testObjLitDeclarationWithGetAndSet1() {
108 test("var a = {b: {get c() { return 3; },set c(d) {}}};",
109 "var a$b = {get c() { return 3; },set c(d) {}};");
110 }
111
112 public void testObjLitDeclarationWithDuplicateKeys() {
113 test("var a = {b: 0, b: 1}; var c = a.b;",
114 "var a$b = 0; var a$b = 1; var c = a$b;",
115 SyntacticScopeCreator.VAR_MULTIPLY_DECLARED_ERROR);
116 }
117
118 public void testObjLitAssignmentDepth1() {
119 test("var a = {b: {}, c: {}}; var d = a.b; var e = a.c",
120 "var a$b = {}; var a$c = {}; var d = a$b; var e = a$c");
121 }
122
123 public void testObjLitAssignmentDepth2() {
124 test("var a = {}; a.b = {c: {}, d: {}}; var e = a.b.c; var f = a.b.d",
125 "var a$b$c = {}; var a$b$d = {}; var e = a$b$c; var f = a$b$d");
126 }
127
128 public void testObjLitAssignmentDepth3() {
129 test("var a = {}; a.b = {}; a.b.c = {d: 1, e: 2}; var f = a.b.c.d",
130 "var a$b$c$d = 1; var a$b$c$e = 2; var f = a$b$c$d");
131 }
132
133 public void testObjLitAssignmentDepth4() {
134 test("var a = {}; a.b = {}; a.b.c = {}; a.b.c.d = {e: 1, f: 2}; " +
135 "var g = a.b.c.d.e",
136 "var a$b$c$d$e = 1; var a$b$c$d$f = 2; var g = a$b$c$d$e");
137 }
138
139 public void testGlobalObjectDeclaredToPreserveItsPreviousValue1() {
140 test("var a = a ? a : {}; a.c = 1;",
141 "var a = a ? a : {}; var a$c = 1;");
142 }
143
144 public void testGlobalObjectDeclaredToPreserveItsPreviousValue2() {
145 test("var a = a || {}; a.c = 1;",
146 "var a = a || {}; var a$c = 1;");
147 }
148
149 public void testGlobalObjectDeclaredToPreserveItsPreviousValue3() {
150 test("var a = a || {get b() {}}; a.c = 1;",
151 "var a = a || {get b() {}}; var a$c = 1;");
152 }
153
154 public void testGlobalObjectNameInBooleanExpressionDepth1_1() {
155 test("var a = {b: 0}; a.c = 1; if (a) x();",
156 "var a$b = 0; var a = {}; var a$c = 1; if (a) x();");
157 }
158
159 public void testGlobalObjectNameInBooleanExpressionDepth1_2() {
160 test("var a = {b: 0}; a.c = 1; if (!(a && a.c)) x();",
161 "var a$b = 0; var a = {}; var a$c = 1; if (!(a && a$c)) x();");
162 }
163
164 public void testGlobalObjectNameInBooleanExpressionDepth1_3() {
165 test("var a = {b: 0}; a.c = 1; while (a || a.c) x();",
166 "var a$b = 0; var a = {}; var a$c = 1; while (a || a$c) x();");
167 }
168
169 public void testGlobalObjectNameInBooleanExpressionDepth1_4() {
170 testSame("var a = {}; a.c = 1; var d = a || {}; a.c;");
171 }
172
173 public void testGlobalObjectNameInBooleanExpressionDepth1_5() {
174 testSame("var a = {}; a.c = 1; var d = a.c || a; a.c;");
175 }
176
177 public void testGlobalObjectNameInBooleanExpressionDepth1_6() {
178 test("var a = {b: 0}; a.c = 1; var d = !(a.c || a); a.c;",
179 "var a$b = 0; var a = {}; var a$c = 1; var d = !(a$c || a); a$c;");
180 }
181
182 public void testGlobalObjectNameInBooleanExpressionDepth2() {
183 test("var a = {b: {}}; a.b.c = 1; if (a.b) x(a.b.c);",
184 "var a$b = {}; var a$b$c = 1; if (a$b) x(a$b$c);");
185 }
186
187 public void testGlobalObjectNameInBooleanExpressionDepth3() {
188 // TODO(user): Make CollapseProperties even more aggressive so that
189 // a$b.z gets collapsed. Right now, it doesn't get collapsed because the
190 // expression (a.b && a.b.c) could return a.b. But since it returns a.b iff
191 // a.b *is* safely collapsible, the Boolean logic should be smart enough to
192 // only consider the right side of the && as aliasing.
193 test("var a = {}; a.b = {}; /** @constructor */ a.b.c = function(){};" +
194 " a.b.z = 1; var d = a.b && a.b.c;",
195 "var a$b = {}; var a$b$c = function(){};" +
196 " a$b.z = 1; var d = a$b && a$b$c;", null,
197 CollapseProperties.UNSAFE_NAMESPACE_WARNING);
198 }
199
200 public void testGlobalFunctionNameInBooleanExpressionDepth1() {
201 test("function a() {} a.c = 1; if (a) x(a.c);",
202 "function a() {} var a$c = 1; if (a) x(a$c);");
203 }
204
205 public void testGlobalFunctionNameInBooleanExpressionDepth2() {
206 test("var a = {b: function(){}}; a.b.c = 1; if (a.b) x(a.b.c);",
207 "var a$b = function(){}; var a$b$c = 1; if (a$b) x(a$b$c);");
208 }
209
210 public void testAliasCreatedForObjectDepth1_1() {
211 // An object's properties are not collapsed if the object is referenced
212 // in a such a way that an alias is created for it.
213 testSame("var a = {b: 0}; var c = a; c.b = 1; a.b == c.b;");
214 }
215
216 public void testAliasCreatedForObjectDepth1_2() {
217 testSame("var a = {b: 0}; f(a); a.b;");
218 }
219
220 public void testAliasCreatedForObjectDepth1_3() {
221 testSame("var a = {b: 0}; new f(a); a.b;");
222 }
223
224 public void testAliasCreatedForObjectDepth2_1() {
225 test("var a = {}; a.b = {c: 0}; var d = a.b; a.b.c == d.c;",
226 "var a$b = {c: 0}; var d = a$b; a$b.c == d.c;");
227 }
228
229 public void testAliasCreatedForObjectDepth2_2() {
230 test("var a = {}; a.b = {c: 0}; for (var p in a.b) { e(a.b[p]); }",
231 "var a$b = {c: 0}; for (var p in a$b) { e(a$b[p]); }");
232 }
233
234 public void testAliasCreatedForEnumDepth1_1() {
235 // An enum's values are always collapsed, even if the enum object is
236 // referenced in a such a way that an alias is created for it.
237 test("/** @enum */ var a = {b: 0}; var c = a; c.b = 1; a.b != c.b;",
238 "var a$b = 0; var a = {b: a$b}; var c = a; c.b = 1; a$b != c.b;");
239 }
240
241 public void testAliasCreatedForEnumDepth1_2() {
242 test("/** @enum */ var a = {b: 0}; f(a); a.b;",
243 "var a$b = 0; var a = {b: a$b}; f(a); a$b;");
244 }
245
246 public void testAliasCreatedForEnumDepth1_3() {
247 test("/** @enum */ var a = {b: 0}; new f(a); a.b;",
248 "var a$b = 0; var a = {b: a$b}; new f(a); a$b;");
249 }
250
251 public void testAliasCreatedForEnumDepth1_4() {
252 test("/** @enum */ var a = {b: 0}; for (var p in a) { f(a[p]); }",
253 "var a$b = 0; var a = {b: a$b}; for (var p in a) { f(a[p]); }");
254 }
255
256 public void testAliasCreatedForEnumDepth2_1() {
257 test("var a = {}; /** @enum */ a.b = {c: 0};" +
258 "var d = a.b; d.c = 1; a.b.c != d.c;",
259 "var a$b$c = 0; var a$b = {c: a$b$c};" +
260 "var d = a$b; d.c = 1; a$b$c != d.c;");
261 }
262
263 public void testAliasCreatedForEnumDepth2_2() {
264 test("var a = {}; /** @enum */ a.b = {c: 0};" +
265 "for (var p in a.b) { f(a.b[p]); }",
266 "var a$b$c = 0; var a$b = {c: a$b$c};" +
267 "for (var p in a$b) { f(a$b[p]); }");
268 }
269
270 public void testAliasCreatedForEnumDepth2_3() {
271 test("var a = {}; var d = a; /** @enum */ a.b = {c: 0};" +
272 "for (var p in a.b) { f(a.b[p]); }",
273 "var a = {}; var d = a; var a$b$c = 0; var a$b = {c: a$b$c};" +
274 "for (var p in a$b) { f(a$b[p]); }",
275 null, CollapseProperties.UNSAFE_NAMESPACE_WARNING);
276 }
277
278 public void testAliasCreatedForEnumOfObjects() {
279 test("var a = {}; " +
280 "/** @enum {Object} */ a.b = {c: {d: 1}}; a.b.c;" +
281 "searchEnum(a.b);",
282 "var a$b$c = {d: 1};var a$b = {c: a$b$c}; a$b$c; " +
283 "searchEnum(a$b)");
284 }
285
286 public void testAliasCreatedForEnumOfObjects2() {
287 test("var a = {}; " +
288 "/** @enum {Object} */ a.b = {c: {d: 1}}; a.b.c.d;" +
289 "searchEnum(a.b);",
290 "var a$b$c = {d: 1};var a$b = {c: a$b$c}; a$b$c.d; " +
291 "searchEnum(a$b)");
292 }
293
294 public void testAliasCreatedForPropertyOfEnumOfObjects() {
295 test("var a = {}; " +
296 "/** @enum {Object} */ a.b = {c: {d: 1}}; a.b.c;" +
297 "searchEnum(a.b.c);",
298 "var a$b$c = {d: 1}; a$b$c; searchEnum(a$b$c);");
299 }
300
301 public void testAliasCreatedForPropertyOfEnumOfObjects2() {
302 test("var a = {}; " +
303 "/** @enum {Object} */ a.b = {c: {d: 1}}; a.b.c.d;" +
304 "searchEnum(a.b.c);",
305 "var a$b$c = {d: 1}; a$b$c.d; searchEnum(a$b$c);");
306 }
307
308 public void testMisusedEnumTag() {
309 testSame("var a = {}; var d = a; a.b = function() {};" +
310 "/** @enum */ a.b.c = 0; a.b.c;");
311 }
312
313 public void testMisusedConstructorTag() {
314 testSame("var a = {}; var d = a; a.b = function() {};" +
315 "/** @constructor */ a.b.c = 0; a.b.c;");
316 }
317
318 public void testAliasCreatedForFunctionDepth1_1() {
319 testSame("var a = function(){}; a.b = 1; var c = a; c.b = 2; a.b != c.b;");
320 }
321
322 public void testAliasCreatedForCtorDepth1_1() {
323 // A constructor's properties *are* collapsed even if the function is
324 // referenced in a such a way that an alias is created for it,
325 // since a function with custom properties is considered a class and its
326 // non-prototype properties are considered static methods and variables.
327 // People don't typically iterate through static members of a class or
328 // refer to them using an alias for the class name.
329 test("/** @constructor */ var a = function(){}; a.b = 1; " +
330 "var c = a; c.b = 2; a.b != c.b;",
331 "var a = function(){}; var a$b = 1; var c = a; c.b = 2; a$b != c.b;");
332 }
333
334 public void testAliasCreatedForFunctionDepth1_2() {
335 testSame("var a = function(){}; a.b = 1; f(a); a.b;");
336 }
337
338 public void testAliasCreatedForCtorDepth1_2() {
339 test("/** @constructor */ var a = function(){}; a.b = 1; f(a); a.b;",
340 "var a = function(){}; var a$b = 1; f(a); a$b;");
341 }
342
343 public void testAliasCreatedForFunctionDepth1_3() {
344 testSame("var a = function(){}; a.b = 1; new f(a); a.b;");
345 }
346
347 public void testAliasCreatedForCtorDepth1_3() {
348 test("/** @constructor */ var a = function(){}; a.b = 1; new f(a); a.b;",
349 "var a = function(){}; var a$b = 1; new f(a); a$b;");
350 }
351
352 public void testAliasCreatedForFunctionDepth2() {
353 test(
354 "var a = {}; a.b = function() {}; a.b.c = 1; var d = a.b;" +
355 "a.b.c != d.c;",
356 "var a$b = function() {}; a$b.c = 1; var d = a$b;" +
357 "a$b.c != d.c;");
358 }
359
360 public void testAliasCreatedForCtorDepth2() {
361 test("var a = {}; /** @constructor */ a.b = function() {}; " +
362 "a.b.c = 1; var d = a.b;" +
363 "a.b.c != d.c;",
364 "var a$b = function() {}; var a$b$c = 1; var d = a$b;" +
365 "a$b$c != d.c;");
366 }
367
368 public void testAliasCreatedForClassDepth1_1() {
369 // A class's name is always collapsed, even if one of its prefixes is
370 // referenced in a such a way that an alias is created for it.
371 test("var a = {}; /** @constructor */ a.b = function(){};" +
372 "var c = a; c.b = 0; a.b != c.b;",
373 "var a = {}; var a$b = function(){};" +
374 "var c = a; c.b = 0; a$b != c.b;", null,
375 CollapseProperties.UNSAFE_NAMESPACE_WARNING);
376 }
377
378 public void testAliasCreatedForClassDepth1_2() {
379 test("var a = {}; /** @constructor */ a.b = function(){}; f(a); a.b;",
380 "var a = {}; var a$b = function(){}; f(a); a$b;",
381 null, CollapseProperties.UNSAFE_NAMESPACE_WARNING);
382 }
383
384 public void testAliasCreatedForClassDepth1_3() {
385 test("var a = {}; /** @constructor */ a.b = function(){}; new f(a); a.b;",
386 "var a = {}; var a$b = function(){}; new f(a); a$b;",
387 null, CollapseProperties.UNSAFE_NAMESPACE_WARNING);
388 }
389
390 public void testAliasCreatedForClassDepth2_1() {
391 test("var a = {}; a.b = {}; /** @constructor */ a.b.c = function(){};" +
392 "var d = a.b; a.b.c != d.c;",
393 "var a$b = {}; var a$b$c = function(){};" +
394 "var d = a$b; a$b$c != d.c;",
395 null, CollapseProperties.UNSAFE_NAMESPACE_WARNING);
396 }
397
398 public void testAliasCreatedForClassDepth2_2() {
399 test("var a = {}; a.b = {}; /** @constructor */ a.b.c = function(){};" +
400 "f(a.b); a.b.c;",
401 "var a$b = {}; var a$b$c = function(){}; f(a$b); a$b$c;",
402 null, CollapseProperties.UNSAFE_NAMESPACE_WARNING);
403 }
404
405 public void testAliasCreatedForClassDepth2_3() {
406 test("var a = {}; a.b = {}; /** @constructor */ a.b.c = function(){};" +
407 "new f(a.b); a.b.c;",
408 "var a$b = {}; var a$b$c = function(){}; new f(a$b); a$b$c;",
409 null, CollapseProperties.UNSAFE_NAMESPACE_WARNING);
410 }
411
412 public void testAliasCreatedForClassProperty() {
413 test("var a = {}; /** @constructor */ a.b = function(){};" +
414 "a.b.c = {d: 3}; new f(a.b.c); a.b.c.d;",
415 "var a$b = function(){}; var a$b$c = {d:3}; new f(a$b$c); a$b$c.d;");
416 }
417
418 public void testNestedObjLit() {
419 test("var a = {}; a.b = {f: 0, c: {d: 1}}; var e = a.b.c.d",
420 "var a$b$f = 0; var a$b$c$d = 1; var e = a$b$c$d;");
421 }
422
423 public void testObjLitDeclarationUsedInSameVarList() {
424 // The collapsed properties must defined in the same place in the var list
425 // where they were originally defined (and not, for example, at the end).
426 test("var a = {b: {}, c: {}}; var d = a.b; var e = a.c;",
427 "var a$b = {}; var a$c = {}; var d = a$b; var e = a$c;");
428 }
429
430 public void testPropGetInsideAnObjLit() {
431 test("var x = {}; x.y = 1; var a = {}; a.b = {c: x.y}",
432 "var x$y = 1; var a$b$c = x$y;");
433 }
434
435 public void testObjLitWithQuotedKeyThatDoesNotGetRead() {
436 test("var a = {}; a.b = {c: 0, 'd': 1}; var e = a.b.c;",
437 "var a$b$c = 0; var a$b$d = 1; var e = a$b$c;");
438 }
439
440 public void testObjLitWithQuotedKeyThatGetsRead() {
441 test("var a = {}; a.b = {c: 0, 'd': 1}; var e = a.b['d'];",
442 "var a$b = {c: 0, 'd': 1}; var e = a$b['d'];");
443 }
444
445 public void testFunctionWithQuotedPropertyThatDoesNotGetRead() {
446 test("var a = {}; a.b = function() {}; a.b['d'] = 1;",
447 "var a$b = function() {}; a$b['d'] = 1;");
448 }
449
450 public void testFunctionWithQuotedPropertyThatGetsRead() {
451 test("var a = {}; a.b = function() {}; a.b['d'] = 1; f(a.b['d']);",
452 "var a$b = function() {}; a$b['d'] = 1; f(a$b['d']);");
453 }
454
455 public void testObjLitAssignedToMultipleNames1() {
456 // An object literal that's assigned to multiple names isn't collapsed.
457 testSame("var a = b = {c: 0, d: 1}; var e = a.c; var f = b.d;");
458 }
459
460 public void testObjLitAssignedToMultipleNames2() {
461 testSame("a = b = {c: 0, d: 1}; var e = a.c; var f = b.d;");
462 }
463
464 public void testObjLitRedefinedInGlobalScope() {
465 testSame("a = {b: 0}; a = {c: 1}; var d = a.b; var e = a.c;");
466 }
467
468 public void testObjLitRedefinedInLocalScope() {
469 test("var a = {}; a.b = {c: 0}; function d() { a.b = {c: 1}; } e(a.b.c);",
470 "var a$b = {c: 0}; function d() { a$b = {c: 1}; } e(a$b.c);");
471 }
472
473 public void testObjLitAssignedInTernaryExpression1() {
474 testSame("a = x ? {b: 0} : d; var c = a.b;");
475 }
476
477 public void testObjLitAssignedInTernaryExpression2() {
478 testSame("a = x ? {b: 0} : {b: 1}; var c = a.b;");
479 }
480
481 public void testGlobalVarSetToObjLitConditionally1() {
482 testSame("var a; if (x) a = {b: 0}; var c = x ? a.b : 0;");
483 }
484
485 public void testGlobalVarSetToObjLitConditionally1b() {
486 test("if (x) var a = {b: 0}; var c = x ? a.b : 0;",
487 "if (x) var a$b = 0; var c = x ? a$b : 0;");
488 }
489
490 public void testGlobalVarSetToObjLitConditionally2() {
491 test("if (x) var a = {b: 0}; var c = a.b; var d = a.c;",
492 "if (x){ var a$b = 0; var a = {}; }var c = a$b; var d = a.c;");
493 }
494
495 public void testGlobalVarSetToObjLitConditionally3() {
496 testSame("var a; if (x) a = {b: 0}; else a = {b: 1}; var c = a.b;");
497 }
498
499 public void testObjectPropertySetToObjLitConditionally() {
500 test("var a = {}; if (x) a.b = {c: 0}; var d = a.b ? a.b.c : 0;",
501 "if (x){ var a$b$c = 0; var a$b = {} } var d = a$b ? a$b$c : 0;");
502 }
503
504 public void testFunctionPropertySetToObjLitConditionally() {
505 test("function a() {} if (x) a.b = {c: 0}; var d = a.b ? a.b.c : 0;",
506 "function a() {} if (x){ var a$b$c = 0; var a$b = {} }" +
507 "var d = a$b ? a$b$c : 0;");
508 }
509
510 public void testPrototypePropertySetToAnObjectLiteral() {
511 test("var a = {b: function(){}}; a.b.prototype.c = {d: 0};",
512 "var a$b = function(){}; a$b.prototype.c = {d: 0};");
513 }
514
515 public void testObjectPropertyResetInLocalScope() {
516 test("var z = {}; z.a = 0; function f() {z.a = 5; return z.a}",
517 "var z$a = 0; function f() {z$a = 5; return z$a}");
518 }
519
520 public void testFunctionPropertyResetInLocalScope() {
521 test("function z() {} z.a = 0; function f() {z.a = 5; return z.a}",
522 "function z() {} var z$a = 0; function f() {z$a = 5; return z$a}");
523 }
524
525 public void testNamespaceResetInGlobalScope1() {
526 test("var a = {}; /** @constructor */a.b = function() {}; a = {};",
527 "var a = {}; var a$b = function() {}; a = {};",
528 null, CollapseProperties.NAMESPACE_REDEFINED_WARNING);
529 }
530
531 public void testNamespaceResetInGlobalScope2() {
532 test("var a = {}; a = {}; /** @constructor */a.b = function() {};",
533 "var a = {}; a = {}; var a$b = function() {};",
534 null, CollapseProperties.NAMESPACE_REDEFINED_WARNING);
535 }
536
537 public void testNamespaceResetInLocalScope1() {
538 test("var a = {}; /** @constructor */a.b = function() {};" +
539 " function f() { a = {}; }",
540 "var a = {};var a$b = function() {};" +
541 " function f() { a = {}; }",
542 null, CollapseProperties.NAMESPACE_REDEFINED_WARNING);
543 }
544
545 public void testNamespaceResetInLocalScope2() {
546 test("var a = {}; function f() { a = {}; }" +
547 " /** @constructor */a.b = function() {};",
548 "var a = {}; function f() { a = {}; }" +
549 " var a$b = function() {};",
550 null, CollapseProperties.NAMESPACE_REDEFINED_WARNING);
551 }
552
553 public void testNamespaceDefinedInLocalScope() {
554 test("var a = {}; (function() { a.b = {}; })();" +
555 " /** @constructor */a.b.c = function() {};",
556 "var a$b; (function() { a$b = {}; })(); var a$b$c = function() {};");
557 }
558
559 public void testAddPropertyToObjectInLocalScopeDepth1() {
560 test("var a = {b: 0}; function f() { a.c = 5; return a.c; }",
561 "var a$b = 0; var a$c; function f() { a$c = 5; return a$c; }");
562 }
563
564 public void testAddPropertyToObjectInLocalScopeDepth2() {
565 test("var a = {}; a.b = {}; (function() {a.b.c = 0;})(); x = a.b.c;",
566 "var a$b$c; (function() {a$b$c = 0;})(); x = a$b$c;");
567 }
568
569 public void testAddPropertyToFunctionInLocalScopeDepth1() {
570 test("function a() {} function f() { a.c = 5; return a.c; }",
571 "function a() {} var a$c; function f() { a$c = 5; return a$c; }");
572 }
573
574 public void testAddPropertyToFunctionInLocalScopeDepth2() {
575 test("var a = {}; a.b = function() {}; function f() {a.b.c = 0;}",
576 "var a$b = function() {}; var a$b$c; function f() {a$b$c = 0;}");
577 }
578
579 public void testAddPropertyToUncollapsibleObjectInLocalScopeDepth1() {
580 testSame("var a = {}; var c = a; (function() {a.b = 0;})(); a.b;");
581 }
582
583 public void testAddPropertyToUncollapsibleFunctionInLocalScopeDepth1() {
584 testSame("function a() {} var c = a; (function() {a.b = 0;})(); a.b;");
585 }
586
587 public void testAddPropertyToUncollapsibleNamedCtorInLocalScopeDepth1() {
588 testSame(
589 "/** @constructor */ function a() {} var a$b; var c = a; " +
590 "(function() {a$b = 0;})(); a$b;");
591 }
592
593 public void testAddPropertyToUncollapsibleCtorInLocalScopeDepth1() {
594 test("/** @constructor */ var a = function() {}; var c = a; " +
595 "(function() {a.b = 0;})(); a.b;",
596 "var a = function() {}; var a$b; " +
597 "var c = a; (function() {a$b = 0;})(); a$b;");
598 }
599
600 public void testAddPropertyToUncollapsibleObjectInLocalScopeDepth2() {
601 test("var a = {}; a.b = {}; var d = a.b;" +
602 "(function() {a.b.c = 0;})(); a.b.c;",
603 "var a$b = {}; var d = a$b;" +
604 "(function() {a$b.c = 0;})(); a$b.c;");
605 }
606
607 public void testAddPropertyToUncollapsibleFunctionInLocalScopeDepth2() {
608 test("var a = {}; a.b = function (){}; var d = a.b;" +
609 "(function() {a.b.c = 0;})(); a.b.c;",
610 "var a$b = function (){}; var d = a$b;" +
611 "(function() {a$b.c = 0;})(); a$b.c;");
612 }
613
614 public void testAddPropertyToUncollapsibleCtorInLocalScopeDepth2() {
615 test("var a = {}; /** @constructor */ a.b = function (){}; var d = a.b;" +
616 "(function() {a.b.c = 0;})(); a.b.c;",
617 "var a$b = function (){}; var a$b$c; var d = a$b;" +
618 "(function() {a$b$c = 0;})(); a$b$c;");
619 }
620
621 public void testPropertyOfChildFuncOfUncollapsibleObjectDepth1() {
622 testSame("var a = {}; var c = a; a.b = function (){}; a.b.x = 0; a.b.x;");
623 }
624
625 public void testPropertyOfChildFuncOfUncollapsibleObjectDepth2() {
626 test("var a = {}; a.b = {}; var c = a.b;" +
627 "a.b.c = function (){}; a.b.c.x = 0; a.b.c.x;",
628 "var a$b = {}; var c = a$b;" +
629 "a$b.c = function (){}; a$b.c.x = 0; a$b.c.x;");
630 }
631
632 public void testAddPropertyToChildFuncOfUncollapsibleObjectInLocalScope() {
633 testSame("var a = {}; a.b = function (){}; a.b.x = 0;" +
634 "var c = a; (function() {a.b.y = 1;})(); a.b.x; a.b.y;");
635 }
636
637 public void testAddPropertyToChildTypeOfUncollapsibleObjectInLocalScope() {
638 test("var a = {}; /** @constructor */ a.b = function (){}; a.b.x = 0;" +
639 "var c = a; (function() {a.b.y = 1;})(); a.b.x; a.b.y;",
640 "var a = {}; var a$b = function (){}; var a$b$y; var a$b$x = 0;" +
641 "var c = a; (function() {a$b$y = 1;})(); a$b$x; a$b$y;",
642 null, CollapseProperties.UNSAFE_NAMESPACE_WARNING);
643 }
644
645 public void testAddPropertyToChildOfUncollapsibleFunctionInLocalScope() {
646 testSame(
647 "function a() {} a.b = {x: 0}; var c = a;" +
648 "(function() {a.b.y = 0;})(); a.b.y;");
649 }
650
651 public void testAddPropertyToChildOfUncollapsibleCtorInLocalScope() {
652 test("/** @constructor */ var a = function() {}; a.b = {x: 0}; var c = a;" +
653 "(function() {a.b.y = 0;})(); a.b.y;",
654 "var a = function() {}; var a$b$x = 0; var a$b$y; var c = a;" +
655 "(function() {a$b$y = 0;})(); a$b$y;");
656 }
657
658 public void testResetObjectPropertyInLocalScope() {
659 test("var a = {b: 0}; a.c = 1; function f() { a.c = 5; }",
660 "var a$b = 0; var a$c = 1; function f() { a$c = 5; }");
661 }
662
663 public void testResetFunctionPropertyInLocalScope() {
664 test("function a() {}; a.c = 1; function f() { a.c = 5; }",
665 "function a() {}; var a$c = 1; function f() { a$c = 5; }");
666 }
667
668 public void testGlobalNameReferencedInLocalScopeBeforeDefined1() {
669 // Because referencing global names earlier in the source code than they're
670 // defined is such a common practice, we collapse them even though a runtime
671 // exception could result (in the off-chance that the function gets called
672 // before the alias variable is defined).
673 test("var a = {b: 0}; function f() { a.c = 5; } a.c = 1;",
674 "var a$b = 0; function f() { a$c = 5; } var a$c = 1;");
675 }
676
677 public void testGlobalNameReferencedInLocalScopeBeforeDefined2() {
678 test("var a = {b: 0}; function f() { return a.c; } a.c = 1;",
679 "var a$b = 0; function f() { return a$c; } var a$c = 1;");
680 }
681
682 public void testTwiceDefinedGlobalNameDepth1_1() {
683 testSame("var a = {}; function f() { a.b(); }" +
684 "a = function() {}; a.b = function() {};");
685 }
686
687 public void testTwiceDefinedGlobalNameDepth1_2() {
688 testSame("var a = {}; /** @constructor */ a = function() {};" +
689 "a.b = {}; a.b.c = 0; function f() { a.b.d = 1; }");
690 }
691
692 public void testTwiceDefinedGlobalNameDepth2() {
693 test("var a = {}; a.b = {}; function f() { a.b.c(); }" +
694 "a.b = function() {}; a.b.c = function() {};",
695 "var a$b = {}; function f() { a$b.c(); }" +
696 "a$b = function() {}; a$b.c = function() {};");
697 }
698
699 public void testFunctionCallDepth1() {
700 test("var a = {}; a.b = function(){}; var c = a.b();",
701 "var a$b = function(){}; var c = a$b()");
702 }
703
704 public void testFunctionCallDepth2() {
705 test("var a = {}; a.b = {}; a.b.c = function(){}; a.b.c();",
706 "var a$b$c = function(){}; a$b$c();");
707 }
708
709 public void testFunctionAlias() {
710 test("var a = {}; a.b = {}; a.b.c = function(){}; a.b.d = a.b.c;",
711 "var a$b$c = function(){}; var a$b$d = a$b$c;");
712 }
713
714 public void testCallToRedefinedFunction() {
715 test("var a = {}; a.b = function(){}; a.b = function(){}; a.b();",
716 "var a$b = function(){}; a$b = function(){}; a$b();");
717 }
718
719 public void testCollapsePrototypeName() {
720 test("var a = {}; a.b = {}; a.b.c = function(){}; " +
721 "a.b.c.prototype.d = function(){}; (new a.b.c()).d();",
722 "var a$b$c = function(){}; a$b$c.prototype.d = function(){}; " +
723 "new a$b$c().d();");
724 }
725
726 public void testReferencedPrototypeProperty() {
727 test("var a = {b: {}}; a.b.c = function(){}; a.b.c.prototype.d = {};" +
728 "e = a.b.c.prototype.d;",
729 "var a$b$c = function(){}; a$b$c.prototype.d = {};" +
730 "e = a$b$c.prototype.d;");
731 }
732
733 public void testSetStaticAndPrototypePropertiesOnFunction() {
734 test("var a = {}; a.b = function(){}; a.b.prototype.d = 0; a.b.c = 1;",
735 "var a$b = function(){}; a$b.prototype.d = 0; var a$b$c = 1;");
736 }
737
738 public void testReadUndefinedPropertyDepth1() {
739 test("var a = {b: 0}; var c = a.d;",
740 "var a$b = 0; var a = {}; var c = a.d;");
741 }
742
743 public void testReadUndefinedPropertyDepth2() {
744 test("var a = {b: {c: 0}}; f(a.b.c); f(a.b.d);",
745 "var a$b$c = 0; var a$b = {}; f(a$b$c); f(a$b.d);");
746 }
747
748 public void testCallUndefinedMethodOnObjLitDepth1() {
749 test("var a = {b: 0}; a.c();",
750 "var a$b = 0; var a = {}; a.c();");
751 }
752
753 public void testCallUndefinedMethodOnObjLitDepth2() {
754 test("var a = {b: {}}; a.b.c = function() {}; a.b.c(); a.b.d();",
755 "var a$b = {}; var a$b$c = function() {}; a$b$c(); a$b.d();");
756 }
757
758 public void testPropertiesOfAnUndefinedVar() {
759 testSame("a.document = d; f(a.document.innerHTML);");
760 }
761
762 public void testPropertyOfAnObjectThatIsNeitherFunctionNorObjLit() {
763 testSame("var a = window; a.document = d; f(a.document)");
764 }
765
766 public void testStaticFunctionReferencingThis1() {
767 // Note: Google's JavaScript Style Guide says to avoid using the 'this'
768 // keyword in a static function.
769 test("var a = {}; a.b = function() {this.c}; var d = a.b;",
770 "var a$b = function() {this.c}; var d = a$b;", null, UNSAFE_THIS);
771 }
772
773 public void testStaticFunctionReferencingThis2() {
774 // This gives no warning, because "this" is in a scope whose name is not
775 // getting collapsed.
776 test("var a = {}; " +
777 "a.b = function() { return function(){ return this; }; };",
778 "var a$b = function() { return function(){ return this; }; };");
779 }
780
781 public void testStaticFunctionReferencingThis3() {
782 test("var a = {b: function() {this.c}};",
783 "var a$b = function() { this.c };", null, UNSAFE_THIS);
784 }
785
786 public void testStaticFunctionReferencingThis4() {
787 test("var a = {/** @this {Element} */ b: function() {this.c}};",
788 "var a$b = function() { this.c };");
789 }
790
791 public void testPrototypeMethodReferencingThis() {
792 testSame("var A = function(){}; A.prototype = {b: function() {this.c}};");
793 }
794
795 public void testConstructorReferencingThis() {
796 test("var a = {}; " +
797 "/** @constructor */ a.b = function() { this.a = 3; };",
798 "var a$b = function() { this.a = 3; };");
799 }
800
801 public void testSafeReferenceOfThis() {
802 test("var a = {}; " +
803 "/** @this {Object} */ a.b = function() { this.a = 3; };",
804 "var a$b = function() { this.a = 3; };");
805 }
806
807 public void testGlobalFunctionReferenceOfThis() {
808 testSame("var a = function() { this.a = 3; };");
809 }
810
811 public void testFunctionGivenTwoNames() {
812 // It's okay to collapse f's properties because g is not added to the
813 // global scope as an alias for f. (Try it in your browser.)
814 test("var f = function g() {}; f.a = 1; h(f.a);",
815 "var f = function g() {}; var f$a = 1; h(f$a);");
816 }
817
818 public void testObjLitWithUsedNumericKey() {
819 testSame("a = {40: {}, c: {}}; var d = a[40]; var e = a.c;");
820 }
821
822 public void testObjLitWithUnusedNumericKey() {
823 test("var a = {40: {}, c: {}}; var e = a.c;",
824 "var a$1 = {}; var a$c = {}; var e = a$c");
825 }
826
827 public void testObjLitWithNonIdentifierKeys() {
828 testSame("a = {' ': 0, ',': 1}; var c = a[' '];");
829 }
830
831 public void testChainedAssignments1() {
832 test("var x = {}; x.y = a = 0;",
833 "var x$y = a = 0;");
834 }
835
836 public void testChainedAssignments2() {
837 test("var x = {}; x.y = a = b = c();",
838 "var x$y = a = b = c();");
839 }
840
841 public void testChainedAssignments3() {
842 test("var x = {y: 1}; a = b = x.y;",
843 "var x$y = 1; a = b = x$y;");
844 }
845
846 public void testChainedAssignments4() {
847 test("var x = {}; a = b = x.y;",
848 "var x = {}; a = b = x.y;");
849 }
850
851 public void testChainedAssignments5() {
852 test("var x = {}; a = x.y = 0;", "var x$y; a = x$y = 0;");
853 }
854
855 public void testChainedAssignments6() {
856 test("var x = {}; a = x.y = b = c();",
857 "var x$y; a = x$y = b = c();");
858 }
859
860 public void testChainedAssignments7() {
861 test("var x = {}; a = x.y = {}; /** @constructor */ x.y.z = function() {};",
862 "var x$y; a = x$y = {}; var x$y$z = function() {};",
863 null, CollapseProperties.UNSAFE_NAMESPACE_WARNING);
864 }
865
866 public void testChainedVarAssignments1() {
867 test("var x = {y: 1}; var a = x.y = 0;",
868 "var x$y = 1; var a = x$y = 0;");
869 }
870
871 public void testChainedVarAssignments2() {
872 test("var x = {y: 1}; var a = x.y = b = 0;",
873 "var x$y = 1; var a = x$y = b = 0;");
874 }
875
876 public void testChainedVarAssignments3() {
877 test("var x = {y: {z: 1}}; var b = 0; var a = x.y.z = 1; var c = 2;",
878 "var x$y$z = 1; var b = 0; var a = x$y$z = 1; var c = 2;");
879 }
880
881 public void testChainedVarAssignments4() {
882 test("var x = {}; var a = b = x.y = 0;",
883 "var x$y; var a = b = x$y = 0;");
884 }
885
886 public void testChainedVarAssignments5() {
887 test("var x = {y: {}}; var a = b = x.y.z = 0;",
888 "var x$y$z; var a = b = x$y$z = 0;");
889 }
890
891 public void testPeerAndSubpropertyOfUncollapsibleProperty() {
892 test("var x = {}; var a = x.y = 0; x.w = 1; x.y.z = 2;" +
893 "b = x.w; c = x.y.z;",
894 "var x$y; var a = x$y = 0; var x$w = 1; x$y.z = 2;" +
895 "b = x$w; c = x$y.z;");
896 }
897
898 public void testComplexAssignmentAfterInitialAssignment() {
899 test("var d = {}; d.e = {}; d.e.f = 0; a = b = d.e.f = 1;",
900 "var d$e$f = 0; a = b = d$e$f = 1;");
901 }
902
903 public void testRenamePrefixOfUncollapsibleProperty() {
904 test("var d = {}; d.e = {}; a = b = d.e.f = 0;",
905 "var d$e$f; a = b = d$e$f = 0;");
906 }
907
908 public void testNewOperator() {
909 // Using the new operator on a name doesn't prevent its (static) properties
910 // from getting collapsed.
911 test("var a = {}; a.b = function() {}; a.b.c = 1; var d = new a.b();",
912 "var a$b = function() {}; var a$b$c = 1; var d = new a$b();");
913 }
914
915 public void testMethodCall() {
916 test("var a = {}; a.b = function() {}; var d = a.b();",
917 "var a$b = function() {}; var d = a$b();");
918 }
919
920 public void testObjLitDefinedInLocalScopeIsLeftAlone() {
921 test("var a = {}; a.b = function() {};" +
922 "a.b.prototype.f_ = function() {" +
923 " var x = { p: '', q: '', r: ''}; var y = x.q;" +
924 "};",
925 "var a$b = function() {};" +
926 "a$b.prototype.f_ = function() {" +
927 " var x = { p: '', q: '', r: ''}; var y = x.q;" +
928 "};");
929 }
930
931 public void testPropertiesOnBothSidesOfAssignment() {
932 // This verifies that replacements are done in the right order. Collapsing
933 // the l-value in an assignment affects the parse tree immediately above
934 // the r-value, so we update all rvalues before any lvalues.
935 test("var a = {b: 0}; a.c = a.b;", "var a$b = 0; var a$c = a$b;");
936 }
937
938 public void testCallOnUndefinedProperty() {
939 // The "inherits" property is not explicitly defined on a.b anywhere, but
940 // it is accessed as though it certainly exists (it is called), so we infer
941 // that it must be an uncollapsible property that has come into existence
942 // some other way.
943 test("var a = {}; a.b = function(){}; a.b.inherits(x);",
944 "var a$b = function(){}; a$b.inherits(x);");
945 }
946
947 public void testGetPropOnUndefinedProperty() {
948 // The "superClass_" property is not explicitly defined on a.b anywhere,
949 // but it is accessed as though it certainly exists (a subproperty of it
950 // is accessed), so we infer that it must be an uncollapsible property that
951 // has come into existence some other way.
952 test("var a = {b: function(){}}; a.b.prototype.c =" +
953 "function() { a.b.superClass_.c.call(this); }",
954 "var a$b = function(){}; a$b.prototype.c =" +
955 "function() { a$b.superClass_.c.call(this); }");
956 }
957
958 public void testLocalAlias1() {
959 test("var a = {b: 3}; function f() { var x = a; f(x.b); }",
960 "var a$b = 3; function f() { var x = null; f(a$b); }");
961 }
962
963 public void testLocalAlias2() {
964 test("var a = {b: 3, c: 4}; function f() { var x = a; f(x.b); f(x.c);}",
965 "var a$b = 3; var a$c = 4; " +
966 "function f() { var x = null; f(a$b); f(a$c);}");
967 }
968
969 public void testLocalAlias3() {
970 test("var a = {b: 3, c: {d: 5}}; " +
971 "function f() { var x = a; f(x.b); f(x.c); f(x.c.d); }",
972 "var a$b = 3; var a$c = {d: 5}; " +
973 "function f() { var x = null; f(a$b); f(a$c); f(a$c.d);}");
974 }
975
976 public void testLocalAlias4() {
977 test("var a = {b: 3}; var c = {d: 5}; " +
978 "function f() { var x = a; var y = c; f(x.b); f(y.d); }",
979 "var a$b = 3; var c$d = 5; " +
980 "function f() { var x = null; var y = null; f(a$b); f(c$d);}");
981 }
982
983 public void testLocalAlias5() {
984 test("var a = {b: {c: 5}}; " +
985 "function f() { var x = a; var y = x.b; f(a.b.c); f(y.c); }",
986 "var a$b$c = 5; " +
987 "function f() { var x = null; var y = null; f(a$b$c); f(a$b$c);}");
988 }
989
990 public void testLocalAlias6() {
991 test("var a = {b: 3}; function f() { var x = a; if (x.b) { f(x.b); } }",
992 "var a$b = 3; function f() { var x = null; if (a$b) { f(a$b); } }");
993 }
994
995 public void testLocalAlias7() {
996 test("var a = {b: {c: 5}}; function f() { var x = a.b; f(x.c); }",
997 "var a$b$c = 5; function f() { var x = null; f(a$b$c); }");
998 }
999
1000 public void testGlobalWriteToAncestor() {
1001 testSame("var a = {b: 3}; function f() { var x = a; f(a.b); } a = 5;");
1002 }
1003
1004 public void testGlobalWriteToNonAncestor() {
1005 test("var a = {b: 3}; function f() { var x = a; f(a.b); } a.b = 5;",
1006 "var a$b = 3; function f() { var x = null; f(a$b); } a$b = 5;");
1007 }
1008
1009 public void testLocalWriteToAncestor() {
1010 testSame("var a = {b: 3}; function f() { a = 5; var x = a; f(a.b); } ");
1011 }
1012
1013 public void testLocalWriteToNonAncestor() {
1014 test("var a = {b: 3}; " +
1015 "function f() { a.b = 5; var x = a; f(a.b); }",
1016 "var a$b = 3; function f() { a$b = 5; var x = null; f(a$b); } ");
1017 }
1018
1019 public void testNonWellformedAlias1() {
1020 testSame("var a = {b: 3}; function f() { f(x); var x = a; f(x.b); }");
1021 }
1022
1023 public void testNonWellformedAlias2() {
1024 testSame("var a = {b: 3}; " +
1025 "function f() { if (false) { var x = a; f(x.b); } f(x); }");
1026 }
1027
1028 public void testLocalAliasOfAncestor() {
1029 testSame("var a = {b: {c: 5}}; function g() { f(a); } " +
1030 "function f() { var x = a.b; f(x.c); }");
1031 }
1032
1033 public void testGlobalAliasOfAncestor() {
1034 testSame("var a = {b: {c: 5}}; var y = a; " +
1035 "function f() { var x = a.b; f(x.c); }");
1036 }
1037
1038 public void testLocalAliasOfOtherName() {
1039 testSame("var foo = function() { return {b: 3}; };" +
1040 "var a = foo(); a.b = 5; " +
1041 "function f() { var x = a.b; f(x); }");
1042 }
1043
1044 public void testLocalAliasOfFunction() {
1045 test("var a = function() {}; a.b = 5; " +
1046 "function f() { var x = a.b; f(x); }",
1047 "var a = function() {}; var a$b = 5; " +
1048 "function f() { var x = null; f(a$b); }");
1049 }
1050
1051 public void testNoInlineGetpropIntoCall() {
1052 test("var b = x; function f() { var a = b; a(); }",
1053 "var b = x; function f() { var a = null; b(); }");
1054 test("var b = {}; b.c = x; function f() { var a = b.c; a(); }",
1055 "var b$c = x; function f() { var a = null; b$c(); }");
1056 }
1057
1058 public void testInlineAliasWithModifications() {
1059 testSame("var x = 10; function f() { var y = x; x++; alert(y)} ");
1060 testSame("var x = 10; function f() { var y = x; x+=1; alert(y)} ");
1061 test("var x = {}; x.x = 10; function f() {var y=x.x; x.x++; alert(y)}",
1062 "var x$x = 10; function f() {var y=x$x; x$x++; alert(y)}");
1063 test("var x = {}; x.x = 10; function f() {var y=x.x; x.x+=1; alert(y)}",
1064 "var x$x = 10; function f() {var y=x$x; x$x+=1; alert(y)}");
1065 }
1066
1067 public void testCollapsePropertyOnExternType() {
1068 collapsePropertiesOnExternTypes = true;
1069 test("String.myFunc = function() {}; String.myFunc();",
1070 "var String$myFunc = function() {}; String$myFunc()");
1071 }
1072
1073 public void testCollapseForEachWithoutExterns() {
1074 collapsePropertiesOnExternTypes = true;
1075 test("/** @constructor */function Array(){};\n",
1076 "if (!Array.forEach) {\n" +
1077 " Array.forEach = function() {};\n" +
1078 "}",
1079 "if (!Array$forEach) {\n" +
1080 " var Array$forEach = function() {};\n" +
1081 "}", null, null);
1082 }
1083
1084 public void testNoCollapseForEachInExterns() {
1085 collapsePropertiesOnExternTypes = true;
1086 test("/** @constructor */ function Array() {}" +
1087 "Array.forEach = function() {}",
1088 "if (!Array.forEach) {\n" +
1089 " Array.forEach = function() {};\n" +
1090 "}",
1091 "if (!Array.forEach) {\n" +
1092 " Array.forEach = function() {};\n" +
1093 "}", null, null);
1094 }
1095
1096 public void testDoNotCollapsePropertyOnExternType() {
1097 collapsePropertiesOnExternTypes = false;
1098 test("String.myFunc = function() {}; String.myFunc()",
1099 "String.myFunc = function() {}; String.myFunc()");
1100 }
1101
1102 public void testBug1704733() {
1103 String prelude =
1104 "function protect(x) { return x; }" +
1105 "function O() {}" +
1106 "protect(O).m1 = function() {};" +
1107 "protect(O).m2 = function() {};" +
1108 "protect(O).m3 = function() {};";
1109
1110 testSame(prelude +
1111 "alert(O.m1); alert(O.m2()); alert(!O.m3);");
1112 }
1113
1114 public void testBug1956277() {
1115 test("var CONST = {}; CONST.URL = 3;",
1116 "var CONST$URL = 3;");
1117 }
1118
1119 public void testBug1974371() {
1120 test(
1121 "/** @enum {Object} */ var Foo = {A: {c: 2}, B: {c: 3}};" +
1122 "for (var key in Foo) {}",
1123 "var Foo$A = {c: 2}; var Foo$B = {c: 3};" +
1124 "var Foo = {A: Foo$A, B: Foo$B};" +
1125 "for (var key in Foo) {}");
1126 }
1127
1128 private final String COMMON_ENUM =
1129 "/** @enum {Object} */ var Foo = {A: {c: 2}, B: {c: 3}};";
1130
1131 public void testEnumOfObjects1() {
1132 test(
1133 COMMON_ENUM +
1134 "for (var key in Foo.A) {}",
1135 "var Foo$A = {c: 2}; var Foo$B$c = 3; for (var key in Foo$A) {}");
1136 }
1137
1138 public void testEnumOfObjects2() {
1139 test(
1140 COMMON_ENUM +
1141 "foo(Foo.A.c);",
1142 "var Foo$A$c = 2; var Foo$B$c = 3; foo(Foo$A$c);");
1143 }
1144
1145 public void testEnumOfObjects3() {
1146 test(
1147 "var x = {c: 2}; var y = {c: 3};" +
1148 "/** @enum {Object} */ var Foo = {A: x, B: y};" +
1149 "for (var key in Foo) {}",
1150 "var x = {c: 2}; var y = {c: 3};" +
1151 "var Foo$A = x; var Foo$B = y; var Foo = {A: Foo$A, B: Foo$B};" +
1152 "for (var key in Foo) {}");
1153 }
1154
1155 public void testEnumOfObjects4() {
1156 // Note that this produces bad code, but that's OK, because
1157 // checkConsts will yell at you for reassigning an enum value.
1158 // (enum values have to be constant).
1159 test(
1160 COMMON_ENUM +
1161 "for (var key in Foo) {} Foo.A = 3; alert(Foo.A);",
1162 "var Foo$A = {c: 2}; var Foo$B = {c: 3};" +
1163 "var Foo = {A: Foo$A, B: Foo$B};" +
1164 "for (var key in Foo) {} Foo$A = 3; alert(Foo$A);");
1165 }
1166
1167 public void testObjectOfObjects1() {
1168 // Basically the same as testEnumOfObjects4, but without the
1169 // constant enum values.
1170 testSame(
1171 "var Foo = {a: {c: 2}, b: {c: 3}};" +
1172 "for (var key in Foo) {} Foo.a = 3; alert(Foo.a);");
1173 }
1174
1175 public void testReferenceInAnonymousObject0() {
1176 test("var a = {};" +
1177 "a.b = function(){};" +
1178 "a.b.prototype.c = function(){};" +
1179 "var d = a.b.prototype.c;",
1180 "var a$b = function(){};" +
1181 "a$b.prototype.c = function(){};" +
1182 "var d = a$b.prototype.c;");
1183 }
1184
1185 public void testReferenceInAnonymousObject1() {
1186 test("var a = {};" +
1187 "a.b = function(){};" +
1188 "var d = a.b.prototype.c;",
1189 "var a$b = function(){};" +
1190 "var d = a$b.prototype.c;");
1191 }
1192
1193 public void testReferenceInAnonymousObject2() {
1194 test("var a = {};" +
1195 "a.b = function(){};" +
1196 "a.b.prototype.c = function(){};" +
1197 "var d = {c: a.b.prototype.c};",
1198 "var a$b = function(){};" +
1199 "a$b.prototype.c = function(){};" +
1200 "var d$c = a$b.prototype.c;");
1201 }
1202
1203 public void testReferenceInAnonymousObject3() {
1204 test("function CreateClass(a$$1) {}" +
1205 "var a = {};" +
1206 "a.b = function(){};" +
1207 "a.b.prototype.c = function(){};" +
1208 "a.d = CreateClass({c: a.b.prototype.c});",
1209 "function CreateClass(a$$1) {}" +
1210 "var a$b = function(){};" +
1211 "a$b.prototype.c = function(){};" +
1212 "var a$d = CreateClass({c: a$b.prototype.c});");
1213 }
1214
1215 public void testReferenceInAnonymousObject4() {
1216 test("function CreateClass(a) {}" +
1217 "var a = {};" +
1218 "a.b = CreateClass({c: function() {}});" +
1219 "a.d = CreateClass({c: a.b.c});",
1220 "function CreateClass(a$$1) {}" +
1221 "var a$b = CreateClass({c: function() {}});" +
1222 "var a$d = CreateClass({c: a$b.c});");
1223 }
1224
1225 public void testReferenceInAnonymousObject5() {
1226 test("function CreateClass(a) {}" +
1227 "var a = {};" +
1228 "a.b = CreateClass({c: function() {}});" +
1229 "a.d = CreateClass({c: a.b.prototype.c});",
1230 "function CreateClass(a$$1) {}" +
1231 "var a$b = CreateClass({c: function() {}});" +
1232 "var a$d = CreateClass({c: a$b.prototype.c});");
1233 }
1234
1235 public void testCrashInCommaOperator() {
1236 test("var a = {}; a.b = function() {},a.b();",
1237 "var a$b; a$b=function() {},a$b();");
1238 }
1239
1240 public void testCrashInNestedAssign() {
1241 test("var a = {}; if (a.b = function() {}) a.b();",
1242 "var a$b; if (a$b=function() {}) { a$b(); }");
1243 }
1244
1245 public void testTwinReferenceCancelsChildCollapsing() {
1246 test("var a = {}; if (a.b = function() {}) { a.b.c = 3; a.b(a.b.c); }",
1247 "var a$b; if (a$b = function() {}) { a$b.c = 3; a$b(a$b.c); }");
1248 }
1249
1250 public void testPropWithDollarSign() {
1251 test("var a = {$: 3}", "var a$$0 = 3;");
1252 }
1253
1254 public void testPropWithDollarSign2() {
1255 test("var a = {$: function(){}}", "var a$$0 = function(){};");
1256 }
1257
1258 public void testPropWithDollarSign3() {
1259 test("var a = {b: {c: 3}, b$c: function(){}}",
1260 "var a$b$c = 3; var a$b$0c = function(){};");
1261 }
1262
1263 public void testPropWithDollarSign4() {
1264 test("var a = {$$: {$$$: 3}};", "var a$$0$0$$0$0$0 = 3;");
1265 }
1266
1267 public void testPropWithDollarSign5() {
1268 test("var a = {b: {$0c: true}, b$0c: false};",
1269 "var a$b$$00c = true; var a$b$00c = false;");
1270 }
1271
1272 public void testConstKey() {
1273 test("var foo = {A: 3};", "var foo$A = 3;");
1274 }
1275
1276 public void testPropertyOnGlobalCtor() {
1277 test("/** @constructor */ function Map() {} Map.foo = 3; Map;",
1278 "function Map() {} var Map$foo = 3; Map;");
1279 }
1280
1281 public void testPropertyOnGlobalInterface() {
1282 test("/** @interface */ function Map() {} Map.foo = 3; Map;",
1283 "function Map() {} var Map$foo = 3; Map;");
1284 }
1285
1286 public void testPropertyOnGlobalFunction() {
1287 testSame("function Map() {} Map.foo = 3; Map;");
1288 }
1289
1290 public void testIssue389() {
1291 test(
1292 "function alias() {}" +
1293 "var dojo = {};" +
1294 "dojo.gfx = {};" +
1295 "dojo.declare = function() {};" +
1296 "/** @constructor */" +
1297 "dojo.gfx.Shape = function() {};" +
1298 "dojo.gfx.Shape = dojo.declare('dojo.gfx.Shape');" +
1299 "alias(dojo);",
1300 "function alias() {}" +
1301 "var dojo = {};" +
1302 "dojo.gfx = {};" +
1303 "dojo.declare = function() {};" +
1304 "/** @constructor */" +
1305 "var dojo$gfx$Shape = function() {};" +
1306 "dojo$gfx$Shape = dojo.declare('dojo.gfx.Shape');" +
1307 "alias(dojo);",
1308 null,
1309 CollapseProperties.UNSAFE_NAMESPACE_WARNING);
1310 }
1311
1312 public void testAliasedTopLevelName() {
1313 testSame(
1314 "function alias() {}" +
1315 "var dojo = {};" +
1316 "dojo.gfx = {};" +
1317 "dojo.declare = function() {};" +
1318 "dojo.gfx.Shape = {SQUARE: 2};" +
1319 "dojo.gfx.Shape = dojo.declare('dojo.gfx.Shape');" +
1320 "alias(dojo);" +
1321 "alias(dojo$gfx$Shape$SQUARE);");
1322 }
1323
1324 public void testAliasedTopLevelEnum() {
1325 test(
1326 "function alias() {}" +
1327 "var dojo = {};" +
1328 "dojo.gfx = {};" +
1329 "dojo.declare = function() {};" +
1330 "/** @enum {number} */" +
1331 "dojo.gfx.Shape = {SQUARE: 2};" +
1332 "dojo.gfx.Shape = dojo.declare('dojo.gfx.Shape');" +
1333 "alias(dojo);" +
1334 "alias(dojo.gfx.Shape.SQUARE);",
1335 "function alias() {}" +
1336 "var dojo = {};" +
1337 "dojo.gfx = {};" +
1338 "dojo.declare = function() {};" +
1339 "/** @constructor */" +
1340 "var dojo$gfx$Shape = {SQUARE: 2};" +
1341 "dojo$gfx$Shape = dojo.declare('dojo.gfx.Shape');" +
1342 "alias(dojo);" +
1343 "alias(dojo$gfx$Shape.SQUARE);",
1344 null,
1345 CollapseProperties.UNSAFE_NAMESPACE_WARNING);
1346 }
1347
1348 public void testAssignFunctionBeforeDefinition() {
1349 testSame(
1350 "f = function() {};" +
1351 "var f = null;");
1352 }
1353
1354 public void testObjectLitBeforeDefinition() {
1355 testSame(
1356 "a = {b: 3};" +
1357 "var a = null;" +
1358 "this.c = a.b;");
1359 }
1360
1361 public void testTypedef1() {
1362 test("var foo = {};" +
1363 "/** @typedef {number} */ foo.Baz;",
1364 "var foo = {}; var foo$Baz;");
1365 }
1366
1367 public void testTypedef2() {
1368 test("var foo = {};" +
1369 "/** @typedef {number} */ foo.Bar.Baz;" +
1370 "foo.Bar = function() {};",
1371 "var foo$Bar$Baz; var foo$Bar = function(){};");
1372 }
1373
1374 public void testDelete1() {
1375 testSame(
1376 "var foo = {};" +
1377 "foo.bar = 3;" +
1378 "delete foo.bar;");
1379 }
1380
1381 public void testDelete2() {
1382 test(
1383 "var foo = {};" +
1384 "foo.bar = 3;" +
1385 "foo.baz = 3;" +
1386 "delete foo.bar;",
1387 "var foo = {};" +
1388 "foo.bar = 3;" +
1389 "var foo$baz = 3;" +
1390 "delete foo.bar;");
1391 }
1392
1393 public void testDelete3() {
1394 testSame(
1395 "var foo = {bar: 3};" +
1396 "delete foo.bar;");
1397 }
1398
1399 public void testDelete4() {
1400 test(
1401 "var foo = {bar: 3, baz: 3};" +
1402 "delete foo.bar;",
1403 "var foo$baz=3;var foo={bar:3};delete foo.bar");
1404 }
1405
1406 public void testDelete5() {
1407 test(
1408 "var x = {};" +
1409 "x.foo = {};" +
1410 "x.foo.bar = 3;" +
1411 "delete x.foo.bar;",
1412 "var x$foo = {};" +
1413 "x$foo.bar = 3;" +
1414 "delete x$foo.bar;");
1415 }
1416
1417 public void testDelete6() {
1418 test(
1419 "var x = {};" +
1420 "x.foo = {};" +
1421 "x.foo.bar = 3;" +
1422 "x.foo.baz = 3;" +
1423 "delete x.foo.bar;",
1424 "var x$foo = {};" +
1425 "x$foo.bar = 3;" +
1426 "var x$foo$baz = 3;" +
1427 "delete x$foo.bar;");
1428 }
1429
1430 public void testDelete7() {
1431 test(
1432 "var x = {};" +
1433 "x.foo = {bar: 3};" +
1434 "delete x.foo.bar;",
1435 "var x$foo = {bar: 3};" +
1436 "delete x$foo.bar;");
1437 }
1438
1439 public void testDelete8() {
1440 test(
1441 "var x = {};" +
1442 "x.foo = {bar: 3, baz: 3};" +
1443 "delete x.foo.bar;",
1444 "var x$foo$baz = 3; var x$foo = {bar: 3};" +
1445 "delete x$foo.bar;");
1446 }
1447
1448 public void testDelete9() {
1449 testSame(
1450 "var x = {};" +
1451 "x.foo = {};" +
1452 "x.foo.bar = 3;" +
1453 "delete x.foo;");
1454 }
1455
1456 public void testDelete10() {
1457 testSame(
1458 "var x = {};" +
1459 "x.foo = {bar: 3};" +
1460 "delete x.foo;");
1461 }
1462
1463 public void testDelete11() {
1464 // Constructors are always collapsed.
1465 test(
1466 "var x = {};" +
1467 "x.foo = {};" +
1468 "/** @constructor */ x.foo.Bar = function() {};" +
1469 "delete x.foo;",
1470 "var x = {};" +
1471 "x.foo = {};" +
1472 "var x$foo$Bar = function() {};" +
1473 "delete x.foo;",
1474 null,
1475 CollapseProperties.NAMESPACE_REDEFINED_WARNING);
1476 }
1477
1478 public void testPreserveConstructorDoc() {
1479 test("var foo = {};" +
1480 "/** @constructor */\n" +
1481 "foo.bar = function() {}",
1482 "var foo$bar = function() {}");
1483
1484 Node root = getLastCompiler().getRoot();
1485
1486 Node fooBarNode = findQualifiedNameNode("foo$bar", root);
1487 Node varNode = fooBarNode.getParent();
1488 assertTrue(varNode.isVar());
1489 assertTrue(varNode.getJSDocInfo().isConstructor());
1490 }
1491 }