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

Properly handle optional arrow params without type annotations #389

Merged
merged 1 commit into from
Jan 3, 2019
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
10 changes: 7 additions & 3 deletions src/parser/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import {tsParseTypeAnnotation} from "./typescript";
// An apparent conditional expression could actually be an optional parameter in an arrow function.
export function typedParseConditional(noIn: boolean): void {
// If we see ?:, this can't possibly be a valid conditional. typedParseParenItem will be called
// later to finish off the arrow parameter.
if (match(tt.question) && lookaheadType() === tt.colon) {
return;
// later to finish off the arrow parameter. We also need to handle bare ? tokens for optional
// parameters without type annotations, i.e. ?, and ?) .
if (match(tt.question)) {
const nextType = lookaheadType();
if (nextType === tt.colon || nextType === tt.comma || nextType === tt.parenR) {
return;
}
}
baseParseConditional(noIn);
}
Expand Down
17 changes: 17 additions & 0 deletions test/types-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,21 @@ describe("type transforms", () => {
`,
);
});

it("handles optional params without type annotations", () => {
assertTypeScriptAndFlowResult(
`
const test = (a?) => a;
function test2(a?) {
return a;
}
`,
`"use strict";
const test = (a) => a;
function test2(a) {
return a;
}
`,
);
});
});