Skip to content

Commit c89f2b7

Browse files
committed
Properly parse function/constructor types with destructuring parameters
1 parent 0855933 commit c89f2b7

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/compiler/parser.ts

+26-10
Original file line numberDiff line numberDiff line change
@@ -2450,27 +2450,43 @@ namespace ts {
24502450
if (token === SyntaxKind.LessThanToken) {
24512451
return true;
24522452
}
2453-
24542453
return token === SyntaxKind.OpenParenToken && lookAhead(isUnambiguouslyStartOfFunctionType);
24552454
}
24562455

2456+
function skipParameterStart(): boolean {
2457+
if (isModifierKind(token)) {
2458+
// Skip modifiers
2459+
parseModifiers();
2460+
}
2461+
if (isIdentifier()) {
2462+
nextToken();
2463+
return true;
2464+
}
2465+
if (token === SyntaxKind.OpenBracketToken || token === SyntaxKind.OpenBraceToken) {
2466+
// Return true if we can parse an array or object binding pattern with no errors
2467+
const count = parseDiagnostics.length;
2468+
parseIdentifierOrPattern();
2469+
return count === parseDiagnostics.length;
2470+
}
2471+
return false;
2472+
}
2473+
24572474
function isUnambiguouslyStartOfFunctionType() {
24582475
nextToken();
24592476
if (token === SyntaxKind.CloseParenToken || token === SyntaxKind.DotDotDotToken) {
24602477
// ( )
24612478
// ( ...
24622479
return true;
24632480
}
2464-
if (isIdentifier() || isModifierKind(token)) {
2465-
nextToken();
2481+
if (skipParameterStart()) {
2482+
// We successfully skipped modifiers (if any) and an identifier or binding pattern,
2483+
// now see if we have something that indicates a parameter declaration
24662484
if (token === SyntaxKind.ColonToken || token === SyntaxKind.CommaToken ||
2467-
token === SyntaxKind.QuestionToken || token === SyntaxKind.EqualsToken ||
2468-
isIdentifier() || isModifierKind(token)) {
2469-
// ( id :
2470-
// ( id ,
2471-
// ( id ?
2472-
// ( id =
2473-
// ( modifier id
2485+
token === SyntaxKind.QuestionToken || token === SyntaxKind.EqualsToken) {
2486+
// ( xxx :
2487+
// ( xxx ,
2488+
// ( xxx ?
2489+
// ( xxx =
24742490
return true;
24752491
}
24762492
if (token === SyntaxKind.CloseParenToken) {

0 commit comments

Comments
 (0)