From ccfacc717051316ce6b598b69586dedd8a91e30e Mon Sep 17 00:00:00 2001 From: Alan Pierce Date: Sun, 24 Jun 2018 21:36:58 -0700 Subject: [PATCH] Allow trailing commas after rest elements Fixes #249 Nothing stops us from allowing it at parse time, and it's allowed in TS <2.9, so we support it for now. --- src/parser/traverser/expression.ts | 2 -- src/parser/traverser/lval.ts | 2 ++ test/typescript-test.ts | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/parser/traverser/expression.ts b/src/parser/traverser/expression.ts index f8d3b55d..4444a1d8 100644 --- a/src/parser/traverser/expression.ts +++ b/src/parser/traverser/expression.ts @@ -715,8 +715,6 @@ export function parseObj(isPattern: boolean, isBlockScope: boolean): void { unexpected(firstRestLocation, "Cannot have multiple rest elements when destructuring"); } else if (eat(tt.braceR)) { break; - } else if (match(tt.comma) && lookaheadType() === tt.braceR) { - unexpected(position, "A trailing comma is not permitted after the rest element"); } else { firstRestLocation = position; continue; diff --git a/src/parser/traverser/lval.ts b/src/parser/traverser/lval.ts index fcc2cbb1..7a3f8578 100644 --- a/src/parser/traverser/lval.ts +++ b/src/parser/traverser/lval.ts @@ -98,6 +98,8 @@ export function parseBindingList( } else if (match(tt.ellipsis)) { parseRest(isBlockScope); parseAssignableListItemTypes(); + // Support rest element trailing commas allowed by TypeScript <2.9. + eat(TokenType.comma); expect(close); break; } else { diff --git a/test/typescript-test.ts b/test/typescript-test.ts index f1deab42..f73bff13 100644 --- a/test/typescript-test.ts +++ b/test/typescript-test.ts @@ -973,4 +973,21 @@ describe("typescript transform", () => { `, ); }); + + // TODO: TypeScript 2.9 makes this required, so we can drop support for this syntax when we don't + // need to support older versions of TypeScript. + it("allows trailing commas after rest elements", () => { + assertTypeScriptResult( + ` + function foo(a, ...b,) {} + const {a, ...b,} = c; + const [a, ...b,] = c; + `, + `"use strict"; + function foo(a, ...b,) {} + const {a, ...b,} = c; + const [a, ...b,] = c; + `, + ); + }); });