From 75a1a8a493675f4e0744ebddb52d0e50fd185001 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 14 Jan 2015 17:25:37 -0800 Subject: [PATCH] Disallow optional destructured parameters in implementation signatures. --- src/compiler/checker.ts | 3 +++ src/compiler/diagnosticInformationMap.generated.ts | 1 + src/compiler/diagnosticMessages.json | 8 ++++++-- .../reference/optionalBindingParameters1.errors.txt | 5 ++++- .../reference/optionalBindingParameters2.errors.txt | 5 ++++- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8e23fb2050d..781718f7299 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7212,6 +7212,9 @@ module ts { error(node, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation); } } + if (node.questionToken && isBindingPattern(node.name) && func.body) { + error(node, Diagnostics.A_binding_pattern_parameter_may_not_be_optional_in_an_implementation_signature); + } if (node.dotDotDotToken) { if (!isArrayType(getTypeOfSymbol(node.symbol))) { error(node, Diagnostics.A_rest_parameter_must_be_of_an_array_type); diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 2a342fa17d2..5f404d51c23 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -297,6 +297,7 @@ module ts { Type_0_has_no_property_1: { code: 2460, category: DiagnosticCategory.Error, key: "Type '{0}' has no property '{1}'." }, Type_0_is_not_an_array_type: { code: 2461, category: DiagnosticCategory.Error, key: "Type '{0}' is not an array type." }, A_rest_element_must_be_last_in_an_array_destructuring_pattern: { code: 2462, category: DiagnosticCategory.Error, key: "A rest element must be last in an array destructuring pattern" }, + A_binding_pattern_parameter_may_not_be_optional_in_an_implementation_signature: { code: 2463, category: DiagnosticCategory.Error, key: "A binding pattern parameter may not be optional in an implementation signature." }, 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 efb2bfe7e94..aa0af6ff743 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -224,7 +224,7 @@ "A 'declare' modifier cannot be used with an import declaration.": { "category": "Error", "code": 1079, - "isEarly": true + "isEarly": true }, "Invalid 'reference' directive syntax.": { "category": "Error", @@ -659,7 +659,7 @@ "An implementation cannot be declared in ambient contexts.": { "category": "Error", "code": 1184, - "isEarly": true + "isEarly": true }, "Modifiers cannot appear here.": { "category": "Error", @@ -1282,6 +1282,10 @@ "category": "Error", "code": 2462 }, + "A binding pattern parameter may not be optional in an implementation signature.": { + "category": "Error", + "code": 2463 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/optionalBindingParameters1.errors.txt b/tests/baselines/reference/optionalBindingParameters1.errors.txt index dbc0f9a44e9..51cc9d7817f 100644 --- a/tests/baselines/reference/optionalBindingParameters1.errors.txt +++ b/tests/baselines/reference/optionalBindingParameters1.errors.txt @@ -1,9 +1,12 @@ +tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts(2,14): error TS2463: A binding pattern parameter may not be optional in an implementation signature. tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts(8,5): error TS2345: Argument of type '[boolean, number, string]' is not assignable to parameter of type '[string, number, boolean]'. -==== tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts (1 errors) ==== +==== tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts (2 errors) ==== function foo([x,y,z]?: [string, number, boolean]) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter may not be optional in an implementation signature. } diff --git a/tests/baselines/reference/optionalBindingParameters2.errors.txt b/tests/baselines/reference/optionalBindingParameters2.errors.txt index b906bc6a231..56a0a339cc7 100644 --- a/tests/baselines/reference/optionalBindingParameters2.errors.txt +++ b/tests/baselines/reference/optionalBindingParameters2.errors.txt @@ -1,11 +1,14 @@ +tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts(2,14): error TS2463: A binding pattern parameter may not be optional in an implementation signature. tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts(8,5): error TS2345: Argument of type '{ x: boolean; y: number; z: string; }' is not assignable to parameter of type '{ x: string; y: number; z: boolean; }'. Types of property 'x' are incompatible. Type 'boolean' is not assignable to type 'string'. -==== tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts (1 errors) ==== +==== tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts (2 errors) ==== function foo({ x, y, z }?: { x: string; y: number; z: boolean }) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter may not be optional in an implementation signature. }