Skip to content

Commit a027cfa

Browse files
authored
More permissive parsing of instantiation expressions (microsoft#48659)
* More permissive parsing of expressions with type arguments * Add test
1 parent 988fa85 commit a027cfa

File tree

6 files changed

+42
-26
lines changed

6 files changed

+42
-26
lines changed

Diff for: src/compiler/parser.ts

+2-26
Original file line numberDiff line numberDiff line change
@@ -5735,34 +5735,10 @@ namespace ts {
57355735
case SyntaxKind.OpenParenToken: // foo<x>(
57365736
case SyntaxKind.NoSubstitutionTemplateLiteral: // foo<T> `...`
57375737
case SyntaxKind.TemplateHead: // foo<T> `...${100}...`
5738-
// These tokens can't follow in a call expression, nor can they start an
5739-
// expression. So, consider the type argument list part of an instantiation
5740-
// expression.
5741-
// falls through
5742-
case SyntaxKind.CommaToken: // foo<x>,
5743-
case SyntaxKind.DotToken: // foo<x>.
5744-
case SyntaxKind.QuestionDotToken: // foo<x>?.
5745-
case SyntaxKind.CloseParenToken: // foo<x>)
5746-
case SyntaxKind.CloseBracketToken: // foo<x>]
5747-
case SyntaxKind.ColonToken: // foo<x>:
5748-
case SyntaxKind.SemicolonToken: // foo<x>;
5749-
case SyntaxKind.QuestionToken: // foo<x>?
5750-
case SyntaxKind.EqualsEqualsToken: // foo<x> ==
5751-
case SyntaxKind.EqualsEqualsEqualsToken: // foo<x> ===
5752-
case SyntaxKind.ExclamationEqualsToken: // foo<x> !=
5753-
case SyntaxKind.ExclamationEqualsEqualsToken: // foo<x> !==
5754-
case SyntaxKind.AmpersandAmpersandToken: // foo<x> &&
5755-
case SyntaxKind.BarBarToken: // foo<x> ||
5756-
case SyntaxKind.QuestionQuestionToken: // foo<x> ??
5757-
case SyntaxKind.CaretToken: // foo<x> ^
5758-
case SyntaxKind.AmpersandToken: // foo<x> &
5759-
case SyntaxKind.BarToken: // foo<x> |
5760-
case SyntaxKind.CloseBraceToken: // foo<x> }
5761-
case SyntaxKind.EndOfFileToken: // foo<x>
57625738
return true;
57635739
}
5764-
// Treat anything else as an expression.
5765-
return false;
5740+
// Consider something a type argument list only if the following token can't start an expression.
5741+
return !isStartOfExpression();
57665742
}
57675743

57685744
function parsePrimaryExpression(): PrimaryExpression {

Diff for: tests/baselines/reference/instantiationExpressionErrors.errors.txt

+5
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpr
7272

7373
const x3 = f<true>;
7474
true;
75+
76+
// Parsed as instantiation expression
77+
78+
const x4 = f<true>
79+
if (true) {}
7580

Diff for: tests/baselines/reference/instantiationExpressionErrors.js

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ true;
4040

4141
const x3 = f<true>;
4242
true;
43+
44+
// Parsed as instantiation expression
45+
46+
const x4 = f<true>
47+
if (true) {}
4348

4449

4550
//// [instantiationExpressionErrors.js]
@@ -70,6 +75,9 @@ var x2 = f < true >
7075
// Parsed as instantiation expression
7176
var x3 = (f);
7277
true;
78+
// Parsed as instantiation expression
79+
var x4 = (f);
80+
if (true) { }
7381

7482

7583
//// [instantiationExpressionErrors.d.ts]
@@ -101,3 +109,7 @@ declare const x3: {
101109
(): true;
102110
g<U>(): U;
103111
};
112+
declare const x4: {
113+
(): true;
114+
g<U>(): U;
115+
};

Diff for: tests/baselines/reference/instantiationExpressionErrors.symbols

+8
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,11 @@ const x3 = f<true>;
9999

100100
true;
101101

102+
// Parsed as instantiation expression
103+
104+
const x4 = f<true>
105+
>x4 : Symbol(x4, Decl(instantiationExpressionErrors.ts, 44, 5))
106+
>f : Symbol(f, Decl(instantiationExpressionErrors.ts, 0, 11))
107+
108+
if (true) {}
109+

Diff for: tests/baselines/reference/instantiationExpressionErrors.types

+10
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,13 @@ const x3 = f<true>;
122122
true;
123123
>true : true
124124

125+
// Parsed as instantiation expression
126+
127+
const x4 = f<true>
128+
>x4 : { (): true; g<U>(): U; }
129+
>f : { <T>(): T; g<U>(): U; }
130+
>true : true
131+
132+
if (true) {}
133+
>true : true
134+

Diff for: tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressionErrors.ts

+5
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ true;
4242

4343
const x3 = f<true>;
4444
true;
45+
46+
// Parsed as instantiation expression
47+
48+
const x4 = f<true>
49+
if (true) {}

0 commit comments

Comments
 (0)