Debian Codegraph @

test/com/google/javascript/jscomp/RemoveUnusedPrototypePropertiesTest.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 /**
20 * Tests for {@link RemoveUnusedPrototypeProperties}.
21 *
22 * @author [email protected] (Nick Santos)
23 */
24 public class RemoveUnusedPrototypePropertiesTest extends CompilerTestCase {
25 private static final String EXTERNS =
26 "IFoo.prototype.bar; var mExtern; mExtern.bExtern; mExtern['cExtern'];";
27
28 private boolean canRemoveExterns = false;
29 private boolean anchorUnusedVars = false;
30
31 public RemoveUnusedPrototypePropertiesTest() {
32 super(EXTERNS);
33 }
34
35 @Override
36 protected CompilerPass getProcessor(Compiler compiler) {
37 return new RemoveUnusedPrototypeProperties(compiler,
38 canRemoveExterns, anchorUnusedVars);
39 }
40
41 @Override
42 public void setUp() {
43 anchorUnusedVars = false;
44 canRemoveExterns = false;
45 }
46
47 public void testAnalyzePrototypeProperties() {
48 // Basic removal for prototype properties
49 test("function e(){}" +
50 "e.prototype.a = function(){};" +
51 "e.prototype.b = function(){};" +
52 "var x = new e; x.a()",
53 "function e(){}" +
54 "e.prototype.a = function(){};" +
55 "var x = new e; x.a()");
56
57 // Basic removal for prototype replacement
58 test("function e(){}" +
59 "e.prototype = {a: function(){}, b: function(){}};" +
60 "var x=new e; x.a()",
61 "function e(){}" +
62 "e.prototype = {a: function(){}};" +
63 "var x = new e; x.a()");
64
65 // Unused properties that were referenced in the externs file should not be
66 // removed
67 test("function e(){}" +
68 "e.prototype.a = function(){};" +
69 "e.prototype.bExtern = function(){};" +
70 "var x = new e;x.a()",
71 "function e(){}" +
72 "e.prototype.a = function(){};" +
73 "e.prototype.bExtern = function(){};" +
74 "var x = new e; x.a()");
75 test("function e(){}" +
76 "e.prototype = {a: function(){}, bExtern: function(){}};" +
77 "var x = new e; x.a()",
78 "function e(){}" +
79 "e.prototype = {a: function(){}, bExtern: function(){}};" +
80 "var x = new e; x.a()");
81 }
82
83 public void testAliasing1() {
84 // Aliasing a property is not enough for it to count as used
85 test("function e(){}" +
86 "e.prototype.method1 = function(){};" +
87 "e.prototype.method2 = function(){};" +
88 // aliases
89 "e.prototype.alias1 = e.prototype.method1;" +
90 "e.prototype.alias2 = e.prototype.method2;" +
91 "var x = new e; x.method1()",
92 "function e(){}" +
93 "e.prototype.method1 = function(){};" +
94 "var x = new e; x.method1()");
95
96 // Using an alias should keep it
97 test("function e(){}" +
98 "e.prototype.method1 = function(){};" +
99 "e.prototype.method2 = function(){};" +
100 // aliases
101 "e.prototype.alias1 = e.prototype.method1;" +
102 "e.prototype.alias2 = e.prototype.method2;" +
103 "var x=new e; x.alias1()",
104 "function e(){}" +
105 "e.prototype.method1 = function(){};" +
106 "e.prototype.alias1 = e.prototype.method1;" +
107 "var x = new e; x.alias1()");
108 }
109
110 public void testAliasing2() {
111 // Aliasing a property is not enough for it to count as used
112 test("function e(){}" +
113 "e.prototype.method1 = function(){};" +
114 // aliases
115 "e.prototype.alias1 = e.prototype.method1;" +
116 "(new e).method1()",
117 "function e(){}" +
118 "e.prototype.method1 = function(){};" +
119 "(new e).method1()");
120
121 // Using an alias should keep it
122 test("function e(){}" +
123 "e.prototype.method1 = function(){};" +
124 // aliases
125 "e.prototype.alias1 = e.prototype.method1;" +
126 "(new e).alias1()",
127 "function e(){}" +
128 "e.prototype.method1 = function(){};" +
129 "e.prototype.alias1 = e.prototype.method1;" +
130 "(new e).alias1()");
131 }
132
133 public void testAliasing3() {
134 // Aliasing a property is not enough for it to count as used
135 test("function e(){}" +
136 "e.prototype.method1 = function(){};" +
137 "e.prototype.method2 = function(){};" +
138 // aliases
139 "e.prototype['alias1'] = e.prototype.method1;" +
140 "e.prototype['alias2'] = e.prototype.method2;",
141 "function e(){}" +
142 "e.prototype.method1=function(){};" +
143 "e.prototype.method2=function(){};" +
144 "e.prototype[\"alias1\"]=e.prototype.method1;" +
145 "e.prototype[\"alias2\"]=e.prototype.method2;");
146 }
147
148 public void testAliasing4() {
149 // Aliasing a property is not enough for it to count as used
150 test("function e(){}" +
151 "e.prototype['alias1'] = e.prototype.method1 = function(){};" +
152 "e.prototype['alias2'] = e.prototype.method2 = function(){};",
153 "function e(){}" +
154 "e.prototype[\"alias1\"]=e.prototype.method1=function(){};" +
155 "e.prototype[\"alias2\"]=e.prototype.method2=function(){};");
156 }
157
158 public void testAliasing5() {
159 // An exported alias must preserved any referenced values in the
160 // referenced function.
161 test("function e(){}" +
162 "e.prototype.method1 = function(){this.method2()};" +
163 "e.prototype.method2 = function(){};" +
164 // aliases
165 "e.prototype['alias1'] = e.prototype.method1;",
166 "function e(){}" +
167 "e.prototype.method1=function(){this.method2()};" +
168 "e.prototype.method2=function(){};" +
169 "e.prototype[\"alias1\"]=e.prototype.method1;");
170 }
171
172 public void testAliasing6() {
173 // An exported alias must preserved any referenced values in the
174 // referenced function.
175 test("function e(){}" +
176 "e.prototype.method1 = function(){this.method2()};" +
177 "e.prototype.method2 = function(){};" +
178 // aliases
179 "window['alias1'] = e.prototype.method1;",
180 "function e(){}" +
181 "e.prototype.method1=function(){this.method2()};" +
182 "e.prototype.method2=function(){};" +
183 "window['alias1']=e.prototype.method1;");
184 }
185
186 public void testAliasing7() {
187 // An exported alias must preserved any referenced values in the
188 // referenced function.
189 testSame("function e(){}" +
190 "e.prototype['alias1'] = e.prototype.method1 = " +
191 "function(){this.method2()};" +
192 "e.prototype.method2 = function(){};");
193 }
194
195 public void testStatementRestriction() {
196 test("function e(){}" +
197 "var x = e.prototype.method1 = function(){};" +
198 "var y = new e; x()",
199 "function e(){}" +
200 "var x = e.prototype.method1 = function(){};" +
201 "var y = new e; x()");
202 }
203
204 public void testExportedMethodsByNamingConvention() {
205 String classAndItsMethodAliasedAsExtern =
206 "function Foo() {}" +
207 "Foo.prototype.method = function() {};" + // not removed
208 "Foo.prototype.unused = function() {};" + // removed
209 "var _externInstance = new Foo();" +
210 "Foo.prototype._externMethod = Foo.prototype.method"; // aliased here
211
212 String compiled =
213 "function Foo(){}" +
214 "Foo.prototype.method = function(){};" +
215 "var _externInstance = new Foo;" +
216 "Foo.prototype._externMethod = Foo.prototype.method";
217
218 test(classAndItsMethodAliasedAsExtern, compiled);
219 }
220
221 public void testMethodsFromExternsFileNotExported() {
222 canRemoveExterns = true;
223 String classAndItsMethodAliasedAsExtern =
224 "function Foo() {}" +
225 "Foo.prototype.bar_ = function() {};" +
226 "Foo.prototype.unused = function() {};" +
227 "var instance = new Foo;" +
228 "Foo.prototype.bar = Foo.prototype.bar_";
229
230 String compiled =
231 "function Foo(){}" +
232 "var instance = new Foo;";
233
234 test(classAndItsMethodAliasedAsExtern, compiled);
235 }
236
237 public void testExportedMethodsByNamingConventionAlwaysExported() {
238 canRemoveExterns = true;
239 String classAndItsMethodAliasedAsExtern =
240 "function Foo() {}" +
241 "Foo.prototype.method = function() {};" + // not removed
242 "Foo.prototype.unused = function() {};" + // removed
243 "var _externInstance = new Foo();" +
244 "Foo.prototype._externMethod = Foo.prototype.method"; // aliased here
245
246 String compiled =
247 "function Foo(){}" +
248 "Foo.prototype.method = function(){};" +
249 "var _externInstance = new Foo;" +
250 "Foo.prototype._externMethod = Foo.prototype.method";
251
252 test(classAndItsMethodAliasedAsExtern, compiled);
253 }
254
255 public void testExternMethodsFromExternsFile() {
256 String classAndItsMethodAliasedAsExtern =
257 "function Foo() {}" +
258 "Foo.prototype.bar_ = function() {};" + // not removed
259 "Foo.prototype.unused = function() {};" + // removed
260 "var instance = new Foo;" +
261 "Foo.prototype.bar = Foo.prototype.bar_"; // aliased here
262
263 String compiled =
264 "function Foo(){}" +
265 "Foo.prototype.bar_ = function(){};" +
266 "var instance = new Foo;" +
267 "Foo.prototype.bar = Foo.prototype.bar_";
268
269 test(classAndItsMethodAliasedAsExtern, compiled);
270 }
271
272 public void testPropertyReferenceGraph() {
273 // test a prototype property graph that looks like so:
274 // b -> a, c -> b, c -> a, d -> c, e -> a, e -> f
275 String constructor = "function Foo() {}";
276 String defA =
277 "Foo.prototype.a = function() { Foo.superClass_.a.call(this); };";
278 String defB = "Foo.prototype.b = function() { this.a(); };";
279 String defC = "Foo.prototype.c = function() { " +
280 "Foo.superClass_.c.call(this); this.b(); this.a(); };";
281 String defD = "Foo.prototype.d = function() { this.c(); };";
282 String defE = "Foo.prototype.e = function() { this.a(); this.f(); };";
283 String defF = "Foo.prototype.f = function() { };";
284 String fullClassDef = constructor + defA + defB + defC + defD + defE + defF;
285
286 // ensure that all prototypes are compiled out if none are used
287 test(fullClassDef, "");
288
289 // make sure that the right prototypes are called for each use
290 String callA = "(new Foo()).a();";
291 String callB = "(new Foo()).b();";
292 String callC = "(new Foo()).c();";
293 String callD = "(new Foo()).d();";
294 String callE = "(new Foo()).e();";
295 String callF = "(new Foo()).f();";
296 test(fullClassDef + callA, constructor + defA + callA);
297 test(fullClassDef + callB, constructor + defA + defB + callB);
298 test(fullClassDef + callC, constructor + defA + defB + defC + callC);
299 test(fullClassDef + callD, constructor + defA + defB + defC + defD + callD);
300 test(fullClassDef + callE, constructor + defA + defE + defF + callE);
301 test(fullClassDef + callF, constructor + defF + callF);
302
303 test(fullClassDef + callA + callC,
304 constructor + defA + defB + defC + callA + callC);
305 test(fullClassDef + callB + callC,
306 constructor + defA + defB + defC + callB + callC);
307 test(fullClassDef + callA + callB + callC,
308 constructor + defA + defB + defC + callA + callB + callC);
309 }
310
311 public void testPropertiesDefinedWithGetElem() {
312 testSame("function Foo() {} Foo.prototype['elem'] = function() {};");
313 testSame("function Foo() {} Foo.prototype[1 + 1] = function() {};");
314 }
315
316 public void testNeverRemoveImplicitlyUsedProperties() {
317 testSame("function Foo() {} " +
318 "Foo.prototype.length = 3; " +
319 "Foo.prototype.toString = function() { return 'Foo'; }; " +
320 "Foo.prototype.valueOf = function() { return 'Foo'; }; ");
321 }
322
323 public void testPropertyDefinedInBranch() {
324 test("function Foo() {} if (true) Foo.prototype.baz = function() {};",
325 "if (true);");
326 test("function Foo() {} while (true) Foo.prototype.baz = function() {};",
327 "while (true);");
328 test("function Foo() {} for (;;) Foo.prototype.baz = function() {};",
329 "for (;;);");
330 test("function Foo() {} do Foo.prototype.baz = function() {}; while(true);",
331 "do; while(true);");
332 }
333
334 public void testUsingAnonymousObjectsToDefeatRemoval() {
335 String constructor = "function Foo() {}";
336 String declaration = constructor + "Foo.prototype.baz = 3;";
337 test(declaration, "");
338 testSame(declaration + "var x = {}; x.baz = 5;");
339 testSame(declaration + "var x = {baz: 5};");
340 test(declaration + "var x = {'baz': 5};",
341 "var x = {'baz': 5};");
342 }
343
344 public void testGlobalFunctionsInGraph() {
345 test(
346 "var x = function() { (new Foo).baz(); };" +
347 "var y = function() { x(); };" +
348 "function Foo() {}" +
349 "Foo.prototype.baz = function() { y(); };",
350 "");
351 }
352
353 public void testGlobalFunctionsInGraph2() {
354 // In this example, Foo.prototype.baz is a global reference to
355 // Foo, and Foo has a reference to baz. So everything stays in.
356 // TODO(nicksantos): We should be able to make the graph more fine-grained
357 // here. Instead of Foo.prototype.bar creating a global reference to Foo,
358 // it should create a reference from .bar to Foo. That will let us
359 // compile this away to nothing.
360 testSame(
361 "var x = function() { (new Foo).baz(); };" +
362 "var y = function() { x(); };" +
363 "function Foo() { this.baz(); }" +
364 "Foo.prototype.baz = function() { y(); };");
365 }
366
367 public void testGlobalFunctionsInGraph3() {
368 test(
369 "var x = function() { (new Foo).baz(); };" +
370 "var y = function() { x(); };" +
371 "function Foo() { this.baz(); }" +
372 "Foo.prototype.baz = function() { x(); };",
373 "var x = function() { (new Foo).baz(); };" +
374 "function Foo() { this.baz(); }" +
375 "Foo.prototype.baz = function() { x(); };");
376 }
377
378 public void testGlobalFunctionsInGraph4() {
379 test(
380 "var x = function() { (new Foo).baz(); };" +
381 "var y = function() { x(); };" +
382 "function Foo() { Foo.prototype.baz = function() { y(); }; }",
383 "");
384 }
385
386 public void testGlobalFunctionsInGraph5() {
387 test(
388 "function Foo() {}" +
389 "Foo.prototype.methodA = function() {};" +
390 "function x() { (new Foo).methodA(); }" +
391 "Foo.prototype.methodB = function() { x(); };",
392 "");
393
394 anchorUnusedVars = true;
395 test(
396 "function Foo() {}" +
397 "Foo.prototype.methodA = function() {};" +
398 "function x() { (new Foo).methodA(); }" +
399 "Foo.prototype.methodB = function() { x(); };",
400
401 "function Foo() {}" +
402 "Foo.prototype.methodA = function() {};" +
403 "function x() { (new Foo).methodA(); }");
404 }
405
406 public void testGlobalFunctionsInGraph6() {
407 testSame(
408 "function Foo() {}" +
409 "Foo.prototype.methodA = function() {};" +
410 "function x() { (new Foo).methodA(); }" +
411 "Foo.prototype.methodB = function() { x(); };" +
412 "(new Foo).methodB();");
413 }
414
415 public void testGlobalFunctionsInGraph7() {
416 testSame(
417 "function Foo() {}" +
418 "Foo.prototype.methodA = function() {};" +
419 "this.methodA();");
420 }
421
422 public void testGetterBaseline() {
423 anchorUnusedVars = true;
424 test(
425 "function Foo() {}" +
426 "Foo.prototype = { " +
427 " methodA: function() {}," +
428 " methodB: function() { x(); }" +
429 "};" +
430 "function x() { (new Foo).methodA(); }",
431
432 "function Foo() {}" +
433 "Foo.prototype = { " +
434 " methodA: function() {}" +
435 "};" +
436 "function x() { (new Foo).methodA(); }");
437 }
438
439 public void testGetter1() {
440 test(
441 "function Foo() {}" +
442 "Foo.prototype = { " +
443 " get methodA() {}," +
444 " get methodB() { x(); }" +
445 "};" +
446 "function x() { (new Foo).methodA; }",
447
448 "function Foo() {}" +
449 "Foo.prototype = {};");
450
451 anchorUnusedVars = true;
452 test(
453 "function Foo() {}" +
454 "Foo.prototype = { " +
455 " get methodA() {}," +
456 " get methodB() { x(); }" +
457 "};" +
458 "function x() { (new Foo).methodA; }",
459
460 "function Foo() {}" +
461 "Foo.prototype = { " +
462 " get methodA() {}" +
463 "};" +
464 "function x() { (new Foo).methodA; }");
465 }
466
467 public void testGetter2() {
468 anchorUnusedVars = true;
469 test(
470 "function Foo() {}" +
471 "Foo.prototype = { " +
472 " get methodA() {}," +
473 " set methodA(a) {}," +
474 " get methodB() { x(); }," +
475 " set methodB(a) { x(); }" +
476 "};" +
477 "function x() { (new Foo).methodA; }",
478
479 "function Foo() {}" +
480 "Foo.prototype = { " +
481 " get methodA() {}," +
482 " set methodA(a) {}" +
483 "};" +
484 "function x() { (new Foo).methodA; }");
485 }
486
487 public void testHook1() throws Exception {
488 test(
489 "/** @constructor */ function Foo() {}" +
490 "Foo.prototype.method1 = Math.random() ?" +
491 " function() { this.method2(); } : function() { this.method3(); };" +
492 "Foo.prototype.method2 = function() {};" +
493 "Foo.prototype.method3 = function() {};",
494 "");
495 }
496
497 public void testHook2() throws Exception {
498 testSame(
499 "/** @constructor */ function Foo() {}" +
500 "Foo.prototype.method1 = Math.random() ?" +
501 " function() { this.method2(); } : function() { this.method3(); };" +
502 "Foo.prototype.method2 = function() {};" +
503 "Foo.prototype.method3 = function() {};" +
504 "(new Foo()).method1();");
505 }
506
507 }