Skip to content

Commit 593dc2b

Browse files
committed
Report error for block scope function declaration in ES5
1 parent 6988a0a commit 593dc2b

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/compiler/binder.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,35 @@ namespace ts {
11251125
}
11261126
}
11271127

1128+
function getStrictModeBlockScopeFunctionDeclarationMessage(node: Node) {
1129+
// Provide specialized messages to help the user understand why we think they're in
1130+
// strict mode.
1131+
if (getContainingClass(node)) {
1132+
return Diagnostics.In_ES5_or_lower_function_declarations_are_not_allowed_in_block_scope_Class_definitions_are_automatically_in_strict_mode;
1133+
}
1134+
1135+
if (file.externalModuleIndicator) {
1136+
return Diagnostics.In_ES5_or_lower_function_declarations_are_not_allowed_in_block_scope_Modules_are_automatically_in_strict_mode;
1137+
}
1138+
1139+
return Diagnostics.In_ES5_or_lower_function_declarations_are_not_allowed_in_block_scope_in_strict_mode;
1140+
}
1141+
1142+
function checkStrictModeFunctionDeclaration(node: FunctionDeclaration) {
1143+
if (getEmitScriptTarget(options) < ScriptTarget.ES6) {
1144+
// Report error if function is not top level function declaration
1145+
if (blockScopeContainer.kind !== SyntaxKind.SourceFile &&
1146+
blockScopeContainer.kind !== SyntaxKind.ModuleDeclaration &&
1147+
!isFunctionLike(blockScopeContainer)) {
1148+
// We check first if the name is inside class declaration or class expression; if so give explicit message
1149+
// otherwise report generic error message.
1150+
const errorSpan = getErrorSpanForNode(file, node);
1151+
file.bindDiagnostics.push(createFileDiagnostic(file, errorSpan.start, errorSpan.length,
1152+
getStrictModeBlockScopeFunctionDeclarationMessage(node)));
1153+
}
1154+
}
1155+
}
1156+
11281157
function checkStrictModeNumericLiteral(node: LiteralExpression) {
11291158
if (inStrictMode && node.isOctalLiteral) {
11301159
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode));
@@ -1641,6 +1670,7 @@ namespace ts {
16411670

16421671
checkStrictModeFunctionName(<FunctionDeclaration>node);
16431672
if (inStrictMode) {
1673+
checkStrictModeFunctionDeclaration(node);
16441674
bindBlockScopedDeclaration(node, SymbolFlags.Function, SymbolFlags.FunctionExcludes);
16451675
}
16461676
else {

src/compiler/diagnosticMessages.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,18 @@
811811
"category": "Error",
812812
"code": 1249
813813
},
814+
"In ES5 or lower, function declarations are not allowed in block scope in strict mode.": {
815+
"category": "Error",
816+
"code": 1250
817+
},
818+
"In ES5 or lower, function declarations are not allowed in block scope. Class definitions are automatically in strict mode.": {
819+
"category": "Error",
820+
"code": 1251
821+
},
822+
"In ES5 or lower, function declarations are not allowed in block scope. Modules are automatically in strict mode.": {
823+
"category": "Error",
824+
"code": 1252
825+
},
814826
"'with' statements are not allowed in an async function block.": {
815827
"category": "Error",
816828
"code": 1300

tests/baselines/reference/blockScopedFunctionDeclarationStrictES5.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
tests/cases/compiler/blockScopedFunctionDeclarationStrictES5.ts(3,14): error TS1250: In ES5 or lower, function declarations are not allowed in block scope in strict mode.
12
tests/cases/compiler/blockScopedFunctionDeclarationStrictES5.ts(6,1): error TS2304: Cannot find name 'foo'.
23

34

4-
==== tests/cases/compiler/blockScopedFunctionDeclarationStrictES5.ts (1 errors) ====
5+
==== tests/cases/compiler/blockScopedFunctionDeclarationStrictES5.ts (2 errors) ====
56
"use strict";
67
if (true) {
78
function foo() { } // Error to declare function in block scope
9+
~~~
10+
!!! error TS1250: In ES5 or lower, function declarations are not allowed in block scope in strict mode.
811
foo(); // This call should be ok
912
}
1013
foo(); // Error to find name foo

0 commit comments

Comments
 (0)