Skip to content

Commit

Permalink
fix: improve diagnose for invalid generic function call
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrCai0907 committed Nov 6, 2023
1 parent af2a250 commit 341def6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4278,6 +4278,24 @@ export class Parser extends DiagnosticEmitter {
case Token.In: {
let next = this.parseExpression(tn, nextPrecedence + 1);
if (!next) return null;
let state = tn.mark();
if (
expr.kind != NodeKind.Literal &&
token == Token.LessThan &&
tn.skip(Token.GreaterThan) &&
tn.skip(Token.OpenParen) &&
tn.skip(Token.CloseParen) &&
!(tn.skip(Token.Equals_GreaterThan) || tn.skip(Token.Colon))
) {
// detect Expr '<' Expr '>' '(' ')'
// except Expr '<' Expr '>' '(' ')' '=>'
// except Expr '<' Expr '>' '(' ')' ':'
this.error(DiagnosticCode.Type_expected, next.range);
tn.discard(state);
return null;
}
tn.reset(state);
tn.discard(state);
expr = Node.createBinaryExpression(token, expr, next, tn.range(startPos, tn.pos));
break;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/parser/diagnose-generic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
true<false>(true);
a<12345>() => {};
a<12345>() : void => {};
1 < 2,3,4 > (1,2,3);
{
a<12345>();
}
{
1 + a<12345>() + 1;
}
8 changes: 8 additions & 0 deletions tests/parser/diagnose-generic.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
true < false > (true);
a < 12345 > () => {};
a < 12345 > (): void => {};
1 < 2,3,4 > (1,2,3);
{}
{}
// ERROR 1110: "Type expected." in diagnose-generic.ts(6,5+5)
// ERROR 1110: "Type expected." in diagnose-generic.ts(9,9+5)

0 comments on commit 341def6

Please sign in to comment.