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

Fix crash on destructured params in arrow function types #278

Merged
merged 1 commit into from
Jun 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/parser/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,21 @@ function tsSkipParameterStart(): boolean {
next();
return true;
}
// If this is a possible array/object destructure, walk to the matching bracket/brace.
// The next token after will tell us definitively whether this is a function param.
if (match(tt.braceL) || match(tt.bracketL)) {
let depth = 1;
next();
while (depth > 0) {
if (match(tt.braceL) || match(tt.bracketL)) {
depth++;
} else if (match(tt.braceR) || match(tt.bracketR)) {
depth--;
}
next();
}
return true;
}
return false;
}

Expand Down
17 changes: 17 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,4 +1011,21 @@ describe("typescript transform", () => {
`,
);
});

it("allows destructured params in function types", () => {
assertTypeScriptResult(
`
const f: ({a}: {a: number}) => void = () => {};
const g: ([a]: Array<number>) => void = () => {};
const h: ({a: {b: [c]}}: any) => void = () => {};
const o: ({a: {b: c}}) = {};
`,
`"use strict";
const f = () => {};
const g = () => {};
const h = () => {};
const o = {};
`,
);
});
});