This repository has been archived on 2023-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
ima02/resources/defects4j-checkout-closure-1f/test/com/google/javascript/jscomp/GroupVariableDeclarationsTest.java

210 lines
7.8 KiB
Java
Raw Permalink Normal View History

2023-04-25 11:33:41 +00:00
/*
* 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);
}
}