Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2011 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 * @author [email protected] (John Lenz)
21 */
22 public class RemoveUnusedClassPropertiesTest extends CompilerTestCase {
23
24 private static final String EXTERNS =
25 "var window;\n" +
26 "function alert(a) {}\n" +
27 "var EXT = {};" +
28 "EXT.ext;";
29
30 public RemoveUnusedClassPropertiesTest() {
31 super(EXTERNS);
32 }
33
34 @Override
35 protected CompilerPass getProcessor(Compiler compiler) {
36 return new RemoveUnusedClassProperties(compiler);
37 }
38
39 public void testSimple1() {
40 // A property defined on "this" can be removed
41 test("this.a = 2", "2");
42 test("x = (this.a = 2)", "x = 2");
43 testSame("this.a = 2; x = this.a;");
44 }
45
46 public void testSimple2() {
47 // A property defined on "this" can be removed, even when defined
48 // as part of an expression
49 test("this.a = 2, f()", "2, f()");
50 test("x = (this.a = 2, f())", "x = (2, f())");
51 test("x = (f(), this.a = 2)", "x = (f(), 2)");
52 }
53
54 public void testSimple3() {
55 // A property defined on an object other than "this" can not be removed.
56 testSame("y.a = 2");
57 // but doesn't prevent the removal of the definition on 'this'.
58 test("y.a = 2; this.a = 2", "y.a = 2; 2");
59 // Some use of the property "a" prevents the removal.
60 testSame("y.a = 2; this.a = 1; alert(x.a)");
61 }
62
63 public void testObjLit() {
64 // A property defined on an object other than "this" can not be removed.
65 testSame("({a:2})");
66 // but doesn't prevent the removal of the definition on 'this'.
67 test("({a:0}); this.a = 1;", "({a:0});1");
68 // Some use of the property "a" prevents the removal.
69 testSame("x = ({a:0}); this.a = 1; alert(x.a)");
70 }
71
72 public void testExtern() {
73 // A property defined in the externs is can not be removed.
74 testSame("this.ext = 2");
75 }
76
77 public void testExport() {
78 // An exported property can not be removed.
79 testSame("this.ext = 2; window['export'] = this.ext;");
80 testSame("function f() { this.ext = 2; } window['export'] = this.ext;");
81 }
82
83
84 public void testAssignOp1() {
85 // Properties defined using a compound assignment can be removed if the
86 // result of the assignment expression is not immediately used.
87 test("this.x += 2", "2");
88 testSame("x = (this.x += 2)");
89 testSame("this.x += 2; x = this.x;");
90 // But, of course, a later use prevents its removal.
91 testSame("this.x += 2; x.x;");
92 }
93
94 public void testAssignOp2() {
95 // Properties defined using a compound assignment can be removed if the
96 // result of the assignment expression is not immediately used.
97 test("this.a += 2, f()", "2, f()");
98 test("x = (this.a += 2, f())", "x = (2, f())");
99 testSame("x = (f(), this.a += 2)");
100 }
101
102 public void testInc1() {
103 // Increments and Decrements are handled similarly to compound assignments
104 // but need a placeholder value when replaced.
105 test("this.x++", "0");
106 testSame("x = (this.x++)");
107 testSame("this.x++; x = this.x;");
108
109 test("--this.x", "0");
110 testSame("x = (--this.x)");
111 testSame("--this.x; x = this.x;");
112 }
113
114 public void testInc2() {
115 // Increments and Decrements are handled similarly to compound assignments
116 // but need a placeholder value when replaced.
117 test("this.a++, f()", "0, f()");
118 test("x = (this.a++, f())", "x = (0, f())");
119 testSame("x = (f(), this.a++)");
120
121 test("--this.a, f()", "0, f()");
122 test("x = (--this.a, f())", "x = (0, f())");
123 testSame("x = (f(), --this.a)");
124 }
125
126 public void testJSCompiler_renameProperty() {
127 // JSCompiler_renameProperty introduces a use of the property
128 testSame("this.a = 2; x[JSCompiler_renameProperty('a')]");
129 testSame("this.a = 2; JSCompiler_renameProperty('a')");
130 }
131
132 public void testForIn() {
133 // This is the basic assumption that this pass makes:
134 // it can remove properties even when the object is used in a FOR-IN loop
135 test("this.y = 1;for (var a in x) { alert(x[a]) }",
136 "1;for (var a in x) { alert(x[a]) }");
137 }
138
139 public void testObjectKeys() {
140 // This is the basic assumption that this pass makes:
141 // it can remove properties even when the object are referenced
142 test("this.y = 1;alert(Object.keys(this))",
143 "1;alert(Object.keys(this))");
144 }
145
146 public void testIssue730() {
147 // Partial removal of properties can causes problems if the object is
148 // sealed.
149 // TODO(johnlenz): should we not allow partial removals?
150 test(
151 "function A() {this.foo = 0;}\n" +
152 "function B() {this.a = new A();}\n" +
153 "B.prototype.dostuff = function() {this.a.foo++;alert('hi');}\n" +
154 "new B().dostuff();\n",
155 "function A(){0}" +
156 "function B(){this.a=new A}" +
157 "B.prototype.dostuff=function(){this.a.foo++;alert(\"hi\")};" +
158 "new B().dostuff();");
159 }
160 }