Skip to content

Commit 4ddf045

Browse files
author
Andy
authored
For <div a= >, after =, do not try to parse an expression (#21373)
1 parent f41555c commit 4ddf045

15 files changed

+80
-58
lines changed

src/compiler/parser.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4125,7 +4125,7 @@ namespace ts {
41254125
case SyntaxKind.JsxTextAllWhiteSpaces:
41264126
return parseJsxText();
41274127
case SyntaxKind.OpenBraceToken:
4128-
return parseJsxExpression(/*inExpressionContext*/ false);
4128+
return parseJsxExpression(/*inExpressionContext*/ false)!;
41294129
case SyntaxKind.LessThanToken:
41304130
return parseJsxElementOrSelfClosingElementOrFragment(/*inExpressionContext*/ false);
41314131
}
@@ -4236,10 +4236,13 @@ namespace ts {
42364236
return expression;
42374237
}
42384238

4239-
function parseJsxExpression(inExpressionContext: boolean): JsxExpression {
4239+
function parseJsxExpression(inExpressionContext: boolean): JsxExpression | undefined {
42404240
const node = <JsxExpression>createNode(SyntaxKind.JsxExpression);
42414241

4242-
parseExpected(SyntaxKind.OpenBraceToken);
4242+
if (!parseExpected(SyntaxKind.OpenBraceToken)) {
4243+
return undefined;
4244+
}
4245+
42434246
if (token() !== SyntaxKind.CloseBraceToken) {
42444247
node.dotDotDotToken = parseOptionalToken(SyntaxKind.DotDotDotToken);
42454248
node.expression = parseAssignmentExpressionOrHigher();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/compiler/jsxAttributeMissingInitializer.tsx(1,21): error TS1005: '{' expected.
2+
3+
4+
==== tests/cases/compiler/jsxAttributeMissingInitializer.tsx (1 errors) ====
5+
const x = <div foo= ></div>;
6+
~
7+
!!! error TS1005: '{' expected.
8+
const y = 0;
9+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [jsxAttributeMissingInitializer.tsx]
2+
const x = <div foo= ></div>;
3+
const y = 0;
4+
5+
6+
//// [jsxAttributeMissingInitializer.jsx]
7+
var x = <div foo></div>;
8+
var y = 0;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/jsxAttributeMissingInitializer.tsx ===
2+
const x = <div foo= ></div>;
3+
>x : Symbol(x, Decl(jsxAttributeMissingInitializer.tsx, 0, 5))
4+
>foo : Symbol(foo, Decl(jsxAttributeMissingInitializer.tsx, 0, 14))
5+
6+
const y = 0;
7+
>y : Symbol(y, Decl(jsxAttributeMissingInitializer.tsx, 1, 5))
8+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/jsxAttributeMissingInitializer.tsx ===
2+
const x = <div foo= ></div>;
3+
>x : any
4+
><div foo= ></div> : any
5+
>div : any
6+
>foo : true
7+
>div : any
8+
9+
const y = 0;
10+
>y : 0
11+
>0 : 0
12+

tests/baselines/reference/jsxEsprimaFbTestSuite.errors.txt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,1): error TS2695: Left side of comma operator is unused and has no side effects.
22
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,17): error TS1005: '{' expected.
3-
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,23): error TS1005: '}' expected.
4-
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,29): error TS1005: '{' expected.
3+
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,23): error TS2304: Cannot find name 'right'.
4+
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,23): error TS2657: JSX expressions must have one parent element.
55
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,57): error TS1109: Expression expected.
66
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(39,58): error TS1109: Expression expected.
7-
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(41,1): error TS1003: Identifier expected.
8-
tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(41,12): error TS2657: JSX expressions must have one parent element.
97

108

11-
==== tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx (8 errors) ====
9+
==== tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx (6 errors) ====
1210
declare var React: any;
1311
declare var 日本語;
1412
declare var AbC_def;
@@ -48,24 +46,20 @@ tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx(41,12): error TS2657: JSX
4846
<div><br />7x invalid-js-identifier</div>;
4947

5048
<LeftRight left=<a /> right=<b>monkeys /> gorillas</b> />;
51-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49+
~~~~~~~~~~~~~~~~
5250
!!! error TS2695: Left side of comma operator is unused and has no side effects.
5351
~
5452
!!! error TS1005: '{' expected.
5553
~~~~~
56-
!!! error TS1005: '}' expected.
57-
~
58-
!!! error TS1005: '{' expected.
54+
!!! error TS2304: Cannot find name 'right'.
55+
~~~~~
56+
!!! error TS2657: JSX expressions must have one parent element.
5957
~
6058
!!! error TS1109: Expression expected.
6159
~
6260
!!! error TS1109: Expression expected.
6361

6462
<a.b></a.b>;
65-
~
66-
!!! error TS1003: Identifier expected.
67-
~
68-
!!! error TS2657: JSX expressions must have one parent element.
6963

7064
<a.b.c></a.b.c>;
7165

tests/baselines/reference/jsxEsprimaFbTestSuite.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ baz
7171
<a></a>;
7272
<div>@test content</div>;
7373
<div><br />7x invalid-js-identifier</div>;
74-
<LeftRight left={<a />} right={<b>monkeys /> gorillas</b> / > }/>
75-
,
76-
<a.b></a.b>;
74+
<LeftRight left/>, <a />;
75+
right = <b>monkeys /> gorillas</b> / > ;
76+
<a.b></a.b>;
7777
<a.b.c></a.b.c>;
7878
(<div />) < x;
7979
<div {...props}/>;

