Debian Codegraph @

test/com/google/javascript/jscomp/CollapseVariableDeclarationsTest.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 variable declaration collapsing.
21 *
22 */
23 public class CollapseVariableDeclarationsTest extends CompilerTestCase {
24 public void testCollapsing() throws Exception {
25 // Basic collapsing
26 test("var a;var b;",
27 "var a,b;");
28 // With initial values
29 test("var a = 1;var b = 1;",
30 "var a=1,b=1;");
31 // Already collapsed
32 test("var a, b;",
33 "var a,b;");
34 // Already collapsed with values
35 test("var a = 1, b = 1;",
36 "var a=1,b=1;");
37 // Some already collapsed
38 test("var a;var b, c;var d;",
39 "var a,b,c,d;");
40 // Some already collapsed with values
41 test("var a = 1;var b = 2, c = 3;var d = 4;",
42 "var a=1,b=2,c=3,d=4;");
43 }
44
45 public void testIssue820() throws Exception {
46 // Don't redeclare function parameters, this is incompatible with
47 // strict mode.
48 testSame("function f(a){ var b=1; a=2; var c; }");
49 }
50
51 public void testIfElseVarDeclarations() throws Exception {
52 testSame("if (x) var a = 1; else var b = 2;");
53 }
54
55 public void testAggressiveRedeclaration() {
56 test("var x = 2; foo(x); x = 3; var y = 2;",
57 "var x = 2; foo(x); var x = 3, y = 2;");
58
59 test("var x = 2; foo(x); x = 3; x = 1; var y = 2;",
60 "var x = 2; foo(x); var x = 3, x = 1, y = 2;");
61
62 test("var x = 2; foo(x); x = 3; x = 1; var y = 2; var z = 4",
63 "var x = 2; foo(x); var x = 3, x = 1, y = 2, z = 4");
64
65 test("var x = 2; foo(x); x = 3; x = 1; var y = 2; var z = 4; x = 5",
66 "var x = 2; foo(x); var x = 3, x = 1, y = 2, z = 4, x = 5");
67 }
68
69 public void testAggressiveRedeclarationInFor() {
70 testSame("for(var x = 1; x = 2; x = 3) {x = 4}");
71 testSame("for(var x = 1; y = 2; z = 3) {var a = 4}");
72 testSame("var x; for(x = 1; x = 2; z = 3) {x = 4}");
73 }
74
75 public void testIssue397() {
76 test("var x; var y = 3; x = 5;",
77 "var x, y = 3; x = 5;");
78 testSame("var x; x = 5; var z = 7;");
79 test("var x; var y = 3; x = 5; var z = 7;",
80 "var x, y = 3; x = 5; var z = 7;");
81 test("var a = 1; var x; var y = 3; x = 5;",
82 "var a = 1, x, y = 3; x = 5;");
83 }
84
85 @Override
86 protected CompilerPass getProcessor(Compiler compiler) {
87 return new CollapseVariableDeclarations(compiler);
88 }
89 }