mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Report error for block scope function declaration in ES5
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user