Skip to content

Commit d4055a3

Browse files
authored
Merge pull request #26017 from ajafff/rest-param-destructuring
allow BindingPattern in FunctionRestParameter
2 parents e5708e1 + e68f495 commit d4055a3

27 files changed

+395
-125
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30004,10 +30004,6 @@ namespace ts {
3000430004
checkGrammarForDisallowedTrailingComma(parameters, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
3000530005
}
3000630006

30007-
if (isBindingPattern(parameter.name)) {
30008-
return grammarErrorOnNode(parameter.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);
30009-
}
30010-
3001130007
if (parameter.questionToken) {
3001230008
return grammarErrorOnNode(parameter.questionToken, Diagnostics.A_rest_parameter_cannot_be_optional);
3001330009
}

src/compiler/transformers/es2015.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,8 +1350,8 @@ namespace ts {
13501350
* part of a constructor declaration with a
13511351
* synthesized call to `super`
13521352
*/
1353-
function shouldAddRestParameter(node: ParameterDeclaration | undefined, inConstructorWithSynthesizedSuper: boolean) {
1354-
return node && node.dotDotDotToken && node.name.kind === SyntaxKind.Identifier && !inConstructorWithSynthesizedSuper;
1353+
function shouldAddRestParameter(node: ParameterDeclaration | undefined, inConstructorWithSynthesizedSuper: boolean): node is ParameterDeclaration {
1354+
return !!(node && node.dotDotDotToken && !inConstructorWithSynthesizedSuper);
13551355
}
13561356

13571357
/**
@@ -1370,11 +1370,11 @@ namespace ts {
13701370
}
13711371

13721372
// `declarationName` is the name of the local declaration for the parameter.
1373-
const declarationName = getMutableClone(<Identifier>parameter!.name);
1373+
const declarationName = parameter.name.kind === SyntaxKind.Identifier ? getMutableClone(parameter.name) : createTempVariable(/*recordTempVariable*/ undefined);
13741374
setEmitFlags(declarationName, EmitFlags.NoSourceMap);
13751375

13761376
// `expressionName` is the name of the parameter used in expressions.
1377-
const expressionName = getSynthesizedClone(<Identifier>parameter!.name);
1377+
const expressionName = parameter.name.kind === SyntaxKind.Identifier ? getSynthesizedClone(parameter.name) : declarationName;
13781378
const restIndex = node.parameters.length - 1;
13791379
const temp = createLoopVariable();
13801380

@@ -1439,6 +1439,24 @@ namespace ts {
14391439
setEmitFlags(forStatement, EmitFlags.CustomPrologue);
14401440
startOnNewLine(forStatement);
14411441
statements.push(forStatement);
1442+
1443+
if (parameter.name.kind !== SyntaxKind.Identifier) {
1444+
// do the actual destructuring of the rest parameter if necessary
1445+
statements.push(
1446+
setEmitFlags(
1447+
setTextRange(
1448+
createVariableStatement(
1449+
/*modifiers*/ undefined,
1450+
createVariableDeclarationList(
1451+
flattenDestructuringBinding(parameter, visitor, context, FlattenLevel.All, expressionName),
1452+
)
1453+
),
1454+
parameter
1455+
),
1456+
EmitFlags.CustomPrologue
1457+
)
1458+
);
1459+
}
14421460
}
14431461

14441462
/**

tests/baselines/reference/iterableArrayPattern14.errors.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/baselines/reference/iterableArrayPattern15.errors.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/baselines/reference/iterableArrayPattern16.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
tests/cases/conformance/es6/destructuring/iterableArrayPattern16.ts(1,17): error TS2501: A rest element cannot contain a binding pattern.
21
tests/cases/conformance/es6/destructuring/iterableArrayPattern16.ts(2,5): error TS2345: Argument of type 'FooIterator' is not assignable to parameter of type '[Bar, Bar]'.
32
Type 'FooIterator' is missing the following properties from type '[Bar, Bar]': 0, 1, length, pop, and 26 more.
43
tests/cases/conformance/es6/destructuring/iterableArrayPattern16.ts(2,12): error TS2449: Class 'FooIteratorIterator' used before its declaration.
54

65

7-
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern16.ts (3 errors) ====
6+
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern16.ts (2 errors) ====
87
function fun(...[a, b]: [Bar, Bar][]) { }
9-
~~~~~~
10-
!!! error TS2501: A rest element cannot contain a binding pattern.
118
fun(...new FooIteratorIterator);
129
~~~~~~~~~~~~~~~~~~~~~~~~~~
1310
!!! error TS2345: Argument of type 'FooIterator' is not assignable to parameter of type '[Bar, Bar]'.

tests/baselines/reference/iterableArrayPattern17.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
tests/cases/conformance/es6/destructuring/iterableArrayPattern17.ts(16,17): error TS2501: A rest element cannot contain a binding pattern.
21
tests/cases/conformance/es6/destructuring/iterableArrayPattern17.ts(17,5): error TS2345: Argument of type 'FooIterator' is not assignable to parameter of type 'Bar'.
32
Property 'x' is missing in type 'FooIterator' but required in type 'Bar'.
43

54

6-
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern17.ts (2 errors) ====
5+
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern17.ts (1 errors) ====
76
class Bar { x }
87
class Foo extends Bar { y }
98
class FooIterator {
@@ -20,8 +19,6 @@ tests/cases/conformance/es6/destructuring/iterableArrayPattern17.ts(17,5): error
2019
}
2120

2221
function fun(...[a, b]: Bar[]) { }
23-
~~~~~~
24-
!!! error TS2501: A rest element cannot contain a binding pattern.
2522
fun(new FooIterator);
2623
~~~~~~~~~~~~~~~
2724
!!! error TS2345: Argument of type 'FooIterator' is not assignable to parameter of type 'Bar'.

tests/baselines/reference/iterableArrayPattern20.errors.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts(1,33): error TS2501: A rest element cannot contain a binding pattern.
21
tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts(2,1): error TS2554: Expected 2 arguments, but got 1.
32

43

5-
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts (2 errors) ====
4+
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts (1 errors) ====
65
function takeFirstTwoEntries(...[[k1, v1], [k2, v2]]) { }
7-
~~~~~~~~~~~~~~~~~~~~
8-
!!! error TS2501: A rest element cannot contain a binding pattern.
96
takeFirstTwoEntries(new Map([["", 0], ["hello", 1]]));
107
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118
!!! error TS2554: Expected 2 arguments, but got 1.

tests/baselines/reference/iterableArrayPattern26.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
tests/cases/conformance/es6/destructuring/iterableArrayPattern26.ts(1,33): error TS2501: A rest element cannot contain a binding pattern.
21
tests/cases/conformance/es6/destructuring/iterableArrayPattern26.ts(2,21): error TS2345: Argument of type 'Map<string, number>' is not assignable to parameter of type '[string, number]'.
32
Type 'Map<string, number>' is missing the following properties from type '[string, number]': 0, 1, length, pop, and 22 more.
43

54

6-
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern26.ts (2 errors) ====
5+
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern26.ts (1 errors) ====
76
function takeFirstTwoEntries(...[[k1, v1], [k2, v2]]: [string, number][]) { }
8-
~~~~~~~~~~~~~~~~~~~~
9-
!!! error TS2501: A rest element cannot contain a binding pattern.
107
takeFirstTwoEntries(new Map([["", 0], ["hello", 1]]));
118
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129
!!! error TS2345: Argument of type 'Map<string, number>' is not assignable to parameter of type '[string, number]'.

tests/baselines/reference/iterableArrayPattern27.errors.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)