Skip to content
New issue

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

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

Already on GitHub? # to your account

Allow trailing commas after rest elements #272

Merged
merged 1 commit into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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;
`,
);
});
});