Skip to content

Commit

Permalink
Fix crash on destructured params in arrow function types (#278)
Browse files Browse the repository at this point in the history
Fixes #277

There's a decision point on whether the upcoming type is a function type or not,
and it wasn't considering destructured params.
  • Loading branch information
alangpierce authored Jun 27, 2018
1 parent eb1d9b8 commit fa87ab0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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 = {};
`,
);
});
});

0 comments on commit fa87ab0

Please sign in to comment.