Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2005 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.ImmutableList;
20 import com.google.common.collect.Lists;
21
22 /**
23 * {@link RenameProperties} tests.
24 *
25 */
26 public class RenamePropertiesTest extends CompilerTestCase {
27
28 private static final String EXTERNS =
29 "var window;" +
30 "prop.toString;" +
31 "var google = { gears: { factory: {}, workerPool: {} } };";
32
33 private RenameProperties renameProperties;
34
35 private static boolean generatePseudoNames = false;
36
37 private static boolean useAffinity = false;
38
39 private VariableMap prevUsedPropertyMap = null;
40
41
42 public RenamePropertiesTest() {
43 super(EXTERNS);
44 enableNormalize();
45 }
46
47 @Override
48 protected void tearDown() throws Exception {
49 super.tearDown();
50
51 prevUsedPropertyMap = null;
52 useAffinity = false;
53 }
54
55 @Override protected int getNumRepetitions() {
56 // The RenameProperties pass should only be run once over a parse tree.
57 return 1;
58 }
59
60 public void testPrototypeProperties() {
61 test("Bar.prototype.getA = function(){}; bar.getA();" +
62 "Bar.prototype.getB = function(){};",
63 "Bar.prototype.a = function(){}; bar.a();" +
64 "Bar.prototype.b = function(){}");
65 }
66
67 public void testPrototypePropertiesAsObjLitKeys1() {
68 test("Bar.prototype = {2: function(){}, getA: function(){}}; bar[2]();",
69 "Bar.prototype = {2: function(){}, a: function(){}}; bar[2]();");
70 }
71
72 public void testPrototypePropertiesAsObjLitKeys2() {
73 testSame("Bar.prototype = {get 2(){}}; bar[2];");
74
75 testSame("Bar.prototype = {get 'a'(){}}; bar['a'];");
76
77 test("Bar.prototype = {get getA(){}}; bar.getA;",
78 "Bar.prototype = {get a(){}}; bar.a;");
79 }
80
81 public void testPrototypePropertiesAsObjLitKeys3() {
82 testSame("Bar.prototype = {set 2(x){}}; bar[2];");
83
84 testSame("Bar.prototype = {set 'a'(x){}}; bar['a'];");
85
86 test("Bar.prototype = {set getA(x){}}; bar.getA;",
87 "Bar.prototype = {set a(x){}}; bar.a;");
88 }
89
90 public void testMixedQuotedAndUnquotedObjLitKeys1() {
91 test("Bar = {getA: function(){}, 'getB': function(){}}; bar.getA();",
92 "Bar = {a: function(){}, 'getB': function(){}}; bar.a();");
93 }
94
95 public void testMixedQuotedAndUnquotedObjLitKeys2() {
96 test("Bar = {getA: function(){}, 'getB': function(){}}; bar.getA();",
97 "Bar = {a: function(){}, 'getB': function(){}}; bar.a();");
98 }
99
100 public void testQuotedPrototypeProperty() {
101 testSame("Bar.prototype['getA'] = function(){}; bar['getA']();");
102 }
103
104 public void testOverlappingOriginalAndGeneratedNames() {
105 test("Bar.prototype = {b: function(){}, a: function(){}}; bar.b();",
106 "Bar.prototype = {a: function(){}, b: function(){}}; bar.a();");
107 }
108
109 public void testRenamePropertiesWithLeadingUnderscores() {
110 test("Bar.prototype = {_getA: function(){}, _b: 0}; bar._getA();",
111 "Bar.prototype = {a: function(){}, b: 0}; bar.a();");
112 }
113
114 public void testPropertyAddedToObject() {
115 test("var foo = {}; foo.prop = '';",
116 "var foo = {}; foo.a = '';");
117 }
118
119 public void testPropertyAddedToFunction() {
120 test("var foo = function(){}; foo.prop = '';",
121 "var foo = function(){}; foo.a = '';");
122 }
123
124 public void testPropertyOfObjectOfUnknownType() {
125 test("var foo = x(); foo.prop = '';",
126 "var foo = x(); foo.a = '';");
127 }
128
129 public void testSetPropertyOfThis() {
130 test("this.prop = 'bar'",
131 "this.a = 'bar'");
132 }
133
134 public void testReadPropertyOfThis() {
135 test("f(this.prop);",
136 "f(this.a);");
137 }
138
139 public void testObjectLiteralInLocalScope() {
140 test("function x() { var foo = {prop1: 'bar', prop2: 'baz'}; }",
141 "function x() { var foo = {a: 'bar', b: 'baz'}; }");
142 }
143
144 public void testIncorrectAttemptToAccessQuotedProperty() {
145 // The correct way to call the quoted 'getFoo' method is: bar['getFoo']().
146 test("Bar.prototype = {'B': 0, 'getFoo': function(){}}; bar.getFoo();",
147 "Bar.prototype = {'B': 0, 'getFoo': function(){}}; bar.a();");
148 }
149
150 public void testSetQuotedPropertyOfThis() {
151 testSame("this['prop'] = 'bar';");
152 }
153
154 public void testExternedPropertyName() {
155 test("Bar.prototype = {toString: function(){}, foo: 0}; bar.toString();",
156 "Bar.prototype = {toString: function(){}, a: 0}; bar.toString();");
157 }
158
159 public void testExternedPropertyNameDefinedByObjectLiteral() {
160 test("function x() { var foo = google.gears.factory; }",
161 "function x() { var foo = google.gears.factory; }");
162 }
163
164 public void testAvoidingConflictsBetweenQuotedAndUnquotedPropertyNames() {
165 test("Bar.prototype.foo = function(){}; Bar.prototype['a'] = 0; bar.foo();",
166 "Bar.prototype.b = function(){}; Bar.prototype['a'] = 0; bar.b();");
167 }
168
169 public void testSamePropertyNameQuotedAndUnquoted() {
170 test("Bar.prototype.prop = function(){}; y = {'prop': 0};",
171 "Bar.prototype.a = function(){}; y = {'prop': 0};");
172 }
173
174 public void testStaticAndInstanceMethodWithSameName() {
175 test("Bar = function(){}; Bar.getA = function(){}; " +
176 "Bar.prototype.getA = function(){}; Bar.getA(); bar.getA();",
177 "Bar = function(){}; Bar.a = function(){}; " +
178 "Bar.prototype.a = function(){}; Bar.a(); bar.a();");
179 }
180
181 public void testRenamePropertiesFunctionCall1() {
182 test("var foo = {myProp: 0}; f(foo[JSCompiler_renameProperty('myProp')]);",
183 "var foo = {a: 0}; f(foo['a']);");
184 }
185
186 public void testRenamePropertiesFunctionCall2() {
187 test("var foo = {myProp: 0}; " +
188 "f(JSCompiler_renameProperty('otherProp.myProp.someProp')); " +
189 "foo.myProp = 1; foo.theirProp = 2; foo.yourProp = 3;",
190 "var foo = {a: 0}; f('b.a.c'); " +
191 "foo.a = 1; foo.d = 2; foo.e = 3;");
192 }
193
194 public void testRemoveRenameFunctionStubs1() {
195 test("function JSCompiler_renameProperty(x) { return x; }",
196 "");
197 }
198
199 public void testRemoveRenameFunctionStubs2() {
200 test("function JSCompiler_renameProperty(x) { return x; }" +
201 "var foo = {myProp: 0}; f(foo[JSCompiler_renameProperty('myProp')]);",
202 "var foo = {a: 0}; f(foo['a']);");
203 }
204
205 public void testGeneratePseudoNames() {
206 generatePseudoNames = true;
207 test("var foo={}; foo.bar=1; foo['abc']=2",
208 "var foo={}; foo.$bar$=1; foo['abc']=2");
209 generatePseudoNames = false;
210 }
211
212 public void testModules() {
213 String module1Js = "function Bar(){} Bar.prototype.getA=function(x){};" +
214 "var foo;foo.getA(foo);foo.doo=foo;foo.bloo=foo;";
215
216 String module2Js = "function Far(){} Far.prototype.getB=function(x){};" +
217 "var too;too.getB(too);too.woo=too;too.bloo=too;";
218
219 String module3Js = "function Car(){} Car.prototype.getC=function(x){};" +
220 "var noo;noo.getC(noo);noo.zoo=noo;noo.cloo=noo;";
221
222 JSModule module1 = new JSModule("m1");
223 module1.add(SourceFile.fromCode("input1", module1Js));
224
225 JSModule module2 = new JSModule("m2");
226 module2.add(SourceFile.fromCode("input2", module2Js));
227
228 JSModule module3 = new JSModule("m3");
229 module3.add(SourceFile.fromCode("input3", module3Js));
230
231 JSModule[] modules = new JSModule[] { module1, module2, module3 };
232 Compiler compiler = compileModules("", modules);
233
234 Result result = compiler.getResult();
235 assertTrue(result.success);
236
237 assertEquals("function Bar(){}Bar.prototype.b=function(x){};" +
238 "var foo;foo.b(foo);foo.f=foo;foo.a=foo;",
239 compiler.toSource(module1));
240
241 assertEquals("function Far(){}Far.prototype.c=function(x){};" +
242 "var too;too.c(too);too.g=too;too.a=too;",
243 compiler.toSource(module2));
244
245 // Note that properties that occur most often globally get the earliest
246 // names. The "getC" property, which doesn't occur until module 3, is
247 // renamed to an earlier name in the alphabet than "woo", which appears
248 // in module 2, because "getC" occurs more total times across all modules.
249 // Might be better to give early modules the shortest names, but this is
250 // how the pass currently works.
251 assertEquals("function Car(){}Car.prototype.d=function(x){};" +
252 "var noo;noo.d(noo);noo.h=noo;noo.e=noo;",
253 compiler.toSource(module3));
254 }
255
256 public void testPropertyAffinity() {
257 // 'y' gets to be 'b' because it appears with z often.
258 // Other wise, 'x' gets to be 'b' because of alphabetical ordering.
259 useAffinity = true;
260 test("var foo={};foo.x=1;foo.y=2;foo.z=3;" +
261 "function f1() { foo.z; foo.z; foo.z; foo.y}" +
262 "function f2() { foo.x}",
263
264
265 "var foo={};foo.c=1;foo.b=2;foo.a=3;" +
266 "function f1() { foo.a; foo.a; foo.a; foo.b}" +
267 "function f2() { foo.c}");
268
269 test("var foo={};foo.x=1;foo.y=2;foo.z=3;" +
270 "function f1() { foo.z; foo.z; foo.z; foo.y}" +
271 "function f2() { foo.z; foo.z; foo.z; foo.x}",
272
273
274 "var foo={};foo.b=1;foo.c=2;foo.a=3;" +
275 "function f1() { foo.a; foo.a; foo.a; foo.c}" +
276 "function f2() { foo.a; foo.a; foo.a; foo.b}");
277 }
278
279 public void testPropertyAffinityOff() {
280 useAffinity = false;
281 test("var foo={};foo.x=1;foo.y=2;foo.z=3;" +
282 "function f1() { foo.z; foo.z; foo.z; foo.y}" +
283 "function f2() { foo.x}",
284
285
286 "var foo={};foo.b=1;foo.c=2;foo.a=3;" +
287 "function f1() { foo.a; foo.a; foo.a; foo.c}" +
288 "function f2() { foo.b}");
289
290 test("var foo={};foo.x=1;foo.y=2;foo.z=3;" +
291 "function f1() { foo.z; foo.z; foo.z; foo.y}" +
292 "function f2() { foo.z; foo.z; foo.z; foo.x}",
293
294
295 "var foo={};foo.b=1;foo.c=2;foo.a=3;" +
296 "function f1() { foo.a; foo.a; foo.a; foo.c}" +
297 "function f2() { foo.a; foo.a; foo.a; foo.b}");
298 }
299
300 public void testPrototypePropertiesStable() {
301 testStableRenaming(
302 "Bar.prototype.getA = function(){}; bar.getA();" +
303 "Bar.prototype.getB = function(){};",
304 "Bar.prototype.a = function(){}; bar.a();" +
305 "Bar.prototype.b = function(){}",
306 "Bar.prototype.get = function(){}; bar.get();" +
307 "Bar.prototype.getA = function(){}; bar.getA();" +
308 "Bar.prototype.getB = function(){};",
309 "Bar.prototype.c = function(){}; bar.c();" +
310 "Bar.prototype.a = function(){}; bar.a();" +
311 "Bar.prototype.b = function(){}");
312 }
313
314 public void testPrototypePropertiesAsObjLitKeysStable() {
315 testStableRenaming(
316 "Bar.prototype = {2: function(){}, getA: function(){}}; bar[2]();",
317 "Bar.prototype = {2: function(){}, a: function(){}}; bar[2]();",
318 "Bar.prototype = {getB: function(){},getA: function(){}}; bar.getB();",
319 "Bar.prototype = {b: function(){},a: function(){}}; bar.b();");
320 }
321
322 public void testMixedQuotedAndUnquotedObjLitKeysStable() {
323 testStableRenaming(
324 "Bar = {getA: function(){}, 'getB': function(){}}; bar.getA();",
325 "Bar = {a: function(){}, 'getB': function(){}}; bar.a();",
326 "Bar = {get: function(){}, getA: function(){}, 'getB': function(){}};" +
327 "bar.getA();bar.get();",
328 "Bar = {b: function(){}, a: function(){}, 'getB': function(){}};" +
329 "bar.a();bar.b();");
330 }
331
332 public void testOverlappingOriginalAndGeneratedNamesStable() {
333 testStableRenaming(
334 "Bar.prototype = {b: function(){}, a: function(){}}; bar.b();",
335 "Bar.prototype = {a: function(){}, b: function(){}}; bar.a();",
336 "Bar.prototype = {c: function(){}, b: function(){}, a: function(){}};" +
337 "bar.b();",
338 "Bar.prototype = {c: function(){}, a: function(){}, b: function(){}};" +
339 "bar.a();");
340 }
341
342 public void testStableWithTrickyExternsChanges() {
343 test("Bar.prototype = {b: function(){}, a: function(){}}; bar.b();",
344 "Bar.prototype = {a: function(){}, b: function(){}}; bar.a();");
345 prevUsedPropertyMap = renameProperties.getPropertyMap();
346 String externs = EXTERNS + "prop.b;";
347 test(externs,
348 "Bar.prototype = {new_f: function(){}, b: function(){}, " +
349 "a: function(){}};bar.b();",
350 "Bar.prototype = {c:function(){}, b:function(){}, a:function(){}};" +
351 "bar.b();", null, null);
352 }
353
354 public void testRenamePropertiesWithLeadingUnderscoresStable() {
355 testStableRenaming(
356 "Bar.prototype = {_getA: function(){}, _b: 0}; bar._getA();",
357 "Bar.prototype = {a: function(){}, b: 0}; bar.a();",
358 "Bar.prototype = {_getA: function(){}, _c: 1, _b: 0}; bar._getA();",
359 "Bar.prototype = {a: function(){}, c: 1, b: 0}; bar.a();");
360 }
361
362 public void testPropertyAddedToObjectStable() {
363 testStableRenaming("var foo = {}; foo.prop = '';",
364 "var foo = {}; foo.a = '';",
365 "var foo = {}; foo.prop = ''; foo.a='';",
366 "var foo = {}; foo.a = ''; foo.b='';");
367 }
368
369 public void testAvoidingConflictsBetQuotedAndUnquotedPropertyNamesStable() {
370 testStableRenaming(
371 "Bar.prototype.foo = function(){}; Bar.prototype['b'] = 0; bar.foo();",
372 "Bar.prototype.a = function(){}; Bar.prototype['b'] = 0; bar.a();",
373 "Bar.prototype.foo = function(){}; Bar.prototype['a'] = 0; bar.foo();",
374 "Bar.prototype.b = function(){}; Bar.prototype['a'] = 0; bar.b();");
375 }
376
377 public void testRenamePropertiesFunctionCallStable() {
378 testStableRenaming(
379 "var foo = {myProp: 0}; " +
380 "f(JSCompiler_renameProperty('otherProp.myProp.someProp')); " +
381 "foo.myProp = 1; foo.theirProp = 2; foo.yourProp = 3;",
382 "var foo = {a: 0}; f('b.a.c'); " +
383 "foo.a = 1; foo.d = 2; foo.e = 3;",
384 "var bar = {newProp: 0}; var foo = {myProp: 0}; " +
385 "f(JSCompiler_renameProperty('otherProp.myProp.someProp')); " +
386 "foo.myProp = 1; foo.theirProp = 2; foo.yourProp = 3;",
387 "var bar = {f: 0}; var foo = {a: 0}; f('b.a.c'); " +
388 "foo.a = 1; foo.d = 2; foo.e = 3;");
389 }
390
391 private void testStableRenaming(String input1, String expected1,
392 String input2, String expected2) {
393 test(input1, expected1);
394 prevUsedPropertyMap = renameProperties.getPropertyMap();
395 test(input2, expected2);
396 }
397
398 private Compiler compileModules(String externs, JSModule[] modules) {
399 SourceFile externsInput = SourceFile.fromCode("externs", externs);
400
401 CompilerOptions options = new CompilerOptions();
402 options.propertyRenaming = PropertyRenamingPolicy.ALL_UNQUOTED;
403
404 Compiler compiler = new Compiler();
405 compiler.compileModules(
406 ImmutableList.of(externsInput), Lists.newArrayList(modules), options);
407 return compiler;
408 }
409
410 @Override
411 public CompilerPass getProcessor(Compiler compiler) {
412 return renameProperties =
413 new RenameProperties(compiler, useAffinity, generatePseudoNames,
414 prevUsedPropertyMap);
415 }
416 }