Skip to content

Commit 1bc4ab0

Browse files
authored
Merge pull request #12177 from Microsoft/error-on-non-identifier-rest-in-destructuring-assignment
Error on non identifier rest in destructuring assignment
2 parents ea309fe + de9f59a commit 1bc4ab0

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13941,7 +13941,12 @@ namespace ts {
1394113941
error(name, Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), declarationNameToString(name));
1394213942
}
1394313943
}
13944-
else if (property.kind !== SyntaxKind.SpreadAssignment) {
13944+
else if (property.kind === SyntaxKind.SpreadAssignment) {
13945+
if (property.expression.kind !== SyntaxKind.Identifier) {
13946+
error(property.expression, Diagnostics.An_object_rest_element_must_be_an_identifier);
13947+
}
13948+
}
13949+
else {
1394513950
error(property, Diagnostics.Property_assignment_expected);
1394613951
}
1394713952
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,10 @@
19911991
"category": "Error",
19921992
"code": 2700
19931993
},
1994+
"An object rest element must be an identifier.": {
1995+
"category": "Error",
1996+
"code": 2701
1997+
},
19941998

19951999
"Import declaration '{0}' is using private name '{1}'.": {
19962000
"category": "Error",

src/compiler/transformers/destructuring.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ namespace ts {
328328
bindingElements.push(p);
329329
}
330330
}
331-
else if (i === properties.length - 1 && p.kind === SyntaxKind.SpreadAssignment) {
332-
Debug.assert((p as SpreadAssignment).expression.kind === SyntaxKind.Identifier);
331+
else if (i === properties.length - 1 &&
332+
p.kind === SyntaxKind.SpreadAssignment &&
333+
p.expression.kind === SyntaxKind.Identifier) {
333334
if (bindingElements.length) {
334335
emitRestAssignment(bindingElements, value, location, target);
335336
bindingElements = [];

tests/baselines/reference/objectRestNegative.errors.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
tests/cases/conformance/types/rest/objectRestNegative.ts(2,10): error TS2462: A rest element must be last in a destructuring pattern
22
tests/cases/conformance/types/rest/objectRestNegative.ts(3,31): error TS2462: A rest element must be last in a destructuring pattern
33
tests/cases/conformance/types/rest/objectRestNegative.ts(6,17): error TS2700: Rest types may only be created from object types.
4+
tests/cases/conformance/types/rest/objectRestNegative.ts(11,9): error TS2701: An object rest element must be an identifier.
45

56

6-
==== tests/cases/conformance/types/rest/objectRestNegative.ts (3 errors) ====
7+
==== tests/cases/conformance/types/rest/objectRestNegative.ts (4 errors) ====
78
let o = { a: 1, b: 'no' };
89
var { ...mustBeLast, a } = o;
910
~~~~~~~~~~
@@ -18,4 +19,9 @@ tests/cases/conformance/types/rest/objectRestNegative.ts(6,17): error TS2700: Re
1819
!!! error TS2700: Rest types may only be created from object types.
1920
return rest;
2021
}
22+
23+
let rest: { b: string }
24+
({a, ...rest.b + rest.b} = o);
25+
~~~~~~~~~~~~~~~
26+
!!! error TS2701: An object rest element must be an identifier.
2127

tests/baselines/reference/objectRestNegative.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ function generic<T extends { x, y }>(t: T) {
77
let { x, ...rest } = t;
88
return rest;
99
}
10+
11+
let rest: { b: string }
12+
({a, ...rest.b + rest.b} = o);
1013

1114

1215
//// [objectRestNegative.js]
@@ -25,3 +28,5 @@ function generic(t) {
2528
var x = t.x, rest = __rest(t, ["x"]);
2629
return rest;
2730
}
31+
var rest;
32+
(a = o.a, o, o);

tests/cases/conformance/types/rest/objectRestNegative.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ function generic<T extends { x, y }>(t: T) {
66
let { x, ...rest } = t;
77
return rest;
88
}
9+
10+
let rest: { b: string }
11+
({a, ...rest.b + rest.b} = o);

0 commit comments

Comments
 (0)