Skip to content

Commit

Permalink
Allow trailing commas after rest elements (#272)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alangpierce authored Jun 25, 2018
1 parent 94e6a81 commit 781cad1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/parser/traverser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/parser/traverser/lval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
`,
);
});
});

0 comments on commit 781cad1

Please # to comment.