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 89f0f78
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4278,6 +4278,23 @@ 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)
) {
// detect 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
9 changes: 9 additions & 0 deletions tests/parser/diagnose-generic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
true<false>(true);
a<12345>() => {};
1 < 2,3,4 > (1,2,3);
{
a<12345>();
}
{
1 + a<12345>() + 1;
}
7 changes: 7 additions & 0 deletions tests/parser/diagnose-generic.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
true < false > (true);
a < 12345 > () => {};
1 < 2,3,4 > (1,2,3);
{}
{}
// ERROR 1110: "Type expected." in diagnose-generic.ts(5,5+5)
// ERROR 1110: "Type expected." in diagnose-generic.ts(8,9+5)

0 comments on commit 89f0f78

Please sign in to comment.