Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2007 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 public class InlineSimpleMethodsTest extends CompilerTestCase {
20
21 public InlineSimpleMethodsTest() {
22 super("", false);
23 }
24
25 @Override
26 protected void setUp() throws Exception {
27 super.setUp();
28 super.enableLineNumberCheck(true);
29 }
30
31 @Override
32 protected CompilerPass getProcessor(Compiler compiler) {
33 return new InlineSimpleMethods(compiler);
34 }
35
36 /**
37 * Helper for tests that expects definitions to remain unchanged, such
38 * that {@code definitions+js} is converted to {@code definitions+expected}.
39 */
40 private void testWithPrefix(String definitions, String js, String expected) {
41 test(definitions + js, definitions + expected);
42 }
43
44 public void testSimpleInline1() {
45 testWithPrefix("function Foo(){}" +
46 "Foo.prototype.bar=function(){return this.baz};",
47 "var x=(new Foo).bar();var y=(new Foo).bar();",
48 "var x=(new Foo).baz;var y=(new Foo).baz");
49 }
50
51 public void testSimpleInline2() {
52 testWithPrefix("function Foo(){}" +
53 "Foo.prototype={bar:function(){return this.baz}};",
54 "var x=(new Foo).bar();var y=(new Foo).bar();",
55 "var x=(new Foo).baz;var y=(new Foo).baz");
56 }
57
58 public void testSimpleGetterInline1() {
59 // TODO(johnlenz): Support this case.
60 testSame("function Foo(){}" +
61 "Foo.prototype={get bar(){return this.baz}};" +
62 "var x=(new Foo).bar;var y=(new Foo).bar");
63 // Verify we are not confusing calling the result of an ES5 getter
64 // with call the getter.
65 testSame("function Foo(){}" +
66 "Foo.prototype={get bar(){return this.baz}};" +
67 "var x=(new Foo).bar();var y=(new Foo).bar()");
68 }
69
70 public void testSimpleSetterInline1() {
71 // Verify 'get' and 'set' are not confused.
72 testSame("function Foo(){}" +
73 "Foo.prototype={set bar(a){return this.baz}};" +
74 "var x=(new Foo).bar;var y=(new Foo).bar");
75 testSame("function Foo(){}" +
76 "Foo.prototype={set bar(a){return this.baz}};" +
77 "var x=(new Foo).bar();var y=(new Foo).bar()");
78 }
79
80 public void testSelfInline() {
81 testWithPrefix("function Foo(){}" +
82 "Foo.prototype.bar=function(){return this.baz};",
83 "Foo.prototype.meth=function(){this.bar();}",
84 "Foo.prototype.meth=function(){this.baz}");
85 }
86
87 public void testCallWithArgs() {
88 testWithPrefix("function Foo(){}" +
89 "Foo.prototype.bar=function(){return this.baz};",
90 "var x=(new Foo).bar(3,new Foo)",
91 "var x=(new Foo).bar(3,new Foo)");
92 }
93
94 public void testCallWithConstArgs() {
95 testWithPrefix("function Foo(){}" +
96 "Foo.prototype.bar=function(a){return this.baz};",
97 "var x=(new Foo).bar(3, 4)",
98 "var x=(new Foo).baz");
99 }
100
101 public void testNestedProperties() {
102 testWithPrefix("function Foo(){}" +
103 "Foo.prototype.bar=function(){return this.baz.ooka};",
104 "(new Foo).bar()",
105 "(new Foo).baz.ooka");
106 }
107
108 public void testSkipComplexMethods() {
109 testWithPrefix("function Foo(){}" +
110 "Foo.prototype.bar=function(){return this.baz};" +
111 "Foo.prototype.condy=function(){return this.baz?this.baz:1};",
112 "var x=(new Foo).argy()",
113 "var x=(new Foo).argy()");
114 }
115
116 public void testSkipConflictingMethods() {
117 testWithPrefix("function Foo(){}" +
118 "Foo.prototype.bar=function(){return this.baz};" +
119 "Foo.prototype.bar=function(){return this.bazz};",
120 "var x=(new Foo).bar()",
121 "var x=(new Foo).bar()");
122 }
123
124 public void testSameNamesDifferentDefinitions() {
125 testWithPrefix("function A(){}" +
126 "A.prototype.g=function(){return this.a};" +
127 "function B(){}" +
128 "B.prototype.g=function(){return this.b};",
129 "var x=(new A).g();" +
130 "var y=(new B).g();" +
131 "var a=new A;" +
132 "var ag=a.g();",
133 "var x=(new A).g();" +
134 "var y=(new B).g();" +
135 "var a=new A;" +
136 "var ag=a.g()");
137 }
138
139 public void testSameNamesSameDefinitions() {
140 testWithPrefix("function A(){}" +
141 "A.prototype.g=function(){return this.a};" +
142 "function B(){}" +
143 "B.prototype.g=function(){return this.a};",
144 "var x=(new A).g();" +
145 "var y=(new B).g();" +
146 "var a=new A;" +
147 "var ag=a.g();",
148 "var x=(new A).a;" +
149 "var y=(new B).a;" +
150 "var a=new A;" +
151 "var ag=a.a");
152 }
153
154 public void testConfusingNames() {
155 testWithPrefix("function Foo(){}" +
156 "Foo.prototype.bar=function(){return this.baz};",
157 "function bar(){var bar=function(){};bar()}",
158 "function bar(){var bar=function(){};bar()}");
159 }
160
161 public void testConstantInline() {
162 testWithPrefix("function Foo(){}" +
163 "Foo.prototype.bar=function(){return 3};",
164 "var f=new Foo;var x=f.bar()",
165 "var f=new Foo;var x=3");
166 }
167
168 public void testConstantArrayInline() {
169 testWithPrefix("function Foo(){}" +
170 "Foo.prototype.bar=function(){return[3,4]};",
171 "var f=new Foo;var x=f.bar()",
172 "var f=new Foo;var x=[3,4]");
173 }
174
175 public void testConstantInlineWithSideEffects() {
176 testWithPrefix("function Foo(){}" +
177 "Foo.prototype.bar=function(){return 3};",
178 "var x=(new Foo).bar()",
179 "var x=(new Foo).bar()");
180 }
181
182 public void testEmptyMethodInline() {
183 testWithPrefix("function Foo(){}" +
184 "Foo.prototype.bar=function(a){};",
185 "var x=new Foo; x.bar();",
186 "var x=new Foo");
187 }
188
189 public void testEmptyMethodInlineWithSideEffects() {
190 testWithPrefix("function Foo(){}" +
191 "Foo.prototype.bar=function(){};",
192 "(new Foo).bar();var y=new Foo;y.bar(new Foo)",
193 "(new Foo).bar();var y=new Foo;y.bar(new Foo)");
194 }
195
196 public void testEmptyMethodInlineInAssign1() {
197 testWithPrefix("function Foo(){}" +
198 "Foo.prototype.bar=function(){};",
199 "var x=new Foo;var y=x.bar()",
200 "var x=new Foo;var y=void 0");
201 }
202
203 public void testEmptyMethodInlineInAssign2() {
204 testWithPrefix("function Foo(){}" +
205 "Foo.prototype.bar=function(){};",
206 "var x=new Foo;var y=x.bar().toString()",
207 "var x=new Foo;var y=(void 0).toString()");
208 }
209
210 public void testNormalMethod() {
211 testWithPrefix("function Foo(){}" +
212 "Foo.prototype.bar=function(){var x=1};",
213 "var x=new Foo;x.bar()",
214 "var x=new Foo;x.bar()");
215 }
216
217 public void testNoInlineOfExternMethods1() {
218 testSame("var external={};external.charAt;",
219 "external.charAt()", (DiagnosticType) null);
220 }
221
222 public void testNoInlineOfExternMethods2() {
223 testSame("var external={};external.charAt=function(){};",
224 "external.charAt()", (DiagnosticType) null);
225 }
226
227 public void testNoInlineOfExternMethods3() {
228 testSame("var external={};external.bar=function(){};",
229 "function Foo(){}Foo.prototype.bar=function(){};(new Foo).bar()",
230 (DiagnosticType) null);
231 }
232
233 public void testNoInlineOfDangerousProperty() {
234 testSame("function Foo(){this.bar=3}" +
235 "Foo.prototype.bar=function(){};" +
236 "var x=new Foo;var y=x.bar()");
237 }
238
239 // Don't warn about argument naming conventions (this is done in another pass)
240 // opt_ parameters must not be followed by non-optional parameters.
241 // var_args must be last
242 public void testNoWarn() {
243 testSame("function Foo(){}" +
244 "Foo.prototype.bar=function(opt_a,b){var x=1};" +
245 "var x=new Foo;x.bar()");
246
247 testSame("function Foo(){}" +
248 "Foo.prototype.bar=function(var_args,b){var x=1};" +
249 "var x=new Foo;x.bar()");
250 }
251
252 public void testObjectLit() {
253 testSame("Foo.prototype.bar=function(){return this.baz_};" +
254 "var blah={bar:function(){}};" +
255 "(new Foo).bar()");
256 }
257
258 public void testObjectLit2() {
259 testSame("var blah={bar:function(){}};" +
260 "(new Foo).bar()");
261 }
262
263 public void testObjectLitExtern() {
264 String externs = "window.bridge={_sip:function(){}};";
265 testSame(externs, "window.bridge._sip()", null);
266 }
267
268 public void testExternFunction() {
269 String externs = "function emptyFunction() {}";
270 testSame(externs,
271 "function Foo(){this.empty=emptyFunction}" +
272 "(new Foo).empty()", null);
273 }
274
275 public void testIssue2508576_1() {
276 // Method defined by an extern should be left alone.
277 String externs = "function alert(a) {}";
278 testSame(externs, "({a:alert,b:alert}).a(\"a\")", null);
279 }
280
281 public void testIssue2508576_2() {
282 // Anonymous object definition with a side-effect should be left alone.
283 testSame("({a:function(){},b:x()}).a(\"a\")");
284 }
285
286 public void testIssue2508576_3() {
287 // Anonymous object definition without side-effect should be removed.
288 test("({a:function(){},b:alert}).a(\"a\")", "");
289 }
290
291 public void testAnonymousGet() {
292 // Anonymous object definition without side-effect should be removed.
293 testSame("({get a(){return function(){}},b:alert}).a(\"a\")");
294 testSame("({get a(){},b:alert}).a(\"a\")");
295 testSame("({get a(){},b:alert}).a");
296 }
297
298 public void testAnonymousSet() {
299 // Anonymous object definition without side-effect should be removed.
300 testSame("({set a(b){return function(){}},b:alert}).a(\"a\")");
301 testSame("({set a(b){},b:alert}).a(\"a\")");
302 testSame("({set a(b){},b:alert}).a");
303 }
304 }