Skip to content

Commit

Permalink
Disallow type annotation on a for-of variable
Browse files Browse the repository at this point in the history
  • Loading branch information
JsonFreeman committed Feb 18, 2015
1 parent 147cc20 commit 7cb2a64
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8467,9 +8467,6 @@ module ts {
if (variableDeclarationList.declarations.length >= 1) {
var decl = variableDeclarationList.declarations[0];
checkVariableDeclaration(decl);
if (decl.type) {
error(decl, Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation);
}
}
}
else {
Expand Down Expand Up @@ -10769,11 +10766,18 @@ module ts {
Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement;
return grammarErrorOnFirstToken(variableList.declarations[1], diagnostic);
}
if (variableList.declarations[0].initializer) {
var firstDeclaration = variableList.declarations[0];
if (firstDeclaration.initializer) {
var diagnostic = forInOrOfStatement.kind === SyntaxKind.ForInStatement ?
Diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer :
Diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer;
return grammarErrorOnNode(variableList.declarations[0].name, diagnostic);
return grammarErrorOnNode(firstDeclaration.name, diagnostic);
}
if (firstDeclaration.type) {
var diagnostic = forInOrOfStatement.kind === SyntaxKind.ForInStatement ?
Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation :
Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation;
return grammarErrorOnNode(firstDeclaration, diagnostic);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ module ts {
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." },
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}'." },
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" },
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." },
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
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}'." },
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}'." },
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,10 @@
"category": "Error",
"code": 2482
},
"The left-hand side of a 'for...of' statement cannot use a type annotation.": {
"category": "Error",
"code": 2483
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/parserES5ForOfStatement5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/conformance/parser/ecmascript5/Statements/parserES5ForOfStatement5.ts(1,1): error TS2482: For-of statements are only available when targeting ECMAScript 6 or higher
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.


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

This comment has been minimized.

Copy link
@yuit

yuit Feb 18, 2015

Contributor

Same here. should we still keep previous error msg?

}
5 changes: 1 addition & 4 deletions tests/baselines/reference/parserForInStatement7.errors.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
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.
tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement7.ts(1,25): error TS1091: Only a single variable declaration is allowed in a 'for...in' statement.
tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement7.ts(1,43): error TS2304: Cannot find name 'X'.


==== tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement7.ts (3 errors) ====
==== tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement7.ts (2 errors) ====
for (var a: number = 1, b: string = "" in X) {
~
!!! error TS2404: The left-hand side of a 'for...in' statement cannot use a type annotation.
~
!!! error TS1091: Only a single variable declaration is allowed in a 'for...in' statement.
~
Expand Down
8 changes: 8 additions & 0 deletions tests/baselines/reference/parserForOfStatement5.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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.


==== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement5.ts (1 errors) ====
for (var a: number of X) {
~
!!! error TS2483: The left-hand side of a 'for...of' statement cannot use a type annotation.
}

2 comments on commit 7cb2a64

@jonguan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For our benefit, would you mind expounding on why type annotation in a for...of statement should be disallowed?
I personally find the type annotation quite helpful.

Thanks!

@mhegazy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonguan please see #3500 for related discussion.

Please sign in to comment.