Skip to content

Commit f027812

Browse files
committed
Fix it correctly this time
1 parent cd4f644 commit f027812

8 files changed

+37
-31
lines changed

src/compiler/parser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5648,6 +5648,11 @@ namespace Parser {
56485648
case SyntaxKind.VoidKeyword:
56495649
return parseVoidExpression();
56505650
case SyntaxKind.LessThanToken:
5651+
// Just like in parseUpdateExpression, we need to avoid parsing type assertions when
5652+
// in JSX and we see an expression like "+ <foo> bar".
5653+
if (languageVariant === LanguageVariant.JSX && token() === SyntaxKind.LessThanToken && lookAhead(nextTokenIsIdentifierOrKeywordOrGreaterThan)) {
5654+
return parseJsxElementOrSelfClosingElementOrFragment(/*inExpressionContext*/ true);
5655+
}
56515656
// This is modified UnaryExpression grammar in TypeScript
56525657
// UnaryExpression (modified):
56535658
// < type > UnaryExpression
@@ -6163,6 +6168,7 @@ namespace Parser {
61636168
}
61646169

61656170
function parseTypeAssertion(): TypeAssertion {
6171+
Debug.assert(scriptKind === ScriptKind.TS || scriptKind === ScriptKind.TSX, "Type assertions should never be parsed outside of TS/TSX; they should either be comparisons or JSX.");
61666172
const pos = getNodePos();
61676173
parseExpected(SyntaxKind.LessThanToken);
61686174
const type = parseType();

src/compiler/program.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,8 +2936,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
29362936
diagnostics.push(createDiagnosticForNode((node as SatisfiesExpression).type, Diagnostics.Type_satisfaction_expressions_can_only_be_used_in_TypeScript_files));
29372937
return "skip";
29382938
case SyntaxKind.TypeAssertionExpression:
2939-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.Type_assertion_expressions_can_only_be_used_in_TypeScript_files));
2940-
return "skip";
2939+
Debug.fail(); // Won't parse these in a JS file anyway, as they are interpreted as JSX.
29412940
}
29422941
}
29432942

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/index.js(2,13): error TS17004: Cannot use JSX unless the '--jsx' flag is provided.
2+
tests/cases/compiler/index.js(2,14): error TS17008: JSX element 'number' has no corresponding closing tag.
3+
tests/cases/compiler/index.js(3,1): error TS1005: '</' expected.
4+
5+
6+
==== tests/cases/compiler/index.js (3 errors) ====
7+
const x = "oops";
8+
const y = + <number> x;
9+
~~~~~~~~
10+
!!! error TS17004: Cannot use JSX unless the '--jsx' flag is provided.
11+
~~~~~~
12+
!!! error TS17008: JSX element 'number' has no corresponding closing tag.
13+
14+
15+
!!! error TS1005: '</' expected.

tests/baselines/reference/typeAssertionExpressionInJsNoCrash.symbols renamed to tests/baselines/reference/parseUnaryExpressionNoTypeAssertionInJsx.symbols

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
const x = "oops";
33
>x : Symbol(x, Decl(index.js, 0, 5))
44

5-
const y = delete <number> x;
5+
const y = + <number> x;
66
>y : Symbol(y, Decl(index.js, 1, 5))
7-
>x : Symbol(x, Decl(index.js, 0, 5))
87

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.js ===
2+
const x = "oops";
3+
>x : "oops"
4+
>"oops" : "oops"
5+
6+
const y = + <number> x;
7+
>y : number
8+
>+ <number> x; : number
9+
><number> x; : any
10+
>number : any
11+
12+
> : any
13+

tests/baselines/reference/typeAssertionExpressionInJsNoCrash.errors.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/baselines/reference/typeAssertionExpressionInJsNoCrash.types

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/cases/compiler/typeAssertionExpressionInJsNoCrash.ts renamed to tests/cases/compiler/parseUnaryExpressionNoTypeAssertionInJsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
// @filename: index.js
55

66
const x = "oops";
7-
const y = delete <number> x;
7+
const y = + <number> x;

0 commit comments

Comments
 (0)