From 7cb2a643503698d3da5469b01d1a8fcd67d2125f Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Tue, 17 Feb 2015 18:16:00 -0800 Subject: [PATCH] Disallow type annotation on a for-of variable --- src/compiler/checker.ts | 14 +++++++++----- src/compiler/diagnosticInformationMap.generated.ts | 1 + src/compiler/diagnosticMessages.json | 4 ++++ .../reference/parserES5ForOfStatement5.errors.txt | 6 +++--- .../reference/parserForInStatement7.errors.txt | 5 +---- .../reference/parserForOfStatement5.errors.txt | 8 ++++++++ 6 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 tests/baselines/reference/parserForOfStatement5.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 96439b2cf68..aeb2324fcbf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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 { @@ -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); } } } diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 17c0bec44a3..4d9ac1d91a7 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -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}'." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index b0bf5945e8c..65c73d3ccd4 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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", diff --git a/tests/baselines/reference/parserES5ForOfStatement5.errors.txt b/tests/baselines/reference/parserES5ForOfStatement5.errors.txt index 55c56d60aae..0e88ce6cf36 100644 --- a/tests/baselines/reference/parserES5ForOfStatement5.errors.txt +++ b/tests/baselines/reference/parserES5ForOfStatement5.errors.txt @@ -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. } \ No newline at end of file diff --git a/tests/baselines/reference/parserForInStatement7.errors.txt b/tests/baselines/reference/parserForInStatement7.errors.txt index 397401003db..7c85ce3f795 100644 --- a/tests/baselines/reference/parserForInStatement7.errors.txt +++ b/tests/baselines/reference/parserForInStatement7.errors.txt @@ -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. ~ diff --git a/tests/baselines/reference/parserForOfStatement5.errors.txt b/tests/baselines/reference/parserForOfStatement5.errors.txt new file mode 100644 index 00000000000..13fd8739866 --- /dev/null +++ b/tests/baselines/reference/parserForOfStatement5.errors.txt @@ -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. + } \ No newline at end of file