Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow BindingPattern in FunctionRestParameter #26017

Merged
merged 8 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28570,10 +28570,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 @@ -1340,8 +1340,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 @@ -1360,11 +1360,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 @@ -1429,6 +1429,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]'.
Property '0' is missing in type 'FooIterator'.
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'.


==== 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]'.
Property '0' is missing in type 'Map<string, number>'.


==== 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,4 +1,3 @@
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,32): error TS2345: Argument of type '([string, number] | [string, boolean])[]' is not assignable to parameter of type 'ReadonlyArray<[string, number]>'.
Types of property 'concat' are incompatible.
Type '{ (...items: ConcatArray<[string, number] | [string, boolean]>[]): ([string, number] | [string, boolean])[]; (...items: ([string, number] | [string, boolean] | ConcatArray<[string, number] | [string, boolean]>)[]): ([string, number] | [string, boolean])[]; }' is not assignable to type '{ (...items: ConcatArray<[string, number]>[]): [string, number][]; (...items: ([string, number] | ConcatArray<[string, number]>)[]): [string, number][]; }'.
Expand All @@ -8,10 +7,8 @@ tests/cases/conformance/es6/destructuring/iterableArrayPattern28.ts(2,32): error
Type 'boolean' 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 TS2345: Argument of type '([string, number] | [string, boolean])[]' is not assignable to parameter of type 'ReadonlyArray<[string, number]>'.
Expand Down
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.

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
function a(...{a, b}) { }

//// [restParameterWithBindingPattern1.js]
function a() { }
function a() {
var _a = [];
for (var _i = 0; _i < arguments.length; _i++) {
_a[_i] = arguments[_i];
}
var a = _a.a, b = _a.b;
}

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: restParameterWithBindingPattern2.js
mapUrl: restParameterWithBindingPattern2.js.map
sourceRoot:
sources: restParameterWithBindingPattern2.ts
===================================================================
-------------------------------------------------------------------
emittedFile:tests/cases/compiler/restParameterWithBindingPattern2.js
sourceFile:restParameterWithBindingPattern2.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[0], b = _a[1];
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, 18) Source(1, 17) + SourceIndex(0)
4 >Emitted(6, 20) Source(1, 19) + SourceIndex(0)
5 >Emitted(6, 29) Source(1, 20) + SourceIndex(0)
6 >Emitted(6, 30) 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=restParameterWithBindingPattern2.js.map
Loading