Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Port babel-parser changes from 2020-04-12 to 2020-07-22 #556

Merged
merged 8 commits into from
Oct 13, 2020
15 changes: 11 additions & 4 deletions src/parser/plugins/typescript.ts
Expand Up @@ -367,6 +367,7 @@ function tsParseMappedType(): void {
function tsParseTupleType(): void {
expect(tt.bracketL);
while (!eat(tt.bracketR) && !state.error) {
// Do not validate presence of either none or only labeled elements
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't seem super-necessary; across the parser code, all validation is skipped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put it here because it replaces a large chunk of code in babel, but agree that it's pretty useless outside the context of porting babel changes

tsParseTupleElementType();
eat(tt.comma);
}
Expand All @@ -376,11 +377,17 @@ function tsParseTupleElementType(): void {
// parses `...TsType[]`
if (eat(tt.ellipsis)) {
tsParseType();
return;
} else {
// parses `TsType?`
tsParseType();
eat(tt.question);
}

// The type we parsed above was actually a label
if (eat(tt.colon)) {
// Labeled tuple types must affix the label with `...` or `?`, so no need to handle those here
tsParseType();
}
// parses `TsType?`
tsParseType();
eat(tt.question);
}

function tsParseParenthesizedType(): void {
Expand Down
13 changes: 13 additions & 0 deletions test/typescript-test.ts
Expand Up @@ -2200,4 +2200,17 @@ describe("typescript transform", () => {
`,
);
});

it("properly removes labeled tuple types", () => {
assertTypeScriptResult(
`
type T1 = [x: number, y?: number, ...rest: number[]];
function f(args: [s?: string, ...ns: number[]]) {}
`,
`"use strict";

function f(args) {}
`,
);
});
});