Skip to content

Commit

Permalink
Fix tests so that they will pass when block-scoped functions stop par…
Browse files Browse the repository at this point in the history
…sing outside of ES6 mode.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=122777335
  • Loading branch information
shicks authored and blickly committed May 19, 2016
1 parent 7c0b3cd commit 78f7d43
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 71 deletions.
Expand Up @@ -416,7 +416,7 @@ public void testTopLevelNames2() {


public void testTopLevelNames3() { public void testTopLevelNames3() {
// Functions in blocks are not module top level because they are block scoped. // Functions in blocks are not module top level because they are block scoped.
test( testEs6(
LINE_JOINER.join( LINE_JOINER.join(
"goog.module('a.c');", "goog.module('a.c');",
"if (true) {", "if (true) {",
Expand Down
11 changes: 8 additions & 3 deletions test/com/google/javascript/jscomp/CodePrinterTest.java
Expand Up @@ -45,9 +45,6 @@ public void testPrint() {
// Safari: needs ';' at the end of a throw statement // Safari: needs ';' at the end of a throw statement
assertPrint("function foo(){throw 'error';}", assertPrint("function foo(){throw 'error';}",
"function foo(){throw\"error\";}"); "function foo(){throw\"error\";}");
// Safari 3 needs a "{" around a single function
assertPrint("if (true) function foo(){return}",
"if(true){function foo(){return}}");


assertPrint("var x = 10; { var y = 20; }", "var x=10;var y=20"); assertPrint("var x = 10; { var y = 20; }", "var x=10;var y=20");


Expand Down Expand Up @@ -238,6 +235,13 @@ public void testPrint() {
assertPrint("if(x)if(y);", "if(x)if(y);"); assertPrint("if(x)if(y);", "if(x)if(y);");
assertPrint("if(x){if(y);}", "if(x)if(y);"); assertPrint("if(x){if(y);}", "if(x)if(y);");
assertPrint("if(x){if(y){};;;}", "if(x)if(y);"); assertPrint("if(x){if(y){};;;}", "if(x)if(y);");
}

public void testPrintBlockScopedFunctions() {
languageMode = LanguageMode.ECMASCRIPT6;
// Safari 3 needs a "{" around a single function
assertPrint("if (true) function foo(){return}",
"if(true){function foo(){return}}");
assertPrint("if(x){;;function y(){};;}", "if(x){function y(){}}"); assertPrint("if(x){;;function y(){};;}", "if(x){function y(){}}");
} }


Expand Down Expand Up @@ -1418,6 +1422,7 @@ public void testDoLoopIECompatibility() {


public void testFunctionSafariCompatibility() { public void testFunctionSafariCompatibility() {
// Functions within IFs cause syntax errors on Safari. // Functions within IFs cause syntax errors on Safari.
languageMode = LanguageMode.ECMASCRIPT6;
assertPrint("function f(){if(e1){function goo(){return true}}else foo()}", assertPrint("function f(){if(e1){function goo(){return true}}else foo()}",
"function f(){if(e1){function goo(){return true}}else foo()}"); "function f(){if(e1){function goo(){return true}}else foo()}");


Expand Down
52 changes: 27 additions & 25 deletions test/com/google/javascript/jscomp/ControlFlowAnalysisTest.java
Expand Up @@ -917,45 +917,47 @@ public void testSimpleCatch() {


public void testFunctionWithinTry() { public void testFunctionWithinTry() {
// Make sure we don't search for the handler outside of the function. // Make sure we don't search for the handler outside of the function.
String src = "try { function f() {throw 1;} } catch (e) { }"; String src = "try { var f = function() {throw 1;} } catch (e) { }";
String expected = "digraph AST {\n" String expected = "digraph AST {\n"
+ " node [color=lightblue2, style=filled];\n" + " node [color=lightblue2, style=filled];\n"
+ " node0 [label=\"SCRIPT\"];\n" + " node0 [label=\"SCRIPT\"];\n"
+ " node1 [label=\"TRY\"];\n" + " node1 [label=\"TRY\"];\n"
+ " node0 -> node1 [weight=1];\n" + " node0 -> node1 [weight=1];\n"
+ " node2 [label=\"BLOCK\"];\n" + " node2 [label=\"BLOCK\"];\n"
+ " node1 -> node2 [weight=1];\n" + " node1 -> node2 [weight=1];\n"
+ " node3 [label=\"FUNCTION\"];\n" + " node3 [label=\"VAR\"];\n"
+ " node2 -> node3 [weight=1];\n" + " node2 -> node3 [weight=1];\n"
+ " node4 [label=\"NAME\"];\n" + " node4 [label=\"NAME\"];\n"
+ " node3 -> node4 [weight=1];\n" + " node3 -> node4 [weight=1];\n"
+ " node5 [label=\"PARAM_LIST\"];\n" + " node5 [label=\"FUNCTION\"];\n"
+ " node3 -> node5 [weight=1];\n" + " node4 -> node5 [weight=1];\n"
+ " node6 [label=\"BLOCK\"];\n" + " node6 [label=\"NAME\"];\n"
+ " node3 -> node6 [weight=1];\n" + " node5 -> node6 [weight=1];\n"
+ " node7 [label=\"THROW\"];\n" + " node7 [label=\"PARAM_LIST\"];\n"
+ " node6 -> node7 [weight=1];\n" + " node5 -> node7 [weight=1];\n"
+ " node8 [label=\"NUMBER\"];\n" + " node8 [label=\"BLOCK\"];\n"
+ " node7 -> node8 [weight=1];\n" + " node5 -> node8 [weight=1];\n"
+ " node6 -> node7 [label=\"UNCOND\", " + + " node9 [label=\"THROW\"];\n"
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + " node8 -> node9 [weight=1];\n"
+ " node3 -> node6 [label=\"UNCOND\", " + + " node10 [label=\"NUMBER\"];\n"
+ " node9 -> node10 [weight=1];\n"
+ " node3 -> RETURN [label=\"UNCOND\", " +
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" "fontcolor=\"red\", weight=0.01, color=\"red\"];\n"
+ " node2 -> RETURN [label=\"UNCOND\", " + + " node2 -> node3 [label=\"UNCOND\", " +
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" "fontcolor=\"red\", weight=0.01, color=\"red\"];\n"
+ " node9 [label=\"BLOCK\"];\n" + " node11 [label=\"BLOCK\"];\n"
+ " node1 -> node9 [weight=1];\n" + " node1 -> node11 [weight=1];\n"
+ " node10 [label=\"CATCH\"];\n" + " node12 [label=\"CATCH\"];\n"
+ " node9 -> node10 [weight=1];\n" + " node11 -> node12 [weight=1];\n"
+ " node11 [label=\"NAME\"];\n" + " node13 [label=\"NAME\"];\n"
+ " node10 -> node11 [weight=1];\n" + " node12 -> node13 [weight=1];\n"
+ " node12 [label=\"BLOCK\"];\n" + " node14 [label=\"BLOCK\"];\n"
+ " node10 -> node12 [weight=1];\n" + " node12 -> node14 [weight=1];\n"
+ " node12 -> RETURN [label=\"UNCOND\", " + + " node14 -> RETURN [label=\"UNCOND\", " +
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" "fontcolor=\"red\", weight=0.01, color=\"red\"];\n"
+ " node10 -> node12 [label=\"UNCOND\", " + + " node12 -> node14 [label=\"UNCOND\", " +
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" "fontcolor=\"red\", weight=0.01, color=\"red\"];\n"
+ " node9 -> node10 [label=\"UNCOND\", " + + " node11 -> node12 [label=\"UNCOND\", " +
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" "fontcolor=\"red\", weight=0.01, color=\"red\"];\n"
+ " node1 -> node2 [label=\"UNCOND\", " + + " node1 -> node2 [label=\"UNCOND\", " +
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" "fontcolor=\"red\", weight=0.01, color=\"red\"];\n"
Expand Down
Expand Up @@ -269,6 +269,7 @@ public void testFunctionNonMovement1() {
// 2) if it's in an if statement, we can't move it // 2) if it's in an if statement, we can't move it
// 3) if it's in an while statement, we can't move it [with some extra // 3) if it's in an while statement, we can't move it [with some extra
// block elements] // block elements]
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
testSame(createModuleStar( testSame(createModuleStar(
// m1 // m1
"function f(){};f.prototype.bar=new f;" + "function f(){};f.prototype.bar=new f;" +
Expand Down
Expand Up @@ -31,6 +31,7 @@
*/ */
public final class DefinitionsRemoverTest extends CompilerTestCase { public final class DefinitionsRemoverTest extends CompilerTestCase {
public void testRemoveFunction() { public void testRemoveFunction() {
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
testSame("{(function (){bar()})}"); testSame("{(function (){bar()})}");
test("{function a(){bar()}}", "{}"); test("{function a(){bar()}}", "{}");
test("foo(); function a(){} bar()", "foo(); bar();"); test("foo(); function a(){} bar()", "foo(); bar();");
Expand Down
Expand Up @@ -217,6 +217,7 @@ public void testRewriteInGlobalScope() throws Exception {
} }


public void testNoRewriteIfNotInGlobalScope1() throws Exception { public void testNoRewriteIfNotInGlobalScope1() throws Exception {
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
testSame("if(true){" + NoRewriteIfNotInGlobalScopeTestInput.INPUT + "}"); testSame("if(true){" + NoRewriteIfNotInGlobalScopeTestInput.INPUT + "}");
} }


Expand Down
1 change: 1 addition & 0 deletions test/com/google/javascript/jscomp/InlineFunctionsTest.java
Expand Up @@ -113,6 +113,7 @@ public void testInlineEmptyFunction5() {
} }


public void testInlineEmptyFunction6() { public void testInlineEmptyFunction6() {
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
test("if (window) { f(); function f() {} }", test("if (window) { f(); function f() {} }",
"if (window) { void 0; }"); "if (window) { void 0; }");
} }
Expand Down
1 change: 1 addition & 0 deletions test/com/google/javascript/jscomp/InlineVariablesTest.java
Expand Up @@ -427,6 +427,7 @@ public void testNoInlineIntoNamedFunction() {
} }


public void testInlineIntoNestedNonHoistedNamedFunctions() { public void testInlineIntoNestedNonHoistedNamedFunctions() {
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
test("f(); var x = false; if (false) function f() { alert(x); };", test("f(); var x = false; if (false) function f() { alert(x); };",
"f(); if (false) function f() { alert(false); };"); "f(); if (false) function f() { alert(false); };");
} }
Expand Down
Expand Up @@ -261,6 +261,7 @@ public void testForContinueOptimization() throws Exception {
} }


public void testCodeMotionDoesntBreakFunctionHoisting() throws Exception { public void testCodeMotionDoesntBreakFunctionHoisting() throws Exception {
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
fold("function f() { if (x) return; foo(); function foo() {} }", fold("function f() { if (x) return; foo(); function foo() {} }",
"function f() { if (x); else { function foo() {} foo(); } }"); "function f() { if (x); else { function foo() {} foo(); } }");
} }
Expand Down
Expand Up @@ -41,6 +41,7 @@ public void testFunctionsExpression() {
} }


public void testNoMoveDeepFunctionDeclarations() { public void testNoMoveDeepFunctionDeclarations() {
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
testSame("a; if (a) function f(){};"); testSame("a; if (a) function f(){};");
testSame("a; if (a) { function f(){} }"); testSame("a; if (a) { function f(){} }");
} }
Expand Down
Expand Up @@ -8778,26 +8778,6 @@ public void testSpecializeLooseNullDoesntCrash() {
"}")); "}"));
} }


public void testOuterVarDefinitionJoinDoesntCrash() {
typeCheck(LINE_JOINER.join(
"/** @constructor */ function Foo(){}",
"function f() {",
" if (true) {",
" function g() { new Foo; }",
" g();",
" }",
"}"));

// typeCheck(LINE_JOINER.join(
// "function f() {",
// " if (true) {",
// " function g() { new Foo; }",
// " g();",
// " }",
// "}"),
// VarCheck.UNDEFINED_VAR_ERROR);
}

public void testUnparameterizedArrayDefinitionDoesntCrash() { public void testUnparameterizedArrayDefinitionDoesntCrash() {
typeCheckCustomExterns( typeCheckCustomExterns(
DEFAULT_EXTERNS + LINE_JOINER.join( DEFAULT_EXTERNS + LINE_JOINER.join(
Expand Down
20 changes: 20 additions & 0 deletions test/com/google/javascript/jscomp/NewTypeInferenceES6Test.java
Expand Up @@ -201,4 +201,24 @@ public void testMethodOverridesWithoutJsdoc() {
"(new Bar).method('str');"), "(new Bar).method('str');"),
NewTypeInference.WRONG_ARGUMENT_COUNT); NewTypeInference.WRONG_ARGUMENT_COUNT);
} }

public void testOuterVarDefinitionJoinDoesntCrash() {
typeCheck(LINE_JOINER.join(
"/** @constructor */ function Foo(){}",
"function f() {",
" if (true) {",
" function g() { new Foo; }",
" g();",
" }",
"}"));

// typeCheck(LINE_JOINER.join(
// "function f() {",
// " if (true) {",
// " function g() { new Foo; }",
// " g();",
// " }",
// "}"),
// VarCheck.UNDEFINED_VAR_ERROR);
}
} }
3 changes: 3 additions & 0 deletions test/com/google/javascript/jscomp/NormalizeTest.java
Expand Up @@ -197,6 +197,7 @@ public void testMoveFunctions2() throws Exception {
testSame("function f() { function foo() {} }"); testSame("function f() { function foo() {} }");
test("function f() { f(); a:function bar() {} }", test("function f() { f(); a:function bar() {} }",
"function f() { f(); a:{ var bar = function () {} }}"); "function f() { f(); a:{ var bar = function () {} }}");
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
test("function f() { f(); {function bar() {}}}", test("function f() { f(); {function bar() {}}}",
"function f() { f(); {var bar = function () {}}}"); "function f() { f(); {var bar = function () {}}}");
test("function f() { f(); if (true) {function bar() {}}}", test("function f() { f(); if (true) {function bar() {}}}",
Expand All @@ -216,6 +217,7 @@ private void testInFunction(String code, String expected) {
} }


public void testNormalizeFunctionDeclarations() throws Exception { public void testNormalizeFunctionDeclarations() throws Exception {
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
testSame("function f() {}"); testSame("function f() {}");
testSame("var f = function () {}"); testSame("var f = function () {}");
test("var f = function f() {}", test("var f = function f() {}",
Expand Down Expand Up @@ -315,6 +317,7 @@ public void testRemoveDuplicateVarDeclarations2() {
} }


public void testRemoveDuplicateVarDeclarations3() { public void testRemoveDuplicateVarDeclarations3() {
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
test("var f = 1; function f(){}", test("var f = 1; function f(){}",
"f = 1; function f(){}"); "f = 1; function f(){}");
test("var f; function f(){}", test("var f; function f(){}",
Expand Down
Expand Up @@ -95,7 +95,7 @@ public void testFoldBlocksWithManyChildren() {
fold("function f() { if (false) {} }", "function f(){}"); fold("function f() { if (false) {} }", "function f(){}");
fold("function f() { { if (false) {} if (true) {} {} } }", fold("function f() { { if (false) {} if (true) {} {} } }",
"function f(){}"); "function f(){}");
fold("{var x; var y; var z; function f() { { var a; { var b; } } } }", testEs6("{var x; var y; var z; function f() { { var a; { var b; } } } }",
"var x;var y;var z;function f(){var a;var b}"); "var x;var y;var z;function f(){var a;var b}");
} }


Expand Down
Expand Up @@ -48,6 +48,7 @@ protected CompilerPass getProcessor(final Compiler compiler) {
} }


public void testRemoveUnusedVars() { public void testRemoveUnusedVars() {
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
// Test lots of stuff // Test lots of stuff
test("var a;var b=3;var c=function(){};var x=A();var y; var z;" + test("var a;var b=3;var c=function(){};var x=A();var y; var z;" +
"function A(){B()} function B(){C(b)} function C(){} " + "function A(){B()} function B(){C(b)} function C(){} " +
Expand Down
1 change: 1 addition & 0 deletions test/com/google/javascript/jscomp/RenameLabelsTest.java
Expand Up @@ -33,6 +33,7 @@ public void testRenameInFunction() {
"function x(){ a(); }"); "function x(){ a(); }");
test("function x(){ Foo:{ a(); break Foo; } }", test("function x(){ Foo:{ a(); break Foo; } }",
"function x(){ a:{ a(); break a; } }"); "function x(){ a:{ a(); break a; } }");
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
test("function x() { " + test("function x() { " +
"Foo:{ " + "Foo:{ " +
"function goo() {" + "function goo() {" +
Expand Down
Expand Up @@ -144,6 +144,7 @@ public void testFunctionStatements() {
test( test(
"function test(){}", "function test(){}",
"_.test=function (){}"); "_.test=function (){}");
setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
test( test(
"if(1)function test(){}", "if(1)function test(){}",
"if(1)_.test=function (){}"); "if(1)_.test=function (){}");
Expand Down
1 change: 1 addition & 0 deletions test/com/google/javascript/jscomp/ScopedAliasesTest.java
Expand Up @@ -866,6 +866,7 @@ public void testNonAliasLocal() {
} }


public void testInvalidVariableInScope() { public void testInvalidVariableInScope() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT6);
testScopedError("if (true) { function f() {}}", ScopedAliases.GOOG_SCOPE_INVALID_VARIABLE); testScopedError("if (true) { function f() {}}", ScopedAliases.GOOG_SCOPE_INVALID_VARIABLE);
testScopedError("for (;;) { function f() {}}", ScopedAliases.GOOG_SCOPE_INVALID_VARIABLE); testScopedError("for (;;) { function f() {}}", ScopedAliases.GOOG_SCOPE_INVALID_VARIABLE);
} }
Expand Down
6 changes: 3 additions & 3 deletions test/com/google/javascript/jscomp/StrictModeCheckTest.java
Expand Up @@ -230,17 +230,17 @@ public void testFunctionDecl() {
testSame(inFn("(function() {})();")); testSame(inFn("(function() {})();"));
testSame(inFn("(function() {});")); testSame(inFn("(function() {});"));


testError("{function g() {}}", StrictModeCheck.BAD_FUNCTION_DECLARATION);
testSame("{var g = function () {}}"); testSame("{var g = function () {}}");
testSame("{(function g() {})()}"); testSame("{(function g() {})()}");
testErrorEs6("{function g() {}}", StrictModeCheck.BAD_FUNCTION_DECLARATION);


testError("var x;if (x) { function g(){} }", StrictModeCheck.BAD_FUNCTION_DECLARATION);
testSame("var x;if (x) {var g = function () {}}"); testSame("var x;if (x) {var g = function () {}}");
testSame("var x;if (x) {(function g() {})()}"); testSame("var x;if (x) {(function g() {})()}");
testErrorEs6("var x;if (x) { function g(){} }", StrictModeCheck.BAD_FUNCTION_DECLARATION);
} }


public void testFunctionDecl2() { public void testFunctionDecl2() {
testError("{function g() {}}", StrictModeCheck.BAD_FUNCTION_DECLARATION); testErrorEs6("{function g() {}}", StrictModeCheck.BAD_FUNCTION_DECLARATION);
} }


public void testClass() { public void testClass() {
Expand Down
Expand Up @@ -52,6 +52,7 @@ private Node getRoot(String js) {
} }


public void testFunctionScope() { public void testFunctionScope() {
compiler.getOptions().setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT6);
Scope scope = getScope("function foo() {}\n" + Scope scope = getScope("function foo() {}\n" +
"var x = function bar(a1) {};" + "var x = function bar(a1) {};" +
"[function bar2() { var y; }];" + "[function bar2() { var y; }];" +
Expand Down
2 changes: 2 additions & 0 deletions test/com/google/javascript/jscomp/TypeCheckTest.java
Expand Up @@ -8392,6 +8392,7 @@ public void testCast15() {
// Some code assumes that an object literal must have a object type, // Some code assumes that an object literal must have a object type,
// while because of the cast, it could have any type (including // while because of the cast, it could have any type (including
// a union). // a union).
compiler.getOptions().setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT6);
testTypes( testTypes(
"for (var i = 0; i < 10; i++) {" + "for (var i = 0; i < 10; i++) {" +
"var x = /** @type {Object|number} */ ({foo: 3});" + "var x = /** @type {Object|number} */ ({foo: 3});" +
Expand Down Expand Up @@ -8713,6 +8714,7 @@ public void testConstDecl1() {
} }


public void testConstDecl2() { public void testConstDecl2() {
compiler.getOptions().setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT6);
testTypes( testTypes(
"/** @param {?number} x */" + "/** @param {?number} x */" +
"function f(x) { " + "function f(x) { " +
Expand Down
4 changes: 2 additions & 2 deletions test/com/google/javascript/jscomp/VarCheckTest.java
Expand Up @@ -267,8 +267,8 @@ public void testVarInWithBlock() {
} }


public void testFunctionDeclaredInBlock() { public void testFunctionDeclaredInBlock() {
testError("if (true) {function foo() {}} foo();", VarCheck.UNDEFINED_VAR_ERROR); testErrorEs6("if (true) {function foo() {}} foo();", VarCheck.UNDEFINED_VAR_ERROR);
testError("foo(); if (true) {function foo() {}}", VarCheck.UNDEFINED_VAR_ERROR); testErrorEs6("foo(); if (true) {function foo() {}}", VarCheck.UNDEFINED_VAR_ERROR);


testSameEs6("if (true) {var foo = ()=>{}} foo();"); testSameEs6("if (true) {var foo = ()=>{}} foo();");
testErrorEs6("if (true) {let foo = ()=>{}} foo();", VarCheck.UNDEFINED_VAR_ERROR); testErrorEs6("if (true) {let foo = ()=>{}} foo();", VarCheck.UNDEFINED_VAR_ERROR);
Expand Down

0 comments on commit 78f7d43

Please sign in to comment.