1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*
* Copyright 2010 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
public class GroupVariableDeclarationsTest extends CompilerTestCase {
@Override
protected void setUp() {
super.enableLineNumberCheck(false);
}
public void testGroupingUninitializedVarsInScope() {
// basic with just one fn call in between
test("var a = 1; f1(); var b;", "var a = 1, b; f1();");
// > 1 line of code in between
test("var a = \"mangoes\"; f1(); alert(a); var b;",
"var a = \"mangoes\", b; f1(); alert(a);");
// nested block in between that also contains a declaration + initialization
// of the variable that needs to be collapsed
test("var a = 1; {var c; alert(c);} var b;",
"var a = 1, c, b; {alert(c);}");
// multiple variables
test("var a = 1; var b = 1; f1(); f2(); var c; var d;",
"var a = 1, b, c, d; b = 1; f1(); f2();");
test("var a = 1; var b = 2; var c; f1(); f2(); var d, e;",
"var a = 1, b, c, d, e; b = 2; f1(); f2();");
test("var a = 1, b = 2, c; f1(); f2(); var d; var e; " +
"f3(); f4(); var f = 10; var g; var h = a + b;",
"var a = 1, b = 2, c, d, e, f, g, h; f1(); f2(); f3(); f4(); " +
"f = 10; h = a + b;");
}
public void testGroupingInitializedVarsInScope() {
// basic with just one fn call in between
test("var a = 1; f1(); var b = 2;", "var a = 1, b; f1(); b = 2;");
// > 1 line of code in between
test("var a = \"mangoes\"; f1(); alert(a); var b = 2;",
"var a = \"mangoes\", b; f1(); alert(a); b = 2;");
// nested block in between that also contains a declaration + initialization
// of the variable that needs to be collapsed
test("var a = 1; {var c = 34; alert(c);} var b = 2;",
"var a = 1, c, b; {c = 34; alert(c);} b = 2;");
// multiple variables
test("var a = 1; var b = 1; f1(); f2(); var c = 3; var d = 4;",
"var a = 1, b, c, d; b = 1; f1(); f2(); c = 3; d = 4;");
test("var a = 1; var b = 2; var c; f1(); f2(); var d = 4, e;",
"var a = 1, b, c, d, e; b = 2; f1(); f2(); d = 4;");
test("var a = 1, b = 2, c; f1(); f2(); var d; var e = 6; " +
"f3(); f4(); var f; var g; var h = a + b;",
"var a = 1, b = 2, c, d, e, f, g, h; f1(); f2(); e = 6; " +
"f3(); f4(); h = a + b;");
}
public void testGroupingVarsInForAndForInLoops() {
// test for loop
test("var a = 1; for (var x = 0; x < 10; ++x) {a++;} var y;",
"var a = 1, x, y; for (x = 0; x < 10; ++x) {a++;}");
test("var a = 1, x; for (x = 0; x < 10; ++x) {a++;} var y;",
"var a = 1, x, y; for (x = 0; x < 10; ++x) {a++;}");
test("var a = 1, x; for (x; x < 10; ++x) {a++;} var y;",
"var a = 1, x, y; for (x; x < 10; ++x) {a++;}");
test("var a = 1; for (; a < 10; ++a) {alert(a);} var y;",
"var a = 1, y; for (; a < 10; ++a) {alert(a);}");
test("var a = 1; for (var x; x < 10; ++x) {a += 2;} var y = 5;",
"var a = 1, x, y; for (; x < 10; ++x) {a += 2;} y = 5;");
// multiple loop-control variables in for loop
test("var a = 1; " +
"for (var a1 = 0, a2 = 10; a1 < 10 && a2 > 0; ++a1, --a2) {}" +
"var x = 5;",
"var a = 1, x;" +
"for (var a1 = 0, a2 = 10; a1 < 10 && a2 > 0; ++a1, --a2) {} " +
"x = 5;");
test("var a = 1; " +
"for (var a1 = 0, a2; a1 < 10 && a2 > 0; ++a1, --a2) {}" +
"var x = 5;",
"var a = 1, a1, a2, x;" +
"for (a1 = 0; a1 < 10 && a2 > 0; ++a1, --a2) {}" +
"x = 5;");
test("var a = 1; " +
"for (var a1, a2; a1 < 10 && a2 > 0; ++a1, --a2) {}" +
"var x = 5;",
"var a = 1, a1, a2, x;" +
"for (; a1 < 10 && a2 > 0; ++a1, --a2) {}" +
"x = 5;");
// test for-in loop
test("var a = [1, 2, 3, 4]; for (var z in a) {alert(z);} var y;",
"var a = [1, 2, 3, 4], z, y; for (z in a) {alert(z);}");
test("var a = [1, 2, 3, 4]; for (var z in a) {alert(z);} var y = 5;",
"var a = [1, 2, 3, 4], z, y; for (z in a) {alert(z);} y = 5;");
test("var a; for (var z in a = [1, 2, 3, 4]) {alert(z);} var y, x = 5;",
"var a, z, y, x; for (z in a = [1, 2, 3, 4]) {alert(z);} x = 5;");
test("var a; for (var z = 1 in a = [1, 2, 3, 4]) {alert(z);} var y, x = 5;",
"var a, y, x; for (var z = 1 in a = [1, 2, 3, 4]) {alert(z);} x = 5;");
test("var a, z; for (z in a = [1, 2, 3, 4]) {alert(z);} var y, x = 5;",
"var a, z, y, x; for (z in a = [1, 2, 3, 4]) {alert(z);} x = 5;");
}
public void testGroupingVarsNestedFunction() {
test("function f(b) {var x; function g() {var x; a = x; var y;} var a;}",
"function f(b) {var x, a; function g() {var x, y; a = x;}}");
}
public void testGroupingVarsInnerFunction() {
test("function f(b) {var x; h = x * x; var myfn = function() " +
"{var x; a = x; var y;}; var a;}",
"function f(b) {var x, myfn, a; h = x * x; myfn = function() " +
"{var x, y; a = x;};}");
}
public void testGroupingVarsFirstStatementNotVar() {
test("f(); var a; g(); var b;", "f(); var a, b; g();");
}
public void testGroupingVarsInScopeRegtest() {
// regtest with lots of different scopes
test("var x = 0, y = 1, z;" +
"function f1(aa, bb) {" +
" if (y) {" +
" if (x === 0) {" +
" var h, r = 999;" +
" }" +
" } else {" +
" r = 1000;" +
" }" +
" var mylist = [1, 2, 3, 4];" +
" var k1 = 200, k2 = 400;" +
" for (var i1 = 0; i1 < 10; ++i1) {" +
" for (var i2 in mylist) {" +
" alert(i1);" +
" }" +
" }" +
" var jam, q = 100;" +
" var myfn = function() {" +
" var x = 1;" +
" f5();" +
" var z = 5;" +
" };" +
" function f5() {" +
" var aa = 5;" +
" if (y === 1) {" +
" var x = 100;" +
" }" +
" }" +
"}" +
"var h = x + y;" +
"function g() {" +
" y = 0;" +
" { var x = 200;}" +
" var h = y + x;" +
"}" +
"var ggg = 0;", // End of input
"var x = 0, y = 1, z, h, ggg;" +
"function f1(aa, bb) {" +
" if (y) {" +
" if (x === 0) {" +
" var h, r = 999, mylist, i1, i2, jam, q, myfn;" +
" }" +
" } else {" +
" r = 1000;" +
" }" +
" mylist = [1, 2, 3, 4];" +
" var k1 = 200, k2 = 400;" +
" for (i1 = 0; i1 < 10; ++i1) {" +
" for (i2 in mylist) {" +
" alert(i1);" +
" }" +
" }" +
" q = 100; " +
" myfn = function() {" +
" var x = 1, z;" +
" f5();" +
" z = 5;" +
" };" +
" function f5() {" +
" var aa = 5, x;" +
" if (y === 1) {" +
" x = 100;" +
" }" +
" }" +
"}" +
"h = x + y;" +
"function g() {" +
" y = 0;" +
" { var x = 200, h;}" +
" h = y + x;" +
"}" +
"ggg = 0;");
}
@Override
protected CompilerPass getProcessor(Compiler compiler) {
return new GroupVariableDeclarations(compiler);
}
}
|