Skip to content

Commit 7cb2a64

Browse files
committed
Disallow type annotation on a for-of variable
1 parent 147cc20 commit 7cb2a64

6 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/compiler/checker.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8467,9 +8467,6 @@ module ts {
84678467
if (variableDeclarationList.declarations.length >= 1) {
84688468
var decl = variableDeclarationList.declarations[0];
84698469
checkVariableDeclaration(decl);
8470-
if (decl.type) {
8471-
error(decl, Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation);
8472-
}
84738470
}
84748471
}
84758472
else {
@@ -10769,11 +10766,18 @@ module ts {
1076910766
Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement;
1077010767
return grammarErrorOnFirstToken(variableList.declarations[1], diagnostic);
1077110768
}
10772-
if (variableList.declarations[0].initializer) {
10769+
var firstDeclaration = variableList.declarations[0];
10770+
if (firstDeclaration.initializer) {
1077310771
var diagnostic = forInOrOfStatement.kind === SyntaxKind.ForInStatement ?
1077410772
Diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer :
1077510773
Diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer;
10776-
return grammarErrorOnNode(variableList.declarations[0].name, diagnostic);
10774+
return grammarErrorOnNode(firstDeclaration.name, diagnostic);
10775+
}
10776+
if (firstDeclaration.type) {
10777+
var diagnostic = forInOrOfStatement.kind === SyntaxKind.ForInStatement ?
10778+
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation :
10779+
Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation;
10780+
return grammarErrorOnNode(firstDeclaration, diagnostic);
1077710781
}
1077810782
}
1077910783
}

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ module ts {
317317
let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations: { code: 2476, category: DiagnosticCategory.Error, key: "'let' is not allowed to be used as a name in 'let' or 'const' declarations." },
318318
Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1: { code: 2477, category: DiagnosticCategory.Error, key: "Cannot initialize outer scoped variable '{0}' in the same scope as block scoped declaration '{1}'." },
319319
For_of_statements_are_only_available_when_targeting_ECMAScript_6_or_higher: { code: 2482, category: DiagnosticCategory.Error, key: "For-of statements are only available when targeting ECMAScript 6 or higher" },
320+
The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation: { code: 2483, category: DiagnosticCategory.Error, key: "The left-hand side of a 'for...of' statement cannot use a type annotation." },
320321
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
321322
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
322323
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,10 @@
12601260
"category": "Error",
12611261
"code": 2482
12621262
},
1263+
"The left-hand side of a 'for...of' statement cannot use a type annotation.": {
1264+
"category": "Error",
1265+
"code": 2483
1266+
},
12631267

12641268
"Import declaration '{0}' is using private name '{1}'.": {
12651269
"category": "Error",
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/conformance/parser/ecmascript5/Statements/parserES5ForOfStatement5.ts(1,1): error TS2482: For-of statements are only available when targeting ECMAScript 6 or higher
1+
tests/cases/conformance/parser/ecmascript5/Statements/parserES5ForOfStatement5.ts(1,10): error TS2483: The left-hand side of a 'for...of' statement cannot use a type annotation.
22

33

44
==== tests/cases/conformance/parser/ecmascript5/Statements/parserES5ForOfStatement5.ts (1 errors) ====
55
for (var a: number of X) {
6-
~~~
7-
!!! error TS2482: For-of statements are only available when targeting ECMAScript 6 or higher
6+
~
7+
!!! error TS2483: The left-hand side of a 'for...of' statement cannot use a type annotation.
88
}

tests/baselines/reference/parserForInStatement7.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement7.ts(1,10): error TS2404: The left-hand side of a 'for...in' statement cannot use a type annotation.
21
tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement7.ts(1,25): error TS1091: Only a single variable declaration is allowed in a 'for...in' statement.
32
tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement7.ts(1,43): error TS2304: Cannot find name 'X'.
43

54

6-
==== tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement7.ts (3 errors) ====
5+
==== tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement7.ts (2 errors) ====
76
for (var a: number = 1, b: string = "" in X) {
8-
~
9-
!!! error TS2404: The left-hand side of a 'for...in' statement cannot use a type annotation.
107
~
118
!!! error TS1091: Only a single variable declaration is allowed in a 'for...in' statement.
129
~
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement5.ts(1,10): error TS2483: The left-hand side of a 'for...of' statement cannot use a type annotation.
2+
3+
4+
==== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement5.ts (1 errors) ====
5+
for (var a: number of X) {
6+
~
7+
!!! error TS2483: The left-hand side of a 'for...of' statement cannot use a type annotation.
8+
}

0 commit comments

Comments
 (0)