Skip to content

Commit b2d5d9b

Browse files
TypeScript Botjakebailey
TypeScript Bot
andauthored
🤖 Pick PR #53666 (Disable JSX recovery hack when in u...) into release-5.0 (#53676)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 0e198c2 commit b2d5d9b

11 files changed

+71
-3
lines changed

‎src/compiler/parser.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -5684,7 +5684,7 @@ namespace Parser {
56845684
// Just like in parseUpdateExpression, we need to avoid parsing type assertions when
56855685
// in JSX and we see an expression like "+ <foo> bar".
56865686
if (languageVariant === LanguageVariant.JSX) {
5687-
return parseJsxElementOrSelfClosingElementOrFragment(/*inExpressionContext*/ true);
5687+
return parseJsxElementOrSelfClosingElementOrFragment(/*inExpressionContext*/ true, /*topInvalidNodePosition*/ undefined, /*openingTag*/ undefined, /*mustBeUnary*/ true);
56885688
}
56895689
// This is modified UnaryExpression grammar in TypeScript
56905690
// UnaryExpression (modified):
@@ -5911,7 +5911,7 @@ namespace Parser {
59115911
return finishNode(factoryCreatePropertyAccessExpression(expression, parseRightSideOfDot(/*allowIdentifierNames*/ true, /*allowPrivateIdentifiers*/ true)), pos);
59125912
}
59135913

5914-
function parseJsxElementOrSelfClosingElementOrFragment(inExpressionContext: boolean, topInvalidNodePosition?: number, openingTag?: JsxOpeningElement | JsxOpeningFragment): JsxElement | JsxSelfClosingElement | JsxFragment {
5914+
function parseJsxElementOrSelfClosingElementOrFragment(inExpressionContext: boolean, topInvalidNodePosition?: number, openingTag?: JsxOpeningElement | JsxOpeningFragment, mustBeUnary = false): JsxElement | JsxSelfClosingElement | JsxFragment {
59155915
const pos = getNodePos();
59165916
const opening = parseJsxOpeningOrSelfClosingElementOrOpeningFragment(inExpressionContext);
59175917
let result: JsxElement | JsxSelfClosingElement | JsxFragment;
@@ -5968,7 +5968,9 @@ namespace Parser {
59685968
// does less damage and we can report a better error.
59695969
// Since JSX elements are invalid < operands anyway, this lookahead parse will only occur in error scenarios
59705970
// of one sort or another.
5971-
if (inExpressionContext && token() === SyntaxKind.LessThanToken) {
5971+
// If we are in a unary context, we can't do this recovery; the binary expression we return here is not
5972+
// a valid UnaryExpression and will cause problems later.
5973+
if (!mustBeUnary && inExpressionContext && token() === SyntaxKind.LessThanToken) {
59725974
const topBadPos = typeof topInvalidNodePosition === "undefined" ? result.pos : topInvalidNodePosition;
59735975
const invalidElement = tryParse(() => parseJsxElementOrSelfClosingElementOrFragment(/*inExpressionContext*/ true, topBadPos));
59745976
if (invalidElement) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/a.js(1,4): error TS1003: Identifier expected.
2+
tests/cases/compiler/a.js(1,5): error TS1109: Expression expected.
3+
4+
5+
==== tests/cases/compiler/a.js (2 errors) ====
6+
~< <
7+
~
8+
!!! error TS1003: Identifier expected.
9+
10+
!!! error TS1109: Expression expected.
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [a.js]
2+
~< <
3+
4+
5+
//// [a.js]
6+
~< /> <
7+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/a.js ===
2+
3+
~< <
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/a.js ===
2+
~< <
3+
>~< < : boolean
4+
>~< : number
5+
>< : any
6+
> : any
7+
8+
> : any
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/compiler/a.js(1,9): error TS1109: Expression expected.
2+
3+
4+
==== tests/cases/compiler/a.js (1 errors) ====
5+
~<></> <
6+
7+
!!! error TS1109: Expression expected.
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [a.js]
2+
~<></> <
3+
4+
5+
//// [a.js]
6+
~<></> <
7+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/a.js ===
2+
3+
~<></> <
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/a.js ===
2+
~<></> <
3+
>~<></> < : boolean
4+
>~<></> : number
5+
><></> : any
6+
7+
> : any
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @allowJs: true
2+
// @outDir: ./out
3+
// @filename: a.js
4+
~< <
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @allowJs: true
2+
// @outDir: ./out
3+
// @filename: a.js
4+
~<></> <

0 commit comments

Comments
 (0)