Skip to content

Commit

Permalink
Merge pull request #26017 from ajafff/rest-param-destructuring
Browse files Browse the repository at this point in the history
allow BindingPattern in FunctionRestParameter
  • Loading branch information
sheetalkamat authored Jan 14, 2019
2 parents e5708e1 + e68f495 commit d4055a3
Show file tree
Hide file tree
Showing 27 changed files with 395 additions and 125 deletions.
4 changes: 0 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30004,10 +30004,6 @@ namespace ts {
checkGrammarForDisallowedTrailingComma(parameters, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
}

if (isBindingPattern(parameter.name)) {
return grammarErrorOnNode(parameter.name, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);
}

if (parameter.questionToken) {
return grammarErrorOnNode(parameter.questionToken, Diagnostics.A_rest_parameter_cannot_be_optional);
}
Expand Down
26 changes: 22 additions & 4 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1350,8 +1350,8 @@ namespace ts {
* part of a constructor declaration with a
* synthesized call to `super`
*/
function shouldAddRestParameter(node: ParameterDeclaration | undefined, inConstructorWithSynthesizedSuper: boolean) {
return node && node.dotDotDotToken && node.name.kind === SyntaxKind.Identifier && !inConstructorWithSynthesizedSuper;
function shouldAddRestParameter(node: ParameterDeclaration | undefined, inConstructorWithSynthesizedSuper: boolean): node is ParameterDeclaration {
return !!(node && node.dotDotDotToken && !inConstructorWithSynthesizedSuper);
}

/**
Expand All @@ -1370,11 +1370,11 @@ namespace ts {
}

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

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

Expand Down Expand Up @@ -1439,6 +1439,24 @@ namespace ts {
setEmitFlags(forStatement, EmitFlags.CustomPrologue);
startOnNewLine(forStatement);
statements.push(forStatement);

if (parameter.name.kind !== SyntaxKind.Identifier) {
// do the actual destructuring of the rest parameter if necessary
statements.push(
setEmitFlags(
setTextRange(
createVariableStatement(
/*modifiers*/ undefined,
createVariableDeclarationList(
flattenDestructuringBinding(parameter, visitor, context, FlattenLevel.All, expressionName),
)
),
parameter
),
EmitFlags.CustomPrologue
)
);
}
}

