diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 20e6310f77195..08b3b80918901 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -29969,10 +29969,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); } diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 66f635a31e9cd..c8892707b5dd7 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -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); } /** @@ -1370,11 +1370,11 @@ namespace ts { } // `declarationName` is the name of the local declaration for the parameter. - const declarationName = getMutableClone(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(parameter!.name); + const expressionName = parameter.name.kind === SyntaxKind.Identifier ? getSynthesizedClone(parameter.name) : declarationName; const restIndex = node.parameters.length - 1; const temp = createLoopVariable(); @@ -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 + ) + ); + } } /** diff --git a/tests/baselines/reference/iterableArrayPattern14.errors.txt b/tests/baselines/reference/iterableArrayPattern14.errors.txt deleted file mode 100644 index 664f73e13a04f..0000000000000 --- a/tests/baselines/reference/iterableArrayPattern14.errors.txt +++ /dev/null @@ -1,23 +0,0 @@ -tests/cases/conformance/es6/destructuring/iterableArrayPattern14.ts(16,17): error TS2501: A rest element cannot contain a binding pattern. - - -==== tests/cases/conformance/es6/destructuring/iterableArrayPattern14.ts (1 errors) ==== - class Bar { x } - class Foo extends Bar { y } - class FooIterator { - next() { - return { - value: new Foo, - done: false - }; - } - - [Symbol.iterator]() { - return this; - } - } - - function fun(...[a, ...b]) { } - ~~~~~~~~~ -!!! error TS2501: A rest element cannot contain a binding pattern. - fun(new FooIterator); \ No newline at end of file diff --git a/tests/baselines/reference/iterableArrayPattern15.errors.txt b/tests/baselines/reference/iterableArrayPattern15.errors.txt deleted file mode 100644 index ce7559de94f62..0000000000000 --- a/tests/baselines/reference/iterableArrayPattern15.errors.txt +++ /dev/null @@ -1,23 +0,0 @@ -tests/cases/conformance/es6/destructuring/iterableArrayPattern15.ts(16,17): error TS2501: A rest element cannot contain a binding pattern. - - -==== tests/cases/conformance/es6/destructuring/iterableArrayPattern15.ts (1 errors) ==== - class Bar { x } - class Foo extends Bar { y } - class FooIterator { - next() { - return { - value: new Foo, - done: false - }; - } - - [Symbol.iterator]() { - return this; - } - } - - function fun(...[a, b]: Bar[]) { } - ~~~~~~ -!!! error TS2501: A rest element cannot contain a binding pattern. - fun(...new FooIterator); \ No newline at end of file diff --git a/tests/baselines/reference/iterableArrayPattern16.errors.txt b/tests/baselines/reference/iterableArrayPattern16.errors.txt index 78adabcad59d7..13b3981a94d90 100644 --- a/tests/baselines/reference/iterableArrayPattern16.errors.txt +++ b/tests/baselines/reference/iterableArrayPattern16.errors.txt @@ -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]'. diff --git a/tests/baselines/reference/iterableArrayPattern17.errors.txt b/tests/baselines/reference/iterableArrayPattern17.errors.txt index 4de8c2a817274..2a3091adc5bc9 100644 --- a/tests/baselines/reference/iterableArrayPattern17.errors.txt +++ b/tests/baselines/reference/iterableArrayPattern17.errors.txt @@ -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 { @@ -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'. diff --git a/tests/baselines/reference/iterableArrayPattern20.errors.txt b/tests/baselines/reference/iterableArrayPattern20.errors.txt deleted file mode 100644 index 23f133238cebf..0000000000000 --- a/tests/baselines/reference/iterableArrayPattern20.errors.txt +++ /dev/null @@ -1,23 +0,0 @@ -tests/cases/conformance/es6/destructuring/iterableArrayPattern20.ts(16,17): error TS2501: A rest element cannot contain a binding pattern. - - -==== tests/cases/conformance/es6/destructuring/iterableArrayPattern20.ts (1 errors) ==== - class Bar { x } - class Foo extends Bar { y } - class FooArrayIterator { - next() { - return { - value: [new Foo], - done: false - }; - } - - [Symbol.iterator]() { - return this; - } - } - - function fun(...[[a = new Foo], b = [new Foo]]: Bar[][]) { } - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2501: A rest element cannot contain a binding pattern. - fun(...new FooArrayIterator); \ No newline at end of file diff --git a/tests/baselines/reference/iterableArrayPattern25.errors.txt b/tests/baselines/reference/iterableArrayPattern25.errors.txt index 19f2dca07f392..0161be07d4bfa 100644 --- a/tests/baselines/reference/iterableArrayPattern25.errors.txt +++ b/tests/baselines/reference/iterableArrayPattern25.errors.txt @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/iterableArrayPattern26.errors.txt b/tests/baselines/reference/iterableArrayPattern26.errors.txt index 021a404f8a39d..f56f56e4504c5 100644 --- a/tests/baselines/reference/iterableArrayPattern26.errors.txt +++ b/tests/baselines/reference/iterableArrayPattern26.errors.txt @@ -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' is not assignable to parameter of type '[string, number]'. Type 'Map' 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' is not assignable to parameter of type '[string, number]'. diff --git a/tests/baselines/reference/iterableArrayPattern27.errors.txt b/tests/baselines/reference/iterableArrayPattern27.errors.txt deleted file mode 100644 index 99914ddc50815..0000000000000 --- a/tests/baselines/reference/iterableArrayPattern27.errors.txt +++ /dev/null @@ -1,8 +0,0 @@ -tests/cases/conformance/es6/destructuring/iterableArrayPattern27.ts(1,33): error TS2501: A rest element cannot contain a binding pattern. - - -==== tests/cases/conformance/es6/destructuring/iterableArrayPattern27.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]])); \ No newline at end of file diff --git a/tests/baselines/reference/iterableArrayPattern28.errors.txt b/tests/baselines/reference/iterableArrayPattern28.errors.txt index 26ce99e048aa7..f90bba07c1a19 100644 --- a/tests/baselines/reference/iterableArrayPattern28.errors.txt +++ b/tests/baselines/reference/iterableArrayPattern28.errors.txt @@ -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'. \ No newline at end of file diff --git a/tests/baselines/reference/iterableArrayPattern29.errors.txt b/tests/baselines/reference/iterableArrayPattern29.errors.txt index 29e5d0d2a3f98..a978e357cdf01 100644 --- a/tests/baselines/reference/iterableArrayPattern29.errors.txt +++ b/tests/baselines/reference/iterableArrayPattern29.errors.txt @@ -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]'. diff --git a/tests/baselines/reference/restParameterWithBindingPattern1.errors.txt b/tests/baselines/reference/restParameterWithBindingPattern1.errors.txt deleted file mode 100644 index 7194159a220e3..0000000000000 --- a/tests/baselines/reference/restParameterWithBindingPattern1.errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -tests/cases/compiler/restParameterWithBindingPattern1.ts(1,15): error TS2501: A rest element cannot contain a binding pattern. - - -==== tests/cases/compiler/restParameterWithBindingPattern1.ts (1 errors) ==== - function a(...{a, b}) { } - ~~~~~~ -!!! error TS2501: A rest element cannot contain a binding pattern. \ No newline at end of file diff --git a/tests/baselines/reference/restParameterWithBindingPattern1.js b/tests/baselines/reference/restParameterWithBindingPattern1.js index 9fb45768afe33..d872d23245067 100644 --- a/tests/baselines/reference/restParameterWithBindingPattern1.js +++ b/tests/baselines/reference/restParameterWithBindingPattern1.js @@ -2,4 +2,11 @@ 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; +} +//# sourceMappingURL=restParameterWithBindingPattern1.js.map \ No newline at end of file diff --git a/tests/baselines/reference/restParameterWithBindingPattern1.js.map b/tests/baselines/reference/restParameterWithBindingPattern1.js.map new file mode 100644 index 0000000000000..94c277257b9fd --- /dev/null +++ b/tests/baselines/reference/restParameterWithBindingPattern1.js.map @@ -0,0 +1,2 @@ +//// [restParameterWithBindingPattern1.js.map] +{"version":3,"file":"restParameterWithBindingPattern1.js","sourceRoot":"","sources":["restParameterWithBindingPattern1.ts"],"names":[],"mappings":"AAAA,SAAS,CAAC;IAAC,YAAS;SAAT,UAAS,EAAT,qBAAS,EAAT,IAAS;QAAT,uBAAS;;IAAT,IAAI,QAAC,EAAE,QAAC,CAAC;AAAI,CAAC"} \ No newline at end of file diff --git a/tests/baselines/reference/restParameterWithBindingPattern1.sourcemap.txt b/tests/baselines/reference/restParameterWithBindingPattern1.sourcemap.txt new file mode 100644 index 0000000000000..a5aa2351bc08b --- /dev/null +++ b/tests/baselines/reference/restParameterWithBindingPattern1.sourcemap.txt @@ -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 \ No newline at end of file diff --git a/tests/baselines/reference/restParameterWithBindingPattern2.errors.txt b/tests/baselines/reference/restParameterWithBindingPattern2.errors.txt deleted file mode 100644 index e69e1c2ba78a6..0000000000000 --- a/tests/baselines/reference/restParameterWithBindingPattern2.errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -tests/cases/compiler/restParameterWithBindingPattern2.ts(1,15): error TS2501: A rest element cannot contain a binding pattern. - - -==== tests/cases/compiler/restParameterWithBindingPattern2.ts (1 errors) ==== - function a(...[a, b]) { } - ~~~~~~ -!!! error TS2501: A rest element cannot contain a binding pattern. \ No newline at end of file diff --git a/tests/baselines/reference/restParameterWithBindingPattern2.js b/tests/baselines/reference/restParameterWithBindingPattern2.js index 5c97769acbbb3..acef0954194c4 100644 --- a/tests/baselines/reference/restParameterWithBindingPattern2.js +++ b/tests/baselines/reference/restParameterWithBindingPattern2.js @@ -2,4 +2,11 @@ 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]; +} +//# sourceMappingURL=restParameterWithBindingPattern2.js.map \ No newline at end of file diff --git a/tests/baselines/reference/restParameterWithBindingPattern2.js.map b/tests/baselines/reference/restParameterWithBindingPattern2.js.map new file mode 100644 index 0000000000000..1e25b0f14bed3 --- /dev/null +++ b/tests/baselines/reference/restParameterWithBindingPattern2.js.map @@ -0,0 +1,2 @@ +//// [restParameterWithBindingPattern2.js.map] +{"version":3,"file":"restParameterWithBindingPattern2.js","sourceRoot":"","sources":["restParameterWithBindingPattern2.ts"],"names":[],"mappings":"AAAA,SAAS,CAAC;IAAC,YAAS;SAAT,UAAS,EAAT,qBAAS,EAAT,IAAS;QAAT,uBAAS;;IAAT,IAAI,SAAC,EAAE,SAAC,CAAC;AAAI,CAAC"} \ No newline at end of file diff --git a/tests/baselines/reference/restParameterWithBindingPattern2.sourcemap.txt b/tests/baselines/reference/restParameterWithBindingPattern2.sourcemap.txt new file mode 100644 index 0000000000000..7cd85ba9debb1 --- /dev/null +++ b/tests/baselines/reference/restParameterWithBindingPattern2.sourcemap.txt @@ -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 \ No newline at end of file diff --git a/tests/baselines/reference/restParameterWithBindingPattern3.errors.txt b/tests/baselines/reference/restParameterWithBindingPattern3.errors.txt new file mode 100644 index 0000000000000..96f066ca194ad --- /dev/null +++ b/tests/baselines/reference/restParameterWithBindingPattern3.errors.txt @@ -0,0 +1,36 @@ +tests/cases/compiler/restParameterWithBindingPattern3.ts(1,16): error TS2322: Type '1' is not assignable to type 'string'. +tests/cases/compiler/restParameterWithBindingPattern3.ts(1,23): error TS2322: Type 'true' is not assignable to type 'string'. +tests/cases/compiler/restParameterWithBindingPattern3.ts(3,23): error TS1186: A rest element cannot have an initializer. +tests/cases/compiler/restParameterWithBindingPattern3.ts(5,30): error TS2493: Tuple type '[boolean, string, number]' of length '3' has no element at index '3'. +tests/cases/compiler/restParameterWithBindingPattern3.ts(7,23): error TS2493: Tuple type '[boolean, string, number]' of length '3' has no element at index '3'. +tests/cases/compiler/restParameterWithBindingPattern3.ts(9,19): error TS2322: Type '1' is not assignable to type 'boolean'. +tests/cases/compiler/restParameterWithBindingPattern3.ts(9,29): error TS2322: Type 'true' is not assignable to type 'string'. +tests/cases/compiler/restParameterWithBindingPattern3.ts(9,48): error TS2566: A rest element cannot have a property name. + + +==== tests/cases/compiler/restParameterWithBindingPattern3.ts (8 errors) ==== + function a(...[a = 1, b = true]: string[]) { } + ~ +!!! error TS2322: Type '1' is not assignable to type 'string'. + ~ +!!! error TS2322: Type 'true' is not assignable to type 'string'. + + function b(...[...foo = []]: string[]) { } + ~ +!!! error TS1186: A rest element cannot have an initializer. + + function c(...{0: a, length, 3: d}: [boolean, string, number]) { } + ~ +!!! error TS2493: Tuple type '[boolean, string, number]' of length '3' has no element at index '3'. + + function d(...[a, , , d]: [boolean, string, number]) { } + ~ +!!! error TS2493: Tuple type '[boolean, string, number]' of length '3' has no element at index '3'. + + function e(...{0: a = 1, 1: b = true, ...rest: rest}: [boolean, string, number]) { } + ~ +!!! error TS2322: Type '1' is not assignable to type 'boolean'. + ~ +!!! error TS2322: Type 'true' is not assignable to type 'string'. + ~~~~ +!!! error TS2566: A rest element cannot have a property name. \ No newline at end of file diff --git a/tests/baselines/reference/restParameterWithBindingPattern3.js b/tests/baselines/reference/restParameterWithBindingPattern3.js new file mode 100644 index 0000000000000..00342e7f741c5 --- /dev/null +++ b/tests/baselines/reference/restParameterWithBindingPattern3.js @@ -0,0 +1,56 @@ +//// [restParameterWithBindingPattern3.ts] +function a(...[a = 1, b = true]: string[]) { } + +function b(...[...foo = []]: string[]) { } + +function c(...{0: a, length, 3: d}: [boolean, string, number]) { } + +function d(...[a, , , d]: [boolean, string, number]) { } + +function e(...{0: a = 1, 1: b = true, ...rest: rest}: [boolean, string, number]) { } + +//// [restParameterWithBindingPattern3.js] +var __rest = (this && this.__rest) || function (s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) + t[p[i]] = s[p[i]]; + return t; +}; +function a() { + var _a = []; + for (var _i = 0; _i < arguments.length; _i++) { + _a[_i] = arguments[_i]; + } + var _b = _a[0], a = _b === void 0 ? 1 : _b, _c = _a[1], b = _c === void 0 ? true : _c; +} +function b() { + var _a = []; + for (var _i = 0; _i < arguments.length; _i++) { + _a[_i] = arguments[_i]; + } + var _b = _a.slice(0), foo = _b === void 0 ? [] : _b; +} +function c() { + var _a = []; + for (var _i = 0; _i < arguments.length; _i++) { + _a[_i] = arguments[_i]; + } + var a = _a[0], length = _a.length, d = _a[3]; +} +function d() { + var _a = []; + for (var _i = 0; _i < arguments.length; _i++) { + _a[_i] = arguments[_i]; + } + var a = _a[0], d = _a[3]; +} +function e() { + var _a = []; + for (var _i = 0; _i < arguments.length; _i++) { + _a[_i] = arguments[_i]; + } + var _b = _a[0], a = _b === void 0 ? 1 : _b, _c = _a[1], b = _c === void 0 ? true : _c, rest = __rest(_a, [0, 1]); +} diff --git a/tests/baselines/reference/restParameterWithBindingPattern3.symbols b/tests/baselines/reference/restParameterWithBindingPattern3.symbols new file mode 100644 index 0000000000000..eecaba6d3392c --- /dev/null +++ b/tests/baselines/reference/restParameterWithBindingPattern3.symbols @@ -0,0 +1,27 @@ +=== tests/cases/compiler/restParameterWithBindingPattern3.ts === +function a(...[a = 1, b = true]: string[]) { } +>a : Symbol(a, Decl(restParameterWithBindingPattern3.ts, 0, 0)) +>a : Symbol(a, Decl(restParameterWithBindingPattern3.ts, 0, 15)) +>b : Symbol(b, Decl(restParameterWithBindingPattern3.ts, 0, 21)) + +function b(...[...foo = []]: string[]) { } +>b : Symbol(b, Decl(restParameterWithBindingPattern3.ts, 0, 46)) +>foo : Symbol(foo, Decl(restParameterWithBindingPattern3.ts, 2, 15)) + +function c(...{0: a, length, 3: d}: [boolean, string, number]) { } +>c : Symbol(c, Decl(restParameterWithBindingPattern3.ts, 2, 42)) +>a : Symbol(a, Decl(restParameterWithBindingPattern3.ts, 4, 15)) +>length : Symbol(length, Decl(restParameterWithBindingPattern3.ts, 4, 20)) +>d : Symbol(d, Decl(restParameterWithBindingPattern3.ts, 4, 28)) + +function d(...[a, , , d]: [boolean, string, number]) { } +>d : Symbol(d, Decl(restParameterWithBindingPattern3.ts, 4, 66)) +>a : Symbol(a, Decl(restParameterWithBindingPattern3.ts, 6, 15)) +>d : Symbol(d, Decl(restParameterWithBindingPattern3.ts, 6, 21)) + +function e(...{0: a = 1, 1: b = true, ...rest: rest}: [boolean, string, number]) { } +>e : Symbol(e, Decl(restParameterWithBindingPattern3.ts, 6, 56)) +>a : Symbol(a, Decl(restParameterWithBindingPattern3.ts, 8, 15)) +>b : Symbol(b, Decl(restParameterWithBindingPattern3.ts, 8, 24)) +>rest : Symbol(rest, Decl(restParameterWithBindingPattern3.ts, 8, 37)) + diff --git a/tests/baselines/reference/restParameterWithBindingPattern3.types b/tests/baselines/reference/restParameterWithBindingPattern3.types new file mode 100644 index 0000000000000..1385bde0c3fae --- /dev/null +++ b/tests/baselines/reference/restParameterWithBindingPattern3.types @@ -0,0 +1,35 @@ +=== tests/cases/compiler/restParameterWithBindingPattern3.ts === +function a(...[a = 1, b = true]: string[]) { } +>a : (...[a, b]: string[]) => void +>a : string +>1 : 1 +>b : string +>true : true + +function b(...[...foo = []]: string[]) { } +>b : (...[...foo]: string[]) => void +>foo : string[] +>[] : undefined[] + +function c(...{0: a, length, 3: d}: [boolean, string, number]) { } +>c : (__0_0: boolean, __0_1: string, __0_2: number) => void +>a : boolean +>length : 3 +>d : undefined + +function d(...[a, , , d]: [boolean, string, number]) { } +>d : (__0_0: boolean, __0_1: string, __0_2: number) => void +>a : boolean +> : undefined +> : undefined +>d : undefined + +function e(...{0: a = 1, 1: b = true, ...rest: rest}: [boolean, string, number]) { } +>e : (__0_0: boolean, __0_1: string, __0_2: number) => void +>a : boolean +>1 : 1 +>b : string +>true : true +>rest : any +>rest : { [n: number]: string | number | boolean; 0: boolean; 1: string; 2: number; length: 3; toString(): string; toLocaleString(): string; pop(): string | number | boolean; push(...items: (string | number | boolean)[]): number; concat(...items: ConcatArray[]): (string | number | boolean)[]; concat(...items: (string | number | boolean | ConcatArray)[]): (string | number | boolean)[]; join(separator?: string): string; reverse(): (string | number | boolean)[]; shift(): string | number | boolean; slice(start?: number, end?: number): (string | number | boolean)[]; sort(compareFn?: (a: string | number | boolean, b: string | number | boolean) => number): [boolean, string, number]; splice(start: number, deleteCount?: number): (string | number | boolean)[]; splice(start: number, deleteCount: number, ...items: (string | number | boolean)[]): (string | number | boolean)[]; unshift(...items: (string | number | boolean)[]): number; indexOf(searchElement: string | number | boolean, fromIndex?: number): number; lastIndexOf(searchElement: string | number | boolean, fromIndex?: number): number; every(callbackfn: (value: string | number | boolean, index: number, array: (string | number | boolean)[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: string | number | boolean, index: number, array: (string | number | boolean)[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: string | number | boolean, index: number, array: (string | number | boolean)[]) => void, thisArg?: any): void; map(callbackfn: (value: string | number | boolean, index: number, array: (string | number | boolean)[]) => U, thisArg?: any): U[]; filter(callbackfn: (value: string | number | boolean, index: number, array: (string | number | boolean)[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: string | number | boolean, index: number, array: (string | number | boolean)[]) => any, thisArg?: any): (string | number | boolean)[]; reduce(callbackfn: (previousValue: string | number | boolean, currentValue: string | number | boolean, currentIndex: number, array: (string | number | boolean)[]) => string | number | boolean): string | number | boolean; reduce(callbackfn: (previousValue: string | number | boolean, currentValue: string | number | boolean, currentIndex: number, array: (string | number | boolean)[]) => string | number | boolean, initialValue: string | number | boolean): string | number | boolean; reduce(callbackfn: (previousValue: U, currentValue: string | number | boolean, currentIndex: number, array: (string | number | boolean)[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: string | number | boolean, currentValue: string | number | boolean, currentIndex: number, array: (string | number | boolean)[]) => string | number | boolean): string | number | boolean; reduceRight(callbackfn: (previousValue: string | number | boolean, currentValue: string | number | boolean, currentIndex: number, array: (string | number | boolean)[]) => string | number | boolean, initialValue: string | number | boolean): string | number | boolean; reduceRight(callbackfn: (previousValue: U, currentValue: string | number | boolean, currentIndex: number, array: (string | number | boolean)[]) => U, initialValue: U): U; } + diff --git a/tests/cases/compiler/restParameterWithBindingPattern1.ts b/tests/cases/compiler/restParameterWithBindingPattern1.ts index 1004b0478440a..bd6a3b9bbda64 100644 --- a/tests/cases/compiler/restParameterWithBindingPattern1.ts +++ b/tests/cases/compiler/restParameterWithBindingPattern1.ts @@ -1 +1,3 @@ +// @sourcemap: true + function a(...{a, b}) { } \ No newline at end of file diff --git a/tests/cases/compiler/restParameterWithBindingPattern2.ts b/tests/cases/compiler/restParameterWithBindingPattern2.ts index 85076703b7963..d7057885040c5 100644 --- a/tests/cases/compiler/restParameterWithBindingPattern2.ts +++ b/tests/cases/compiler/restParameterWithBindingPattern2.ts @@ -1 +1,3 @@ +// @sourcemap: true + function a(...[a, b]) { } \ No newline at end of file diff --git a/tests/cases/compiler/restParameterWithBindingPattern3.ts b/tests/cases/compiler/restParameterWithBindingPattern3.ts new file mode 100644 index 0000000000000..f0792354c40dd --- /dev/null +++ b/tests/cases/compiler/restParameterWithBindingPattern3.ts @@ -0,0 +1,9 @@ +function a(...[a = 1, b = true]: string[]) { } + +function b(...[...foo = []]: string[]) { } + +function c(...{0: a, length, 3: d}: [boolean, string, number]) { } + +function d(...[a, , , d]: [boolean, string, number]) { } + +function e(...{0: a = 1, 1: b = true, ...rest: rest}: [boolean, string, number]) { } \ No newline at end of file