Skip to content

Commit

Permalink
allow BindingPattern in FunctionRestParameter
Browse files Browse the repository at this point in the history
also add downlevel emit for the destructured rest param

Part of microsoft#6275
  • Loading branch information
ajafff committed Jul 27, 2018
1 parent 3bfe91c commit d4f6b9b
Show file tree
Hide file tree
Showing 16 changed files with 42 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 @@ -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.

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

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

0 comments on commit d4f6b9b

Please sign in to comment.