Skip to content

Commit

Permalink
Disallow initializers in for-in and for-of loops
Browse files Browse the repository at this point in the history
  • Loading branch information
JsonFreeman committed Feb 18, 2015
1 parent f7a6354 commit 147cc20
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10769,6 +10769,12 @@ 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 diagnostic = forInOrOfStatement.kind === SyntaxKind.ForInStatement ?

This comment has been minimized.

Copy link
@JsonFreeman

JsonFreeman Feb 18, 2015

Author Contributor

align

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);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ module ts {
A_rest_element_cannot_have_an_initializer: { code: 1186, category: DiagnosticCategory.Error, key: "A rest element cannot have an initializer." },
A_parameter_property_may_not_be_a_binding_pattern: { code: 1187, category: DiagnosticCategory.Error, key: "A parameter property may not be a binding pattern." },
Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement: { code: 1188, category: DiagnosticCategory.Error, key: "Only a single variable declaration is allowed in a 'for...of' statement." },
The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer: { code: 1189, category: DiagnosticCategory.Error, key: "The variable declaration of a 'for...in' statement cannot have an initializer." },
The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer: { code: 1190, category: DiagnosticCategory.Error, key: "The variable declaration of a 'for...of' statement cannot have an initializer." },
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,14 @@
"category": "Error",
"code": 1188
},
"The variable declaration of a 'for...in' statement cannot have an initializer.": {
"category": "Error",
"code": 1189
},
"The variable declaration of a 'for...of' statement cannot have an initializer.": {
"category": "Error",
"code": 1190
},

"Duplicate identifier '{0}'.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/cases/conformance/parser/ecmascript5/Statements/parserES5ForOfStatement20.ts(1,10): error TS1189: The variable declaration of a 'for...in' statement cannot have an initializer.


==== tests/cases/conformance/parser/ecmascript5/Statements/parserES5ForOfStatement20.ts (1 errors) ====
for (var of = 0 in of) { }
~~
!!! error TS1189: The variable declaration of a 'for...in' statement cannot have an initializer.
5 changes: 0 additions & 5 deletions tests/baselines/reference/parserES5ForOfStatement20.types

This file was deleted.

6 changes: 3 additions & 3 deletions tests/baselines/reference/parserES5ForOfStatement4.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/conformance/parser/ecmascript5/Statements/parserES5ForOfStatement4.ts(1,1): error TS2482: For-of statements are only available when targeting ECMAScript 6 or higher
tests/cases/conformance/parser/ecmascript5/Statements/parserES5ForOfStatement4.ts(1,10): error TS1190: The variable declaration of a 'for...of' statement cannot have an initializer.


==== tests/cases/conformance/parser/ecmascript5/Statements/parserES5ForOfStatement4.ts (1 errors) ====
for (var a = 1 of X) {
~~~
!!! error TS2482: For-of statements are only available when targeting ECMAScript 6 or higher
~

This comment has been minimized.

Copy link
@yuit

yuit Feb 18, 2015

Contributor

Shouldn't we keep the previous error since we are parsing ES5 ?

!!! error TS1190: The variable declaration of a 'for...of' statement cannot have an initializer.
}
5 changes: 4 additions & 1 deletion tests/baselines/reference/parserForInStatement4.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement4.ts(1,10): error TS1189: The variable declaration of a 'for...in' statement cannot have an initializer.
tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement4.ts(1,19): error TS2304: Cannot find name 'X'.


==== tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement4.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript5/Statements/parserForInStatement4.ts (2 errors) ====
for (var a = 1 in X) {
~
!!! error TS1189: The variable declaration of a 'for...in' statement cannot have an initializer.
~
!!! error TS2304: Cannot find name 'X'.
}
7 changes: 7 additions & 0 deletions tests/baselines/reference/parserForOfStatement20.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement20.ts(1,10): error TS1189: The variable declaration of a 'for...in' statement cannot have an initializer.


==== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement20.ts (1 errors) ====
for (var of = 0 in of) { }
~~
!!! error TS1189: The variable declaration of a 'for...in' statement cannot have an initializer.
5 changes: 0 additions & 5 deletions tests/baselines/reference/parserForOfStatement20.types

This file was deleted.

8 changes: 8 additions & 0 deletions tests/baselines/reference/parserForOfStatement4.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement4.ts(1,10): error TS1190: The variable declaration of a 'for...of' statement cannot have an initializer.


==== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement4.ts (1 errors) ====
for (var a = 1 of X) {
~
!!! error TS1190: The variable declaration of a 'for...of' statement cannot have an initializer.
}

0 comments on commit 147cc20

Please sign in to comment.