Skip to content

Commit e19ebc6

Browse files
Disallow binding patterns in parameter properties.
1 parent 61e2eb6 commit e19ebc6

8 files changed

Lines changed: 36 additions & 7 deletions

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10172,6 +10172,9 @@ module ts {
1017210172
else if (node.kind === SyntaxKind.InterfaceDeclaration && flags & NodeFlags.Ambient) {
1017310173
return grammarErrorOnNode(lastDeclare, Diagnostics.A_declare_modifier_cannot_be_used_with_an_interface_declaration, "declare");
1017410174
}
10175+
else if (node.kind === SyntaxKind.Parameter && (flags & NodeFlags.AccessibilityModifier) && isBindingPattern((<ParameterDeclaration>node).name)) {
10176+
return grammarErrorOnNode(node, Diagnostics.A_parameter_property_may_not_be_a_binding_pattern);
10177+
}
1017510178
}
1017610179

1017710180
function checkGrammarForDisallowedTrailingComma(list: NodeArray<Node>): boolean {

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ module ts {
146146
Modifiers_cannot_appear_here: { code: 1184, category: DiagnosticCategory.Error, key: "Modifiers cannot appear here." },
147147
Merge_conflict_marker_encountered: { code: 1185, category: DiagnosticCategory.Error, key: "Merge conflict marker encountered." },
148148
A_rest_element_cannot_have_an_initializer: { code: 1186, category: DiagnosticCategory.Error, key: "A rest element cannot have an initializer." },
149+
A_parameter_property_may_not_be_a_binding_pattern: { code: 1187, category: DiagnosticCategory.Error, key: "A parameter property may not be a binding pattern." },
149150
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
150151
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." },
151152
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@
224224
"A 'declare' modifier cannot be used with an import declaration.": {
225225
"category": "Error",
226226
"code": 1079,
227-
"isEarly": true
227+
"isEarly": true
228228
},
229229
"Invalid 'reference' directive syntax.": {
230230
"category": "Error",
@@ -659,7 +659,7 @@
659659
"An implementation cannot be declared in ambient contexts.": {
660660
"category": "Error",
661661
"code": 1184,
662-
"isEarly": true
662+
"isEarly": true
663663
},
664664
"Modifiers cannot appear here.": {
665665
"category": "Error",
@@ -673,6 +673,10 @@
673673
"category": "Error",
674674
"code": 1186
675675
},
676+
"A parameter property may not be a binding pattern.": {
677+
"category": "Error",
678+
"code": 1187
679+
},
676680

677681
"Duplicate identifier '{0}'.": {
678682
"category": "Error",

tests/baselines/reference/destructuringParameterProperties1.errors.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(2,17): error TS1187: A parameter property may not be a binding pattern.
2+
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(9,17): error TS1187: A parameter property may not be a binding pattern.
3+
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(16,17): error TS1187: A parameter property may not be a binding pattern.
14
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,26): error TS2339: Property 'x' does not exist on type 'C1'.
25
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,35): error TS2339: Property 'y' does not exist on type 'C1'.
36
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(22,43): error TS2339: Property 'y' does not exist on type 'C1'.
@@ -10,23 +13,29 @@ tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(2
1013
tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts(29,42): error TS2339: Property 'z' does not exist on type 'C3'.
1114

1215

13-
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts (10 errors) ====
16+
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties1.ts (13 errors) ====
1417
class C1 {
1518
constructor(public [x, y, z]: string[]) {
19+
~~~~~~~~~~~~~~~~~~~~~~~~~~
20+
!!! error TS1187: A parameter property may not be a binding pattern.
1621
}
1722
}
1823

1924
type TupleType1 = [string, number, boolean];
2025

2126
class C2 {
2227
constructor(public [x, y, z]: TupleType1) {
28+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29+
!!! error TS1187: A parameter property may not be a binding pattern.
2330
}
2431
}
2532

2633
type ObjType1 = { x: number; y: string; z: boolean }
2734

2835
class C3 {
2936
constructor(public { x, y, z }: ObjType1) {
37+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38+
!!! error TS1187: A parameter property may not be a binding pattern.
3039
}
3140
}
3241

tests/baselines/reference/destructuringParameterProperties2.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(2,36): error TS1187: A parameter property may not be a binding pattern.
12
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(3,59): error TS2339: Property 'b' does not exist on type 'C1'.
23
tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts(3,83): error TS2339: Property 'c' does not exist on type 'C1'.
34
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
78
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]'.
89

910

10-
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts (7 errors) ====
11+
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties2.ts (8 errors) ====
1112
class C1 {
1213
constructor(private k: number, private [a, b, c]: [number, string, boolean]) {
14+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15+
!!! error TS1187: A parameter property may not be a binding pattern.
1316
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) {
1417
~
1518
!!! error TS2339: Property 'b' does not exist on type 'C1'.

tests/baselines/reference/destructuringParameterProperties3.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(2,31): error TS1187: A parameter property may not be a binding pattern.
12
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(3,59): error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'.
23
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(3,83): error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'.
34
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
67
tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts(17,21): error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'.
78

89

9-
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts (6 errors) ====
10+
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties3.ts (7 errors) ====
1011
class C1<T, U, V> {
1112
constructor(private k: T, private [a, b, c]: [T,U,V]) {
13+
~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
!!! error TS1187: A parameter property may not be a binding pattern.
1215
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) {
1316
~
1417
!!! error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'.

tests/baselines/reference/destructuringParameterProperties4.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(3,31): error TS1187: A parameter property may not be a binding pattern.
12
tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(4,59): error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'.
23
tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(4,83): error TS2339: Property 'c' does not exist on type 'C1<T, U, V>'.
34
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
910
tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts(24,44): error TS2339: Property 'c' does not exist on type 'C2'.
1011

1112

12-
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts (9 errors) ====
13+
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties4.ts (10 errors) ====
1314

1415
class C1<T, U, V> {
1516
constructor(private k: T, protected [a, b, c]: [T,U,V]) {
17+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18+
!!! error TS1187: A parameter property may not be a binding pattern.
1619
if ((b === undefined && c === undefined) || (this.b === undefined && this.c === undefined)) {
1720
~
1821
!!! error TS2339: Property 'b' does not exist on type 'C1<T, U, V>'.

tests/baselines/reference/destructuringParameterProperties5.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts(5,17): error TS1187: A parameter property may not be a binding pattern.
12
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.
23
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.
34
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
1213
Property 'x' is missing in type '{ x1: number; x2: string; x3: boolean; }'.
1314

1415

15-
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts (9 errors) ====
16+
==== tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts (10 errors) ====
1617
type ObjType1 = { x: number; y: string; z: boolean }
1718
type TupleType1 = [ObjType1, number, string]
1819

1920
class C1 {
2021
constructor(public [{ x1, x2, x3 }, y, z]: TupleType1) {
22+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23+
!!! error TS1187: A parameter property may not be a binding pattern.
2124
~~
2225
!!! error TS2459: Type '{ x: number; y: string; z: boolean; }' has no property 'x1' and no string index signature.
2326
~~

0 commit comments

Comments
 (0)