Report error for block scope function declaration in ES5

This commit is contained in:
Sheetal Nandi 2016-04-11 12:04:55 -07:00
parent 6988a0a479
commit 593dc2bc9f
3 changed files with 46 additions and 1 deletions

View File

@ -1125,6 +1125,35 @@ namespace ts {
}
}
function getStrictModeBlockScopeFunctionDeclarationMessage(node: Node) {
// Provide specialized messages to help the user understand why we think they're in
// strict mode.
if (getContainingClass(node)) {
return Diagnostics.In_ES5_or_lower_function_declarations_are_not_allowed_in_block_scope_Class_definitions_are_automatically_in_strict_mode;
}
if (file.externalModuleIndicator) {
return Diagnostics.In_ES5_or_lower_function_declarations_are_not_allowed_in_block_scope_Modules_are_automatically_in_strict_mode;
}
return Diagnostics.In_ES5_or_lower_function_declarations_are_not_allowed_in_block_scope_in_strict_mode;
}
function checkStrictModeFunctionDeclaration(node: FunctionDeclaration) {
if (getEmitScriptTarget(options) < ScriptTarget.ES6) {
// Report error if function is not top level function declaration
if (blockScopeContainer.kind !== SyntaxKind.SourceFile &&
blockScopeContainer.kind !== SyntaxKind.ModuleDeclaration &&
!isFunctionLike(blockScopeContainer)) {
// We check first if the name is inside class declaration or class expression; if so give explicit message
// otherwise report generic error message.
const errorSpan = getErrorSpanForNode(file, node);
file.bindDiagnostics.push(createFileDiagnostic(file, errorSpan.start, errorSpan.length,
getStrictModeBlockScopeFunctionDeclarationMessage(node)));
}
}
}
function checkStrictModeNumericLiteral(node: LiteralExpression) {
if (inStrictMode && node.isOctalLiteral) {
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode));
@ -1641,6 +1670,7 @@ namespace ts {
checkStrictModeFunctionName(<FunctionDeclaration>node);
if (inStrictMode) {
checkStrictModeFunctionDeclaration(node);
bindBlockScopedDeclaration(node, SymbolFlags.Function, SymbolFlags.FunctionExcludes);
}
else {

View File

@ -811,6 +811,18 @@
"category": "Error",
"code": 1249
},
"In ES5 or lower, function declarations are not allowed in block scope in strict mode.": {
"category": "Error",
"code": 1250
},
"In ES5 or lower, function declarations are not allowed in block scope. Class definitions are automatically in strict mode.": {
"category": "Error",
"code": 1251
},
"In ES5 or lower, function declarations are not allowed in block scope. Modules are automatically in strict mode.": {
"category": "Error",
"code": 1252
},
"'with' statements are not allowed in an async function block.": {
"category": "Error",
"code": 1300

View File

@ -1,10 +1,13 @@
tests/cases/compiler/blockScopedFunctionDeclarationStrictES5.ts(3,14): error TS1250: In ES5 or lower, function declarations are not allowed in block scope in strict mode.
tests/cases/compiler/blockScopedFunctionDeclarationStrictES5.ts(6,1): error TS2304: Cannot find name 'foo'.
==== tests/cases/compiler/blockScopedFunctionDeclarationStrictES5.ts (1 errors) ====
==== tests/cases/compiler/blockScopedFunctionDeclarationStrictES5.ts (2 errors) ====
"use strict";
if (true) {
function foo() { } // Error to declare function in block scope
~~~
!!! error TS1250: In ES5 or lower, function declarations are not allowed in block scope in strict mode.
foo(); // This call should be ok
}
foo(); // Error to find name foo