Debian Codegraph @

test/com/google/javascript/jscomp/InlineObjectLiteralsTest.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 * Verifies that valid candidates for object literals are inlined as
21 * expected, and invalid candidates are not touched.
22 *
23 */
24 public class InlineObjectLiteralsTest extends CompilerTestCase {
25
26 public InlineObjectLiteralsTest() {
27 enableNormalize();
28 }
29
30 @Override
31 public void setUp() {
32 super.enableLineNumberCheck(true);
33 }
34
35 @Override
36 protected CompilerPass getProcessor(final Compiler compiler) {
37 return new InlineObjectLiterals(
38 compiler,
39 compiler.getUniqueNameIdSupplier());
40 }
41
42 // Test object literal -> variable inlining
43
44 public void testObject0() {
45 // Don't mess with global variables, that is the job of CollapseProperties.
46 testSame("var a = {x:1}; f(a.x);");
47 }
48
49 public void testObject1() {
50 testLocal("var a = {x:x(), y:y()}; f(a.x, a.y);",
51 "var JSCompiler_object_inline_x_0=x();" +
52 "var JSCompiler_object_inline_y_1=y();" +
53 "f(JSCompiler_object_inline_x_0, JSCompiler_object_inline_y_1);");
54 }
55
56 public void testObject1a() {
57 testLocal("var a; a = {x:x, y:y}; f(a.x, a.y);",
58 "var JSCompiler_object_inline_x_0;" +
59 "var JSCompiler_object_inline_y_1;" +
60 "(JSCompiler_object_inline_x_0=x," +
61 "JSCompiler_object_inline_y_1=y, true);" +
62 "f(JSCompiler_object_inline_x_0, JSCompiler_object_inline_y_1);");
63 }
64
65 public void testObject2() {
66 testLocal("var a = {y:y}; a.x = z; f(a.x, a.y);",
67 "var JSCompiler_object_inline_y_0 = y;" +
68 "var JSCompiler_object_inline_x_1;" +
69 "JSCompiler_object_inline_x_1=z;" +
70 "f(JSCompiler_object_inline_x_1, JSCompiler_object_inline_y_0);");
71 }
72
73 public void testObject3() {
74 // Inlining the 'y' would cause the 'this' to be different in the
75 // target function.
76 testSameLocal("var a = {y:y,x:x}; a.y(); f(a.x);");
77 testSameLocal("var a; a = {y:y,x:x}; a.y(); f(a.x);");
78 }
79
80 public void testObject4() {
81 // Object literal is escaped.
82 testSameLocal("var a = {y:y}; a.x = z; f(a.x, a.y); g(a);");
83 testSameLocal("var a; a = {y:y}; a.x = z; f(a.x, a.y); g(a);");
84 }
85
86 public void testObject5() {
87 testLocal("var a = {x:x, y:y}; var b = {a:a}; f(b.a.x, b.a.y);",
88 "var a = {x:x, y:y};" +
89 "var JSCompiler_object_inline_a_0=a;" +
90 "f(JSCompiler_object_inline_a_0.x, JSCompiler_object_inline_a_0.y);");
91 }
92
93 public void testObject6() {
94 testLocal("for (var i = 0; i < 5; i++) { var a = {i:i,x:x}; f(a.i, a.x); }",
95 "for (var i = 0; i < 5; i++) {" +
96 " var JSCompiler_object_inline_i_0=i;" +
97 " var JSCompiler_object_inline_x_1=x;" +
98 " f(JSCompiler_object_inline_i_0,JSCompiler_object_inline_x_1)" +
99 "}");
100 testLocal("if (c) { var a = {i:i,x:x}; f(a.i, a.x); }",
101 "if (c) {" +
102 " var JSCompiler_object_inline_i_0=i;" +
103 " var JSCompiler_object_inline_x_1=x;" +
104 " f(JSCompiler_object_inline_i_0,JSCompiler_object_inline_x_1)" +
105 "}");
106 }
107
108 public void testObject7() {
109 testLocal("var a = {x:x, y:f()}; g(a.x);",
110 "var JSCompiler_object_inline_x_0=x;" +
111 "var JSCompiler_object_inline_y_1=f();" +
112 "g(JSCompiler_object_inline_x_0)");
113 }
114
115 public void testObject8() {
116 testSameLocal("var a = {x:x,y:y}; var b = {x:y}; f((c?a:b).x);");
117
118 testLocal("var a; if(c) { a={x:x, y:y}; } else { a={x:y}; } f(a.x);",
119 "var JSCompiler_object_inline_x_0;" +
120 "var JSCompiler_object_inline_y_1;" +
121 "if(c) JSCompiler_object_inline_x_0=x," +
122 " JSCompiler_object_inline_y_1=y," +
123 " true;" +
124 "else JSCompiler_object_inline_x_0=y," +
125 " JSCompiler_object_inline_y_1=void 0," +
126 " true;" +
127 "f(JSCompiler_object_inline_x_0)");
128 testLocal("var a = {x:x,y:y}; var b = {x:y}; c ? f(a.x) : f(b.x);",
129 "var JSCompiler_object_inline_x_0 = x; " +
130 "var JSCompiler_object_inline_y_1 = y; " +
131 "var JSCompiler_object_inline_x_2 = y; " +
132 "c ? f(JSCompiler_object_inline_x_0):f(JSCompiler_object_inline_x_2)");
133 }
134
135 public void testObject9() {
136 // There is a call, so no inlining
137 testSameLocal("function f(a,b) {" +
138 " var x = {a:a,b:b}; x.a(); return x.b;" +
139 "}");
140
141 testLocal("function f(a,b) {" +
142 " var x = {a:a,b:b}; g(x.a); x = {a:a,b:2}; return x.b;" +
143 "}",
144 "function f(a,b) {" +
145 " var JSCompiler_object_inline_a_0 = a;" +
146 " var JSCompiler_object_inline_b_1 = b;" +
147 " g(JSCompiler_object_inline_a_0);" +
148 " JSCompiler_object_inline_a_0 = a," +
149 " JSCompiler_object_inline_b_1=2," +
150 " true;" +
151 " return JSCompiler_object_inline_b_1" +
152 "}");
153
154 testLocal("function f(a,b) { " +
155 " var x = {a:a,b:b}; g(x.a); x.b = x.c = 2; return x.b; " +
156 "}",
157 "function f(a,b) { " +
158 " var JSCompiler_object_inline_a_0=a;" +
159 " var JSCompiler_object_inline_b_1=b; " +
160 " var JSCompiler_object_inline_c_2;" +
161 " g(JSCompiler_object_inline_a_0);" +
162 " JSCompiler_object_inline_b_1=JSCompiler_object_inline_c_2=2;" +
163 " return JSCompiler_object_inline_b_1" +
164 "}");
165 }
166
167 public void testObject10() {
168 testLocal("var x; var b = f(); x = {a:a, b:b}; if(x.a) g(x.b);",
169 "var JSCompiler_object_inline_a_0;" +
170 "var JSCompiler_object_inline_b_1;" +
171 "var b = f();" +
172 "JSCompiler_object_inline_a_0=a,JSCompiler_object_inline_b_1=b,true;" +
173 "if(JSCompiler_object_inline_a_0) g(JSCompiler_object_inline_b_1)");
174 testLocal("var x = {}; var b = f(); x = {a:a, b:b}; if(x.a) g(x.b) + x.c",
175 "var x = {}; var b = f(); x = {a:a, b:b}; if(x.a) g(x.b) + x.c");
176 testLocal("var x; var b = f(); x = {a:a, b:b}; x.c = c; if(x.a) g(x.b) + x.c",
177 "var JSCompiler_object_inline_a_0;" +
178 "var JSCompiler_object_inline_b_1;" +
179 "var JSCompiler_object_inline_c_2;" +
180 "var b = f();" +
181 "JSCompiler_object_inline_a_0 = a,JSCompiler_object_inline_b_1 = b, " +
182 " JSCompiler_object_inline_c_2=void 0,true;" +
183 "JSCompiler_object_inline_c_2 = c;" +
184 "if (JSCompiler_object_inline_a_0)" +
185 " g(JSCompiler_object_inline_b_1) + JSCompiler_object_inline_c_2;");
186 testLocal("var x = {a:a}; if (b) x={b:b}; f(x.a||x.b);",
187 "var JSCompiler_object_inline_a_0 = a;" +
188 "var JSCompiler_object_inline_b_1;" +
189 "if(b) JSCompiler_object_inline_b_1 = b," +
190 " JSCompiler_object_inline_a_0 = void 0," +
191 " true;" +
192 "f(JSCompiler_object_inline_a_0 || JSCompiler_object_inline_b_1)");
193 testLocal("var x; var y = 5; x = {a:a, b:b, c:c}; if (b) x={b:b}; f(x.a||x.b);",
194 "var JSCompiler_object_inline_a_0;" +
195 "var JSCompiler_object_inline_b_1;" +
196 "var JSCompiler_object_inline_c_2;" +
197 "var y=5;" +
198 "JSCompiler_object_inline_a_0=a," +
199 "JSCompiler_object_inline_b_1=b," +
200 "JSCompiler_object_inline_c_2=c," +
201 "true;" +
202 "if (b) JSCompiler_object_inline_b_1=b," +
203 " JSCompiler_object_inline_a_0=void 0," +
204 " JSCompiler_object_inline_c_2=void 0," +
205 " true;" +
206 "f(JSCompiler_object_inline_a_0||JSCompiler_object_inline_b_1)");
207 }
208
209 public void testObject11() {
210 testSameLocal("var x = {a:b}; (x = {a:a}).c = 5; f(x.a);");
211 testSameLocal("var x = {a:a}; f(x[a]); g(x[a]);");
212 }
213
214 public void testObject12() {
215 testLocal("var a; a = {x:1, y:2}; f(a.x, a.y2);",
216 "var a; a = {x:1, y:2}; f(a.x, a.y2);");
217 }
218
219 public void testObject13() {
220 testSameLocal("var x = {a:1, b:2}; x = {a:3, b:x.a};");
221 }
222
223 public void testObject14() {
224 testSameLocal("var x = {a:1}; if ('a' in x) { f(); }");
225 testSameLocal("var x = {a:1}; for (var y in x) { f(y); }");
226 }
227
228 public void testObject15() {
229 testSameLocal("x = x || {}; f(x.a);");
230 }
231
232 public void testObject16() {
233 testLocal("function f(e) { bar(); x = {a: foo()}; var x; print(x.a); }",
234 "function f(e) { " +
235 " var JSCompiler_object_inline_a_0;" +
236 " bar();" +
237 " JSCompiler_object_inline_a_0 = foo(), true;" +
238 " print(JSCompiler_object_inline_a_0);" +
239 "}");
240 }
241
242 public void testObject17() {
243 // Note: Some day, with careful analysis, these two uses could be
244 // disambiguated, and the second assignment could be inlined.
245 testSameLocal(
246 "var a = {a: function(){}};" +
247 "a.a();" +
248 "a = {a1: 100};" +
249 "print(a.a1);");
250 }
251
252 public void testObject18() {
253 testSameLocal("var a,b; b=a={x:x, y:y}; f(b.x);");
254 }
255
256 public void testObject19() {
257 testSameLocal("var a,b; if(c) { b=a={x:x, y:y}; } else { b=a={x:y}; } f(b.x);");
258 }
259
260 public void testObject20() {
261 testSameLocal("var a,b; if(c) { b=a={x:x, y:y}; } else { b=a={x:y}; } f(a.x);");
262 }
263
264 public void testObject21() {
265 testSameLocal("var a,b; b=a={x:x, y:y};");
266 testSameLocal("var a,b; if(c) { b=a={x:x, y:y}; }" +
267 "else { b=a={x:y}; } f(a.x); f(b.x)");
268 testSameLocal("var a, b; if(c) { if (a={x:x, y:y}) f(); } " +
269 "else { b=a={x:y}; } f(a.x);");
270 testSameLocal("var a,b; b = (a = {x:x, y:x});");
271 testSameLocal("var a,b; a = {x:x, y:x}; b = a");
272 testSameLocal("var a,b; a = {x:x, y:x}; b = x || a");
273 testSameLocal("var a,b; a = {x:x, y:x}; b = y && a");
274 testSameLocal("var a,b; a = {x:x, y:x}; b = y ? a : a");
275 testSameLocal("var a,b; a = {x:x, y:x}; b = y , a");
276 testSameLocal("b = x || (a = {x:1, y:2});");
277 }
278
279 public void testObject22() {
280 testLocal("while(1) { var a = {y:1}; if (b) a.x = 2; f(a.y, a.x);}",
281 "for(;1;){" +
282 " var JSCompiler_object_inline_y_0=1;" +
283 " var JSCompiler_object_inline_x_1;" +
284 " if(b) JSCompiler_object_inline_x_1=2;" +
285 " f(JSCompiler_object_inline_y_0,JSCompiler_object_inline_x_1)" +
286 "}");
287
288 testLocal("var a; while (1) { f(a.x, a.y); a = {x:1, y:1};}",
289 "var a; while (1) { f(a.x, a.y); a = {x:1, y:1};}");
290 }
291
292 public void testObject23() {
293 testLocal("function f() {\n" +
294 " var templateData = {\n" +
295 " linkIds: {\n" +
296 " CHROME: 'cl',\n" +
297 " DISMISS: 'd'\n" +
298 " }\n" +
299 " };\n" +
300 " var html = templateData.linkIds.CHROME \n" +
301 " + \":\" + templateData.linkIds.DISMISS;\n" +
302 "}",
303 "function f(){" +
304 "var JSCompiler_object_inline_CHROME_1='cl';" +
305 "var JSCompiler_object_inline_DISMISS_2='d';" +
306 "var html=JSCompiler_object_inline_CHROME_1 +" +
307 " ':' +JSCompiler_object_inline_DISMISS_2}");
308 }
309
310 public void testObject24() {
311 testLocal("function f() {\n" +
312 " var linkIds = {\n" +
313 " CHROME: 1,\n" +
314 " };\n" +
315 " var g = function () {var o = {a: linkIds};}\n" +
316 "}",
317 "function f(){var linkIds={CHROME:1};" +
318 "var g=function(){var JSCompiler_object_inline_a_0=linkIds}}");
319 }
320
321 public void testObject25() {
322 testLocal("var a = {x:f(), y:g()}; a = {y:g(), x:f()}; f(a.x, a.y);",
323 "var JSCompiler_object_inline_x_0=f();" +
324 "var JSCompiler_object_inline_y_1=g();" +
325 "JSCompiler_object_inline_y_1=g()," +
326 " JSCompiler_object_inline_x_0=f()," +
327 " true;" +
328 "f(JSCompiler_object_inline_x_0,JSCompiler_object_inline_y_1)");
329 }
330
331 public void testObject26() {
332 testLocal("var a = {}; a.b = function() {}; new a.b.c",
333 "var JSCompiler_object_inline_b_0;" +
334 "JSCompiler_object_inline_b_0=function(){};" +
335 "new JSCompiler_object_inline_b_0.c");
336 }
337
338 public void testBug545() {
339 testLocal("var a = {}", "");
340 testLocal("var a; a = {}", "true");
341 }
342
343 public void testIssue724() {
344 testSameLocal(
345 "var getType; getType = {};" +
346 "return functionToCheck && " +
347 " getType.toString.apply(functionToCheck) === " +
348 " '[object Function]';");
349 }
350
351 public void testNoInlineDeletedProperties() {
352 testSameLocal(
353 "var foo = {bar:1};" +
354 "delete foo.bar;" +
355 "return foo.bar;");
356 }
357
358 private final String LOCAL_PREFIX = "function local(){";
359 private final String LOCAL_POSTFIX = "}";
360
361 private void testLocal(String code, String result) {
362 test(LOCAL_PREFIX + code + LOCAL_POSTFIX,
363 LOCAL_PREFIX + result + LOCAL_POSTFIX);
364 }
365
366 private void testSameLocal(String code) {
367 testLocal(code, code);
368 }
369 }