From 341def6bd78cca63b729d56027e1598eef1e2bc8 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Mon, 6 Nov 2023 15:11:59 +0800 Subject: [PATCH] fix: improve diagnose for invalid generic function call Fixes #2780 --- src/parser.ts | 18 ++++++++++++++++++ tests/parser/diagnose-generic.ts | 10 ++++++++++ tests/parser/diagnose-generic.ts.fixture.ts | 8 ++++++++ 3 files changed, 36 insertions(+) create mode 100644 tests/parser/diagnose-generic.ts create mode 100644 tests/parser/diagnose-generic.ts.fixture.ts diff --git a/src/parser.ts b/src/parser.ts index 58050e837e..81e83e3f88 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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; } diff --git a/tests/parser/diagnose-generic.ts b/tests/parser/diagnose-generic.ts new file mode 100644 index 0000000000..66fd949457 --- /dev/null +++ b/tests/parser/diagnose-generic.ts @@ -0,0 +1,10 @@ +true(true); +a<12345>() => {}; +a<12345>() : void => {}; +1 < 2,3,4 > (1,2,3); +{ + a<12345>(); +} +{ + 1 + a<12345>() + 1; +} diff --git a/tests/parser/diagnose-generic.ts.fixture.ts b/tests/parser/diagnose-generic.ts.fixture.ts new file mode 100644 index 0000000000..e2c0bf6eb7 --- /dev/null +++ b/tests/parser/diagnose-generic.ts.fixture.ts @@ -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)