tests/baselines/reference/jsxEsprimaFbTestSuite.symbols

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ baz
7171
<LeftRight left=<a /> right=<b>monkeys /> gorillas</b> />;
7272
>LeftRight : Symbol(LeftRight, Decl(jsxEsprimaFbTestSuite.tsx, 3, 11))
7373
>left : Symbol(left, Decl(jsxEsprimaFbTestSuite.tsx, 38, 10))
74-
>right : Symbol(right, Decl(jsxEsprimaFbTestSuite.tsx, 38, 21))
7574

7675
<a.b></a.b>;
7776
>a : Symbol(a, Decl(jsxEsprimaFbTestSuite.tsx, 5, 11))

tests/baselines/reference/jsxEsprimaFbTestSuite.types

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,14 @@ baz
102102
>div : any
103103

104104
<LeftRight left=<a /> right=<b>monkeys /> gorillas</b> />;
105-
><LeftRight left=<a /> right=<b>monkeys /> gorillas</b> />;<a.b></a.b> : any
106-
><LeftRight left=<a /> right=<b>monkeys /> gorillas</b> />; : any
105+
><LeftRight left=<a /> : any
106+
><LeftRight left= : any
107107
>LeftRight : any
108-
>left : any
108+
>left : true
109109
><a /> : any
110110
>a : any
111-
>right : boolean
111+
>right=<b>monkeys /> gorillas</b> /> : boolean
112+
>right : any
112113
><b>monkeys /> gorillas</b> /> : boolean
113114
><b>monkeys /> gorillas</b> / : number
114115
><b>monkeys /> gorillas</b> : any

tests/baselines/reference/jsxInvalidEsprimaTestSuite.errors.txt

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ tests/cases/conformance/jsx/25.tsx(1,39): error TS1109: Expression expected.
5858
tests/cases/conformance/jsx/28.tsx(1,2): error TS17008: JSX element 'a' has no corresponding closing tag.
5959
tests/cases/conformance/jsx/28.tsx(1,6): error TS1005: '{' expected.
6060
tests/cases/conformance/jsx/28.tsx(2,1): error TS1005: '</' expected.
61-
tests/cases/conformance/jsx/29.tsx(1,2): error TS17008: JSX element 'a' has no corresponding closing tag.
61+
tests/cases/conformance/jsx/29.tsx(1,1): error TS2695: Left side of comma operator is unused and has no side effects.
6262
tests/cases/conformance/jsx/29.tsx(1,6): error TS1005: '{' expected.
63-
tests/cases/conformance/jsx/29.tsx(1,7): error TS1109: Expression expected.
63+
tests/cases/conformance/jsx/29.tsx(1,7): error TS1003: Identifier expected.
6464
tests/cases/conformance/jsx/29.tsx(2,1): error TS1005: '</' expected.
6565
tests/cases/conformance/jsx/3.tsx(1,1): error TS1109: Expression expected.
6666
tests/cases/conformance/jsx/3.tsx(1,2): error TS1109: Expression expected.
@@ -69,10 +69,6 @@ tests/cases/conformance/jsx/3.tsx(1,6): error TS1109: Expression expected.
6969
tests/cases/conformance/jsx/3.tsx(1,7): error TS1109: Expression expected.
7070
tests/cases/conformance/jsx/31.tsx(1,4): error TS1003: Identifier expected.
7171
tests/cases/conformance/jsx/4.tsx(1,6): error TS1005: '{' expected.
72-
tests/cases/conformance/jsx/4.tsx(1,6): error TS2304: Cannot find name 'd'.
73-
tests/cases/conformance/jsx/4.tsx(1,9): error TS1109: Expression expected.
74-
tests/cases/conformance/jsx/4.tsx(1,10): error TS1109: Expression expected.
75-
tests/cases/conformance/jsx/4.tsx(1,11): error TS1005: '/' expected.
7672
tests/cases/conformance/jsx/5.tsx(1,2): error TS17008: JSX element 'a' has no corresponding closing tag.
7773
tests/cases/conformance/jsx/5.tsx(1,5): error TS1005: '</' expected.
7874
tests/cases/conformance/jsx/6.tsx(1,4): error TS17002: Expected corresponding JSX closing tag for 'a'.
@@ -112,18 +108,10 @@ tests/cases/conformance/jsx/9.tsx(1,16): error TS1109: Expression expected.
112108
!!! error TS1109: Expression expected.
113109
~
114110
!!! error TS1109: Expression expected.
115-
==== tests/cases/conformance/jsx/4.tsx (5 errors) ====
111+
==== tests/cases/conformance/jsx/4.tsx (1 errors) ====
116112
<a b=d />;
117113
~
118114
!!! error TS1005: '{' expected.
119-
~
120-
!!! error TS2304: Cannot find name 'd'.
121-
~
122-
!!! error TS1109: Expression expected.
123-
~
124-
!!! error TS1109: Expression expected.
125-
126-
!!! error TS1005: '/' expected.
127115
==== tests/cases/conformance/jsx/5.tsx (2 errors) ====
128116
<a>;
129117
~
@@ -315,12 +303,12 @@ tests/cases/conformance/jsx/9.tsx(1,16): error TS1109: Expression expected.
315303
!!! error TS1005: '</' expected.
316304
==== tests/cases/conformance/jsx/29.tsx (4 errors) ====
317305
<a b=<}>;
318-
~
319-
!!! error TS17008: JSX element 'a' has no corresponding closing tag.
306+
~~~~~
307+
!!! error TS2695: Left side of comma operator is unused and has no side effects.
320308
~
321309
!!! error TS1005: '{' expected.
322310
~
323-
!!! error TS1109: Expression expected.
311+
!!! error TS1003: Identifier expected.
324312

325313

326314
!!! error TS1005: '</' expected.

0 commit comments

Comments
 (0)