Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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.Lists;
20 import com.google.common.collect.Sets;
21 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
22 import com.google.javascript.rhino.Node;
23
24 import java.util.Set;
25
26 /**
27 * @author [email protected] (John Lenz)
28 *
29 */
30 public class NormalizeTest extends CompilerTestCase {
31
32 private static final String EXTERNS = "var window;";
33
34 public NormalizeTest() {
35 super(EXTERNS);
36 super.enableLineNumberCheck(true);
37 }
38
39 @Override
40 public CompilerPass getProcessor(final Compiler compiler) {
41 return new Normalize(compiler, false);
42 }
43
44 @Override
45 protected int getNumRepetitions() {
46 // The normalize pass is only run once.
47 return 1;
48 }
49
50 public void testSplitVar() {
51 testSame("var a");
52 test("var a, b",
53 "var a; var b");
54 test("var a, b, c",
55 "var a; var b; var c");
56 testSame("var a = 0 ");
57 test("var a = 0 , b = foo()",
58 "var a = 0; var b = foo()");
59 test("var a = 0, b = 1, c = 2",
60 "var a = 0; var b = 1; var c = 2");
61 test("var a = foo(1), b = foo(2), c = foo(3)",
62 "var a = foo(1); var b = foo(2); var c = foo(3)");
63
64 // Verify vars extracted from FOR nodes are split.
65 test("for(var a = 0, b = foo(1), c = 1; c < b; c++) foo(2)",
66 "var a = 0; var b = foo(1); var c = 1; for(; c < b; c++) foo(2)");
67
68 // Verify split vars properly introduce blocks when needed.
69 test("for(;;) var b = foo(1), c = foo(2);",
70 "for(;;){var b = foo(1); var c = foo(2)}");
71 test("for(;;){var b = foo(1), c = foo(2);}",
72 "for(;;){var b = foo(1); var c = foo(2)}");
73
74 test("try{var b = foo(1), c = foo(2);} finally foo(3);",
75 "try{var b = foo(1); var c = foo(2)} finally foo(3);");
76 test("try{var b = foo(1),c = foo(2);} finally;",
77 "try{var b = foo(1); var c = foo(2)} finally;");
78 test("try{foo(0);} finally var b = foo(1), c = foo(2);",
79 "try{foo(0);} finally {var b = foo(1); var c = foo(2)}");
80
81 test("switch(a) {default: var b = foo(1), c = foo(2); break;}",
82 "switch(a) {default: var b = foo(1); var c = foo(2); break;}");
83
84 test("do var a = foo(1), b; while(false);",
85 "do{var a = foo(1); var b} while(false);");
86 test("a:var a,b,c;",
87 "a:{ var a;var b; var c; }");
88 test("a:for(var a,b,c;;);",
89 "var a;var b; var c;a:for(;;);");
90 test("if (true) a:var a,b;",
91 "if (true)a:{ var a; var b; }");
92 }
93
94 public void testDuplicateVarInExterns() {
95 test("var extern;",
96 "/** @suppress {duplicate} */ var extern = 3;", "var extern = 3;",
97 null, null);
98 }
99
100 public void testUnhandled() {
101 testSame("var x = y = 1");
102 }
103
104 public void testFor() {
105 // Verify assignments are extracted from the FOR init node.
106 test("for(a = 0; a < 2 ; a++) foo();",
107 "a = 0; for(; a < 2 ; a++) foo()");
108 // Verify vars are extracted from the FOR init node.
109 test("for(var a = 0; c < b ; c++) foo()",
110 "var a = 0; for(; c < b ; c++) foo()");
111
112 // Verify vars are extracted from the FOR init before the label node.
113 test("a:for(var a = 0; c < b ; c++) foo()",
114 "var a = 0; a:for(; c < b ; c++) foo()");
115 // Verify vars are extracted from the FOR init before the labels node.
116 test("a:b:for(var a = 0; c < b ; c++) foo()",
117 "var a = 0; a:b:for(; c < b ; c++) foo()");
118
119 // Verify block are properly introduced for ifs.
120 test("if(x) for(var a = 0; c < b ; c++) foo()",
121 "if(x){var a = 0; for(; c < b ; c++) foo()}");
122
123 // Any other expression.
124 test("for(init(); a < 2 ; a++) foo();",
125 "init(); for(; a < 2 ; a++) foo()");
126 }
127
128 public void testForIn1() {
129 // Verify nothing happens with simple for-in
130 testSame("for(a in b) foo();");
131
132 // Verify vars are extracted from the FOR-IN node.
133 test("for(var a in b) foo()",
134 "var a; for(a in b) foo()");
135
136 // Verify vars are extracted from the FOR init before the label node.
137 test("a:for(var a in b) foo()",
138 "var a; a:for(a in b) foo()");
139 // Verify vars are extracted from the FOR init before the labels node.
140 test("a:b:for(var a in b) foo()",
141 "var a; a:b:for(a in b) foo()");
142
143 // Verify block are properly introduced for ifs.
144 test("if (x) for(var a in b) foo()",
145 "if (x) { var a; for(a in b) foo() }");
146 }
147
148 public void testForIn2() {
149 // Verify vars are extracted from the FOR-IN node.
150 test("for(var a = foo() in b) foo()",
151 "var a = foo(); for(a in b) foo()");
152 }
153
154 public void testWhile() {
155 // Verify while loops are converted to FOR loops.
156 test("while(c < b) foo()",
157 "for(; c < b;) foo()");
158 }
159
160 public void testMoveFunctions1() throws Exception {
161 test("function f() { if (x) return; foo(); function foo() {} }",
162 "function f() {function foo() {} if (x) return; foo(); }");
163 test("function f() { " +
164 "function foo() {} " +
165 "if (x) return;" +
166 "foo(); " +
167 "function bar() {} " +
168 "}",
169 "function f() {" +
170 "function foo() {}" +
171 "function bar() {}" +
172 "if (x) return;" +
173 "foo();" +
174 "}");
175 }
176
177 public void testMoveFunctions2() throws Exception {
178 testSame("function f() { function foo() {} }");
179 test("function f() { f(); a:function bar() {} }",
180 "function f() { f(); a:{ var bar = function () {} }}");
181 test("function f() { f(); {function bar() {}}}",
182 "function f() { f(); {var bar = function () {}}}");
183 test("function f() { f(); if (true) {function bar() {}}}",
184 "function f() { f(); if (true) {var bar = function () {}}}");
185 }
186
187 private String inFunction(String code) {
188 return "(function(){" + code + "})";
189 }
190
191 private void testSameInFunction(String code) {
192 testSame(inFunction(code));
193 }
194
195 private void testInFunction(String code, String expected) {
196 test(inFunction(code), inFunction(expected));
197 }
198
199 public void testNormalizeFunctionDeclarations() throws Exception {
200 testSame("function f() {}");
201 testSame("var f = function () {}");
202 test("var f = function f() {}",
203 "var f = function f$$1() {}");
204 testSame("var f = function g() {}");
205 test("a:function g() {}",
206 "a:{ var g = function () {} }");
207 test("{function g() {}}",
208 "{var g = function () {}}");
209 testSame("if (function g() {}) {}");
210 test("if (true) {function g() {}}",
211 "if (true) {var g = function () {}}");
212 test("if (true) {} else {function g() {}}",
213 "if (true) {} else {var g = function () {}}");
214 testSame("switch (function g() {}) {}");
215 test("switch (1) { case 1: function g() {}}",
216 "switch (1) { case 1: var g = function () {}}");
217
218
219 testSameInFunction("function f() {}");
220 testInFunction("f(); a:function g() {}",
221 "f(); a:{ var g = function () {} }");
222 testInFunction("f(); {function g() {}}",
223 "f(); {var g = function () {}}");
224 testInFunction("f(); if (true) {function g() {}}",
225 "f(); if (true) {var g = function () {}}");
226 testInFunction("if (true) {} else {function g() {}}",
227 "if (true) {} else {var g = function () {}}");
228 }
229
230
231 public void testMakeLocalNamesUnique() {
232 if (!Normalize.MAKE_LOCAL_NAMES_UNIQUE) {
233 return;
234 }
235
236 // Verify global names are untouched.
237 testSame("var a;");
238
239 // Verify global names are untouched.
240 testSame("a;");
241
242 // Local names are made unique.
243 test("var a;function foo(a){var b;a}",
244 "var a;function foo(a$$1){var b;a$$1}");
245 test("var a;function foo(){var b;a}function boo(){var b;a}",
246 "var a;function foo(){var b;a}function boo(){var b$$1;a}");
247 test("function foo(a){var b}" +
248 "function boo(a){var b}",
249 "function foo(a){var b}" +
250 "function boo(a$$1){var b$$1}");
251
252 // Verify function expressions are renamed.
253 test("var a = function foo(){foo()};var b = function foo(){foo()};",
254 "var a = function foo(){foo()};var b = function foo$$1(){foo$$1()};");
255
256 // Verify catch exceptions names are made unique
257 test("try { } catch(e) {e;}",
258 "try { } catch(e) {e;}");
259 test("try { } catch(e) {e;}; try { } catch(e) {e;}",
260 "try { } catch(e) {e;}; try { } catch(e$$1) {e$$1;}");
261 test("try { } catch(e) {e; try { } catch(e) {e;}};",
262 "try { } catch(e) {e; try { } catch(e$$1) {e$$1;} }; ");
263
264 // Verify the 1st global redefinition of extern definition is not removed.
265 test("/** @suppress {duplicate} */\nvar window;", "var window;");
266
267 // Verify the 2nd global redefinition of extern definition is removed.
268 test("/** @suppress {duplicate} */\nvar window;" +
269 "/** @suppress {duplicate} */\nvar window;", "var window;");
270
271 // Verify local masking extern made unique.
272 test("function f() {var window}",
273 "function f() {var window$$1}");
274 }
275
276 public void testRemoveDuplicateVarDeclarations1() {
277 test("function f() { var a; var a }",
278 "function f() { var a; }");
279 test("function f() { var a = 1; var a = 2 }",
280 "function f() { var a = 1; a = 2 }");
281 test("var a = 1; function f(){ var a = 2 }",
282 "var a = 1; function f(){ var a$$1 = 2 }");
283 test("function f() { var a = 1; lable1:var a = 2 }",
284 "function f() { var a = 1; lable1:{a = 2}}");
285 test("function f() { var a = 1; lable1:var a }",
286 "function f() { var a = 1; lable1:{} }");
287 test("function f() { var a = 1; for(var a in b); }",
288 "function f() { var a = 1; for(a in b); }");
289 }
290
291 public void testRemoveDuplicateVarDeclarations2() {
292 test("var e = 1; function f(){ try {} catch (e) {} var e = 2 }",
293 "var e = 1; function f(){ try {} catch (e$$2) {} var e$$1 = 2 }");
294 }
295
296 public void testRemoveDuplicateVarDeclarations3() {
297 test("var f = 1; function f(){}",
298 "f = 1; function f(){}");
299 test("var f; function f(){}",
300 "function f(){}");
301 test("if (a) { var f = 1; } else { function f(){} }",
302 "if (a) { var f = 1; } else { f = function (){} }");
303
304 test("function f(){} var f = 1;",
305 "function f(){} f = 1;");
306 test("function f(){} var f;",
307 "function f(){}");
308 test("if (a) { function f(){} } else { var f = 1; }",
309 "if (a) { var f = function (){} } else { f = 1; }");
310
311 // TODO(johnlenz): Do we need to handle this differently for "third_party"
312 // mode? Remove the previous function definitions?
313 test("function f(){} function f(){}",
314 "function f(){} function f(){}",
315 SyntacticScopeCreator.VAR_MULTIPLY_DECLARED_ERROR);
316 test("if (a) { function f(){} } else { function f(){} }",
317 "if (a) { var f = function (){} } else { f = function (){} }");
318 }
319
320 public void testRenamingConstants() {
321 test("var ACONST = 4;var b = ACONST;",
322 "var ACONST = 4; var b = ACONST;");
323
324 test("var a, ACONST = 4;var b = ACONST;",
325 "var a; var ACONST = 4; var b = ACONST;");
326
327 test("var ACONST; ACONST = 4; var b = ACONST;",
328 "var ACONST; ACONST = 4;" +
329 "var b = ACONST;");
330
331 test("var ACONST = new Foo(); var b = ACONST;",
332 "var ACONST = new Foo(); var b = ACONST;");
333
334 test("/** @const */var aa; aa=1;", "var aa;aa=1");
335 }
336
337 public void testSkipRenamingExterns() {
338 test("var EXTERN; var ext; ext.FOO;", "var b = EXTERN; var c = ext.FOO",
339 "var b = EXTERN; var c = ext.FOO", null, null);
340 }
341
342 public void testIssue166a() {
343 test("try { throw 1 } catch(e) { /** @suppress {duplicate} */ var e=2 }",
344 "try { throw 1 } catch(e) { var e=2 }",
345 Normalize.CATCH_BLOCK_VAR_ERROR);
346 }
347
348 public void testIssue166b() {
349 test("function a() {" +
350 "try { throw 1 } catch(e) { /** @suppress {duplicate} */ var e=2 }" +
351 "};",
352 "function a() {" +
353 "try { throw 1 } catch(e) { var e=2 }" +
354 "}",
355 Normalize.CATCH_BLOCK_VAR_ERROR);
356 }
357
358 public void testIssue166c() {
359 test("var e = 0; try { throw 1 } catch(e) {" +
360 "/** @suppress {duplicate} */ var e=2 }",
361 "var e = 0; try { throw 1 } catch(e) { var e=2 }",
362 Normalize.CATCH_BLOCK_VAR_ERROR);
363 }
364
365 public void testIssue166d() {
366 test("function a() {" +
367 "var e = 0; try { throw 1 } catch(e) {" +
368 "/** @suppress {duplicate} */ var e=2 }" +
369 "};",
370 "function a() {" +
371 "var e = 0; try { throw 1 } catch(e) { var e=2 }" +
372 "}",
373 Normalize.CATCH_BLOCK_VAR_ERROR);
374 }
375
376 public void testIssue166e() {
377 test("var e = 2; try { throw 1 } catch(e) {}",
378 "var e = 2; try { throw 1 } catch(e$$1) {}");
379 }
380
381 public void testIssue166f() {
382 test("function a() {" +
383 "var e = 2; try { throw 1 } catch(e) {}" +
384 "}",
385 "function a() {" +
386 "var e = 2; try { throw 1 } catch(e$$1) {}" +
387 "}");
388 }
389
390 public void testIssue() {
391 super.allowExternsChanges(true);
392 test("var a,b,c; var a,b", "a(), b()", "a(), b()", null, null);
393 }
394
395 public void testNormalizeSyntheticCode() {
396 Compiler compiler = new Compiler();
397 compiler.init(
398 Lists.<SourceFile>newArrayList(),
399 Lists.<SourceFile>newArrayList(), new CompilerOptions());
400 Node code = Normalize.parseAndNormalizeSyntheticCode(
401 compiler, "function f(x) {} function g(x) {}", "prefix_");
402 assertEquals(
403 "function f(x$$prefix_0){}function g(x$$prefix_1){}",
404 compiler.toSource(code));
405 }
406
407 public void testIsConstant() throws Exception {
408 testSame("var CONST = 3; var b = CONST;");
409 Node n = getLastCompiler().getRoot();
410
411 Set<Node> constantNodes = findNodesWithProperty(n, Node.IS_CONSTANT_NAME);
412 assertEquals(2, constantNodes.size());
413 for (Node hasProp : constantNodes) {
414 assertEquals("CONST", hasProp.getString());
415 }
416 }
417
418 public void testPropertyIsConstant1() throws Exception {
419 testSame("var a = {};a.CONST = 3; var b = a.CONST;");
420 Node n = getLastCompiler().getRoot();
421
422 Set<Node> constantNodes = findNodesWithProperty(n, Node.IS_CONSTANT_NAME);
423 assertEquals(2, constantNodes.size());
424 for (Node hasProp : constantNodes) {
425 assertEquals("CONST", hasProp.getString());
426 }
427 }
428
429 public void testPropertyIsConstant2() throws Exception {
430 testSame("var a = {CONST: 3}; var b = a.CONST;");
431 Node n = getLastCompiler().getRoot();
432
433 Set<Node> constantNodes = findNodesWithProperty(n, Node.IS_CONSTANT_NAME);
434 assertEquals(2, constantNodes.size());
435 for (Node hasProp : constantNodes) {
436 assertEquals("CONST", hasProp.getString());
437 }
438 }
439
440 public void testGetterPropertyIsConstant() throws Exception {
441 testSame("var a = { get CONST() {return 3} }; " +
442 "var b = a.CONST;");
443 Node n = getLastCompiler().getRoot();
444
445 Set<Node> constantNodes = findNodesWithProperty(n, Node.IS_CONSTANT_NAME);
446 assertEquals(2, constantNodes.size());
447 for (Node hasProp : constantNodes) {
448 assertEquals("CONST", hasProp.getString());
449 }
450 }
451
452 public void testSetterPropertyIsConstant() throws Exception {
453 // Verifying that a SET is properly annotated.
454 testSame("var a = { set CONST(b) {throw 'invalid'} }; " +
455 "var c = a.CONST;");
456 Node n = getLastCompiler().getRoot();
457
458 Set<Node> constantNodes = findNodesWithProperty(n, Node.IS_CONSTANT_NAME);
459 assertEquals(2, constantNodes.size());
460 for (Node hasProp : constantNodes) {
461 assertEquals("CONST", hasProp.getString());
462 }
463 }
464
465 public void testExposeSimple() {
466 test("var x = {}; /** @expose */ x.y = 3; x.y = 5;",
467 "var x = {}; x['y'] = 3; x['y'] = 5;");
468 }
469
470 public void testExposeComplex() {
471 test(
472 "var x = {/** @expose */ a: 1, b: 2};"
473 + "x.a = 3; /** @expose */ x.b = 5;",
474 "var x = {'a': 1, 'b': 2};"
475 + "x['a'] = 3; x['b'] = 5;");
476 }
477
478 private Set<Node> findNodesWithProperty(Node root, final int prop) {
479 final Set<Node> set = Sets.newHashSet();
480 NodeTraversal.traverse(
481 getLastCompiler(), root, new AbstractPostOrderCallback() {
482 @Override
483 public void visit(NodeTraversal t, Node node, Node parent) {
484 if (node.getBooleanProp(prop)) {
485 set.add(node);
486 }
487 }
488 });
489 return set;
490 }
491
492 public void testRenamingConstantProperties() {
493 // In order to detect that foo.BAR is a constant, we need collapse
494 // properties to run first so that we can tell if the initial value is
495 // non-null and immutable.
496 new WithCollapse().testConstantProperties();
497 }
498
499 private class WithCollapse extends CompilerTestCase {
500 WithCollapse() {
501 enableNormalize();
502 }
503
504 private void testConstantProperties() {
505 test("var a={}; a.ACONST = 4;var b = a.ACONST;",
506 "var a$ACONST = 4; var b = a$ACONST;");
507
508 test("var a={b:{}}; a.b.ACONST = 4;var b = a.b.ACONST;",
509 "var a$b$ACONST = 4;var b = a$b$ACONST;");
510
511 test("var a = {FOO: 1};var b = a.FOO;",
512 "var a$FOO = 1; var b = a$FOO;");
513
514 test("var EXTERN; var ext; ext.FOO;", "var b = EXTERN; var c = ext.FOO",
515 "var b = EXTERN; var c = ext.FOO", null, null);
516
517 test("var a={}; a.ACONST = 4; var b = a.ACONST;",
518 "var a$ACONST = 4; var b = a$ACONST;");
519
520 test("var a = {}; function foo() { var d = a.CONST; };" +
521 "(function(){a.CONST=4})();",
522 "var a$CONST;function foo(){var d = a$CONST;};" +
523 "(function(){a$CONST = 4})();");
524
525 test("var a = {}; a.ACONST = new Foo(); var b = a.ACONST;",
526 "var a$ACONST = new Foo(); var b = a$ACONST;");
527 }
528
529 @Override
530 protected int getNumRepetitions() {
531 // The normalize pass is only run once.
532 return 1;
533 }
534
535 @Override
536 public CompilerPass getProcessor(final Compiler compiler) {
537 return new CompilerPass() {
538 @Override
539 public void process(Node externs, Node root) {
540 new CollapseProperties(compiler, false, true).process(externs, root);
541 }
542 };
543 }
544 }
545 }