Disallow binding patterns in parameter properties.

This commit is contained in:
Daniel Rosenwasser 2015-01-14 15:30:17 -08:00
parent 61e2eb6b89
commit e19ebc6d3e
8 changed files with 36 additions and 7 deletions

View File

@ -10172,6 +10172,9 @@ module ts {
else if (node.kind === SyntaxKind.InterfaceDeclaration && flags & NodeFlags.Ambient) {
return grammarErrorOnNode(lastDeclare, Diagnostics.A_declare_modifier_cannot_be_used_with_an_interface_declaration, "declare");
}
else if (node.kind === SyntaxKind.Parameter && (flags & NodeFlags.AccessibilityModifier) && isBindingPattern((<ParameterDeclaration>node).name)) {
return grammarErrorOnNode(node, Diagnostics.A_parameter_property_may_not_be_a_binding_pattern);
}
}
function checkGrammarForDisallowedTrailingComma(list: NodeArray<Node>): boolean {

View File

@ -146,6 +146,7 @@ module ts {
Modifiers_cannot_appear_here: { code: 1184, category: DiagnosticCategory.Error, key: "Modifiers cannot appear here." },
Merge_conflict_marker_encountered: { code: 1185, category: DiagnosticCategory.Error, key: "Merge conflict marker encountered." },
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." },
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." },

View File

@ -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",
@ -673,6 +673,10 @@
"category": "Error",
"code": 1186
},
"A parameter property may not be a binding pattern.": {
"category": "Error",
"code": 1187
},
"Duplicate identifier '{0}'.": {
"category": "Error",

View File

@ -1,3 +1,6 @@
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(2,17): error TS1187: A parameter property may not be a binding pattern.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(9,17): error TS1187: A parameter property may not be a binding pattern.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(16,17): error TS1187: A parameter property may not be a binding pattern.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,26): error TS2339: Property 'x' does not exist on type 'C1'.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,35): error TS2339: Property 'y' does not exist on type 'C1'.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,43): error TS2339: Property 'y' does not exist on type 'C1'.
@ -10,9 +13,11 @@ tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(2
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(29,42): error TS2339: Property 'z' does not exist on type 'C3'.
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts (10 errors) ====
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts (13 errors) ====
class C1 {
constructor(public [x, y, z]: string[]) {
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1187: A parameter property may not be a binding pattern.
}
}
@ -20,6 +25,8 @@ tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(2
class C2 {
constructor(public [x, y, z]: TupleType1) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1187: A parameter property may not be a binding pattern.
}
}
@ -27,6 +34,8 @@ tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(2
class C3 {
constructor(public { x, y, z }: ObjType1) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1187: A parameter property may not be a binding pattern.
}
}

View File

@ -1,3 +1,4 @@
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(2,36): error TS1187: A parameter property may not be a binding pattern.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(3,59): error TS2339: Property 'b' does not exist on type 'C1'.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(3,83): error TS2339: Property 'c' does not exist on type 'C1'.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(4,18): error TS2339: Property 'a' does not exist on type 'C1'.
@ -7,9 +8,11 @@ tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(1
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(21,27): error TS2345: Argument of type '[number, undefined, string]' is not assignable to parameter of type '[number, string, boolean]'.
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts (7 errors) ====
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts (8 errors) ====
class C1 {
constructor(private k: number, private [a, b, c]: [number, string, boolean]) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1187: A parameter property may not be a binding pattern.
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) {
~
!!! error TS2339: Property 'b' does not exist on type 'C1'.

View File

@ -1,3 +1,4 @@
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(2,31): error TS1187: A parameter property may not be a binding pattern.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(3,59): error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(3,83): error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(4,18): error TS2339: Property 'a' does not exist on type 'C1<T, U, V>'.
@ -6,9 +7,11 @@ tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(1
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(17,21): error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'.
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts (6 errors) ====
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts (7 errors) ====
class C1<T, U, V> {
constructor(private k: T, private [a, b, c]: [T,U,V]) {
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1187: A parameter property may not be a binding pattern.
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) {
~
!!! error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'.

View File

@ -1,3 +1,4 @@
tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(3,31): error TS1187: A parameter property may not be a binding pattern.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(4,59): error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(4,83): error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(5,18): error TS2339: Property 'a' does not exist on type 'C1<T, U, V>'.
@ -9,10 +10,12 @@ tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(2
tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(24,44): error TS2339: Property 'c' does not exist on type 'C2'.
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts (9 errors) ====
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts (10 errors) ====
class C1<T, U, V> {
constructor(private k: T, protected [a, b, c]: [T,U,V]) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1187: A parameter property may not be a binding pattern.
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) {
~
!!! error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'.

View File

@ -1,3 +1,4 @@
tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts(5,17): error TS1187: A parameter property may not be a binding pattern.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts(5,27): error TS2459: Type '{ x: number; y: string; z: boolean; }' has no property 'x1' and no string index signature.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts(5,31): error TS2459: Type '{ x: number; y: string; z: boolean; }' has no property 'x2' and no string index signature.
tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts(5,35): error TS2459: Type '{ x: number; y: string; z: boolean; }' has no property 'x3' and no string index signature.
@ -12,12 +13,14 @@ tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts(1
Property 'x' is missing in type '{ x1: number; x2: string; x3: boolean; }'.
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts (9 errors) ====
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts (10 errors) ====
type ObjType1 = { x: number; y: string; z: boolean }
type TupleType1 = [ObjType1, number, string]
class C1 {
constructor(public [{ x1, x2, x3 }, y, z]: TupleType1) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1187: A parameter property may not be a binding pattern.
~~
!!! error TS2459: Type '{ x: number; y: string; z: boolean; }' has no property 'x1' and no string index signature.
~~