/**
Expand Down
23 changes: 0 additions & 23 deletions tests/baselines/reference/iterableArrayPattern14.errors.txt

This file was deleted.

23 changes: 0 additions & 23 deletions tests/baselines/reference/iterableArrayPattern15.errors.txt

This file was deleted.

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


==== tests/cases/conformance/es6/destructuring/iterableArrayPattern16.ts (3 errors) ====
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern16.ts (2 errors) ====
function fun(...[a, b]: [Bar, Bar][]) { }
~~~~~~
!!! error TS2501: A rest element cannot contain a binding pattern.
fun(...new FooIteratorIterator);
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type 'FooIterator' is not assignable to parameter of type '[Bar, Bar]'.
Expand Down
5 changes: 1 addition & 4 deletions tests/baselines/reference/iterableArrayPattern17.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
tests/cases/conformance/es6/destructuring/iterableArrayPattern17.ts(16,17): error TS2501: A rest element cannot contain a binding pattern.
tests/cases/conformance/es6/destructuring/iterableArrayPattern17.ts(17,5): error TS2345: Argument of type 'FooIterator' is not assignable to parameter of type 'Bar'.
Property 'x' is missing in type 'FooIterator' but required in type 'Bar'.


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

function fun(...[a, b]: Bar[]) { }
~~~~~~
!!! error TS2501: A rest element cannot contain a binding pattern.
fun(new FooIterator);
~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type 'FooIterator' is not assignable to parameter of type 'Bar'.
Expand Down
23 changes: 0 additions & 23 deletions tests/baselines/reference/iterableArrayPattern20.errors.txt

This file was deleted.

5 changes: 1 addition & 4 deletions tests/baselines/reference/iterableArrayPattern25.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts(1,33): error TS2501: A rest element cannot contain a binding pattern.
tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts(2,1): error TS2554: Expected 2 arguments, but got 1.


==== tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts (2 errors) ====
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts (1 errors) ====
function takeFirstTwoEntries(...[[k1, v1], [k2, v2]]) { }
~~~~~~~~~~~~~~~~~~~~
!!! error TS2501: A rest element cannot contain a binding pattern.
takeFirstTwoEntries(new Map([["", 0], ["hello", 1]]));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 2 arguments, but got 1.
5 changes: 1 addition & 4 deletions tests/baselines/reference/iterableArrayPattern26.errors.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
tests/cases/conformance/es6/destructuring/iterableArrayPattern26.ts(1,33): error TS2501: A rest element cannot contain a binding pattern.
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]'.
Type 'Map<string, number>' is missing the following properties from type '[string, number]': 0, 1, length, pop, and 22 more.


==== tests/cases/conformance/es6/destructuring/iterableArrayPattern26.ts (2 errors) ====
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern26.ts (1 errors) ====
function takeFirstTwoEntries(...[[k1, v1], [k2, v2]]: [string, number][]) { }
~~~~~~~~~~~~~~~~~~~~
!!! error TS2501: A rest element cannot contain a binding pattern.
takeFirstTwoEntries(new Map([["", 0], ["hello", 1]]));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type 'Map<string, number>' is not assignable to parameter of type '[string, number]'.
Expand Down
8 changes: 0 additions & 8 deletions tests/baselines/reference/iterableArrayPattern27.errors.txt

This file was deleted.

5 changes: 1 addition & 4 deletions tests/baselines/reference/iterableArrayPattern28.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
tests/cases/conformance/es6/destructuring/iterableArrayPattern28.ts(1,33): error TS2501: A rest element cannot contain a binding pattern.
tests/cases/conformance/es6/destructuring/iterableArrayPattern28.ts(2,52): error TS2322: Type 'true' is not assignable to type 'number'.


==== tests/cases/conformance/es6/destructuring/iterableArrayPattern28.ts (2 errors) ====
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern28.ts (1 errors) ====
function takeFirstTwoEntries(...[[k1, v1], [k2, v2]]: [string, number][]) { }
~~~~~~~~~~~~~~~~~~~~
!!! error TS2501: A rest element cannot contain a binding pattern.
takeFirstTwoEntries(...new Map([["", 0], ["hello", true]]));
~~~~
!!! error TS2322: Type 'true' is not assignable to type 'number'.
5 changes: 1 addition & 4 deletions tests/baselines/reference/iterableArrayPattern29.errors.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
tests/cases/conformance/es6/destructuring/iterableArrayPattern29.ts(1,33): error TS2501: A rest element cannot contain a binding pattern.
tests/cases/conformance/es6/destructuring/iterableArrayPattern29.ts(2,21): error TS2345: Argument of type '[string, boolean]' is not assignable to parameter of type '[string, number]'.
Type 'boolean' is not assignable to type 'number'.


==== tests/cases/conformance/es6/destructuring/iterableArrayPattern29.ts (2 errors) ====
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern29.ts (1 errors) ====
function takeFirstTwoEntries(...[[k1, v1], [k2, v2]]: [string, number][]) { }
~~~~~~~~~~~~~~~~~~~~
!!! error TS2501: A rest element cannot contain a binding pattern.
takeFirstTwoEntries(...new Map([["", true], ["hello", true]]));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '[string, boolean]' is not assignable to parameter of type '[string, number]'.
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
===================================================================
JsFile: restParameterWithBindingPattern1.js
mapUrl: restParameterWithBindingPattern1.js.map
sourceRoot:
sources: restParameterWithBindingPattern1.ts
===================================================================
-------------------------------------------------------------------
emittedFile:tests/cases/compiler/restParameterWithBindingPattern1.js
sourceFile:restParameterWithBindingPattern1.ts
-------------------------------------------------------------------
>>>function a() {
1 >
2 >^^^^^^^^^
3 > ^
4 > ^^^^^^^->
1 >
2 >function
3 > a
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
2 >Emitted(1, 10) Source(1, 10) + SourceIndex(0)
3 >Emitted(1, 11) Source(1, 11) + SourceIndex(0)
---
>>> var _a = [];
1->^^^^
2 > ^^^^^^^^^^^^
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1->(
2 > ...{a, b}
1->Emitted(2, 5) Source(1, 12) + SourceIndex(0)
2 >Emitted(2, 17) Source(1, 21) + SourceIndex(0)
---
>>> for (var _i = 0; _i < arguments.length; _i++) {
1->^^^^^^^^^
2 > ^^^^^^^^^^
3 > ^^
4 > ^^^^^^^^^^^^^^^^^^^^^
5 > ^^
6 > ^^^^
1->
2 > ...{a, b}
3 >
4 > ...{a, b}
5 >
6 > ...{a, b}
1->Emitted(3, 10) Source(1, 12) + SourceIndex(0)
2 >Emitted(3, 20) Source(1, 21) + SourceIndex(0)
3 >Emitted(3, 22) Source(1, 12) + SourceIndex(0)
4 >Emitted(3, 43) Source(1, 21) + SourceIndex(0)
5 >Emitted(3, 45) Source(1, 12) + SourceIndex(0)
6 >Emitted(3, 49) Source(1, 21) + SourceIndex(0)
---
>>> _a[_i] = arguments[_i];
1 >^^^^^^^^
2 > ^^^^^^^^^^^^^^^^^^^^^^^
1 >
2 > ...{a, b}
1 >Emitted(4, 9) Source(1, 12) + SourceIndex(0)
2 >Emitted(4, 32) Source(1, 21) + SourceIndex(0)
---
>>> }
>>> var a = _a.a, b = _a.b;
1 >^^^^
2 > ^^^^
3 > ^^^^^^^^
4 > ^^
5 > ^^^^^^^^
6 > ^
1 >
2 > ...{
3 > a
4 > ,
5 > b
6 > }
1 >Emitted(6, 5) Source(1, 12) + SourceIndex(0)
2 >Emitted(6, 9) Source(1, 16) + SourceIndex(0)
3 >Emitted(6, 17) Source(1, 17) + SourceIndex(0)
4 >Emitted(6, 19) Source(1, 19) + SourceIndex(0)
5 >Emitted(6, 27) Source(1, 20) + SourceIndex(0)
6 >Emitted(6, 28) Source(1, 21) + SourceIndex(0)
---
>>>}
1 >
2 >^
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >) {
2 >}
1 >Emitted(7, 1) Source(1, 25) + SourceIndex(0)
2 >Emitted(7, 2) Source(1, 26) + SourceIndex(0)
---
>>>//# sourceMappingURL=restParameterWithBindingPattern1.js.map

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d4055a3

Please sign in to comment.