Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a052169

Browse files
Throw a recoverable error
1 parent 11433fa commit a052169

7 files changed

Lines changed: 358 additions & 7 deletions

File tree

packages/babel-parser/src/plugins/jsx/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const JsxErrors = Object.freeze({
2424
"JSX attributes must only be assigned a non-empty expression",
2525
MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>",
2626
MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>",
27+
UnexpectedSequenceExpression:
28+
"Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
2729
UnsupportedJsxValue:
2830
"JSX value should be either an expression or a quoted JSX text",
2931
UnterminatedJsxContent: "Unterminated JSX contents",
@@ -344,9 +346,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
344346
if (this.match(tt.braceR)) {
345347
node.expression = this.jsxParseEmptyExpression();
346348
} else {
347-
node.expression = process.env.BABEL_8_BREAKING
348-
? this.parseMaybeAssign()
349-
: this.parseExpression();
349+
const expression = this.parseExpression();
350+
351+
if (process.env.BABEL_8_BREAKING) {
352+
if (
353+
expression.type === "SequenceExpression" &&
354+
!expression.extra?.parenthesized
355+
) {
356+
this.raise(
357+
expression.expressions[1].start,
358+
JsxErrors.UnexpectedSequenceExpression,
359+
);
360+
}
361+
}
362+
363+
node.expression = expression;
350364
}
351365
this.expect(tt.braceR);
352366

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"BABEL_8_BREAKING": true,
3-
"throws": "Unexpected token, expected \"}\" (1:28)"
2+
"BABEL_8_BREAKING": true
43
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}},
4+
"errors": [
5+
"SyntaxError: Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)? (1:30)"
6+
],
7+
"program": {
8+
"type": "Program",
9+
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}},
10+
"sourceType": "script",
11+
"interpreter": null,
12+
"body": [
13+
{
14+
"type": "ExpressionStatement",
15+
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}},
16+
"expression": {
17+
"type": "JSXElement",
18+
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}},
19+
"openingElement": {
20+
"type": "JSXOpeningElement",
21+
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}},
22+
"name": {
23+
"type": "JSXIdentifier",
24+
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
25+
"name": "div"
26+
},
27+
"attributes": [
28+
{
29+
"type": "JSXAttribute",
30+
"start":5,"end":52,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":52}},
31+
"name": {
32+
"type": "JSXIdentifier",
33+
"start":5,"end":8,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":8}},
34+
"name": "key"
35+
},
36+
"value": {
37+
"type": "JSXExpressionContainer",
38+
"start":9,"end":52,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":52}},
39+
"expression": {
40+
"type": "SequenceExpression",
41+
"start":10,"end":51,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":51}},
42+
"expressions": [
43+
{
44+
"type": "CallExpression",
45+
"start":10,"end":28,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":28}},
46+
"callee": {
47+
"type": "MemberExpression",
48+
"start":10,"end":21,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":21}},
49+
"object": {
50+
"type": "Identifier",
51+
"start":10,"end":17,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":17},"identifierName":"console"},
52+
"name": "console"
53+
},
54+
"computed": false,
55+
"property": {
56+
"type": "Identifier",
57+
"start":18,"end":21,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":21},"identifierName":"log"},
58+
"name": "log"
59+
}
60+
},
61+
"arguments": [
62+
{
63+
"type": "StringLiteral",
64+
"start":22,"end":27,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":27}},
65+
"extra": {
66+
"rawValue": "foo",
67+
"raw": "'foo'"
68+
},
69+
"value": "foo"
70+
}
71+
]
72+
},
73+
{
74+
"type": "CallExpression",
75+
"start":30,"end":51,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":51}},
76+
"callee": {
77+
"type": "MemberExpression",
78+
"start":30,"end":44,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":44}},
79+
"object": {
80+
"type": "Identifier",
81+
"start":30,"end":34,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":34},"identifierName":"JSON"},
82+
"name": "JSON"
83+
},
84+
"computed": false,
85+
"property": {
86+
"type": "Identifier",
87+
"start":35,"end":44,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":44},"identifierName":"stringify"},
88+
"name": "stringify"
89+
}
90+
},
91+
"arguments": [
92+
{
93+
"type": "Identifier",
94+
"start":45,"end":50,"loc":{"start":{"line":1,"column":45},"end":{"line":1,"column":50},"identifierName":"props"},
95+
"name": "props"
96+
}
97+
]
98+
}
99+
]
100+
}
101+
}
102+
}
103+
],
104+
"selfClosing": true
105+
},
106+
"closingElement": null,
107+
"children": []
108+
}
109+
}
110+
],
111+
"directives": []
112+
}
113+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>{(console.log('foo'), JSON.stringify(props))}</div>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}},
7+
"sourceType": "script",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "ExpressionStatement",
12+
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}},
13+
"expression": {
14+
"type": "JSXElement",
15+
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}},
16+
"openingElement": {
17+
"type": "JSXOpeningElement",
18+
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
19+
"name": {
20+
"type": "JSXIdentifier",
21+
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
22+
"name": "div"
23+
},
24+
"attributes": [],
25+
"selfClosing": false
26+
},
27+
"closingElement": {
28+
"type": "JSXClosingElement",
29+
"start":50,"end":56,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":56}},
30+
"name": {
31+
"type": "JSXIdentifier",
32+
"start":52,"end":55,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":55}},
33+
"name": "div"
34+
}
35+
},
36+
"children": [
37+
{
38+
"type": "JSXExpressionContainer",
39+
"start":5,"end":50,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":50}},
40+
"expression": {
41+
"type": "SequenceExpression",
42+
"start":7,"end":48,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":48}},
43+
"extra": {
44+
"parenthesized": true,
45+
"parenStart": 6
46+
},
47+
"expressions": [
48+
{
49+
"type": "CallExpression",
50+
"start":7,"end":25,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":25}},
51+
"callee": {
52+
"type": "MemberExpression",
53+
"start":7,"end":18,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":18}},
54+
"object": {
55+
"type": "Identifier",
56+
"start":7,"end":14,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":14},"identifierName":"console"},
57+
"name": "console"
58+
},
59+
"computed": false,
60+
"property": {
61+
"type": "Identifier",
62+
"start":15,"end":18,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":18},"identifierName":"log"},
63+
"name": "log"
64+
}
65+
},
66+
"arguments": [
67+
{
68+
"type": "StringLiteral",
69+
"start":19,"end":24,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":24}},
70+
"extra": {
71+
"rawValue": "foo",
72+
"raw": "'foo'"
73+
},
74+
"value": "foo"
75+
}
76+
]
77+
},
78+
{
79+
"type": "CallExpression",
80+
"start":27,"end":48,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":48}},
81+
"callee": {
82+
"type": "MemberExpression",
83+
"start":27,"end":41,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":41}},
84+
"object": {
85+
"type": "Identifier",
86+
"start":27,"end":31,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":31},"identifierName":"JSON"},
87+
"name": "JSON"
88+
},
89+
"computed": false,
90+
"property": {
91+
"type": "Identifier",
92+
"start":32,"end":41,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":41},"identifierName":"stringify"},
93+
"name": "stringify"
94+
}
95+
},
96+
"arguments": [
97+
{
98+
"type": "Identifier",
99+
"start":42,"end":47,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":47},"identifierName":"props"},
100+
"name": "props"
101+
}
102+
]
103+
}
104+
]
105+
}
106+
}
107+
]
108+
}
109+
}
110+
],
111+
"directives": []
112+
}
113+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"BABEL_8_BREAKING": true,
3-
"throws": "Unexpected token, expected \"}\" (1:24)"
2+
"BABEL_8_BREAKING": true
43
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":54}},
4+
"errors": [
5+
"SyntaxError: Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)? (1:26)"
6+
],
7+
"program": {
8+
"type": "Program",
9+
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":54}},
10+
"sourceType": "script",
11+
"interpreter": null,
12+
"body": [
13+
{
14+
"type": "ExpressionStatement",
15+
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":54}},
16+
"expression": {
17+
"type": "JSXElement",
18+
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":54}},
19+
"openingElement": {
20+
"type": "JSXOpeningElement",
21+
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
22+
"name": {
23+
"type": "JSXIdentifier",
24+
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
25+
"name": "div"
26+
},
27+
"attributes": [],
28+
"selfClosing": false
29+
},
30+
"closingElement": {
31+
"type": "JSXClosingElement",
32+
"start":48,"end":54,"loc":{"start":{"line":1,"column":48},"end":{"line":1,"column":54}},
33+
"name": {
34+
"type": "JSXIdentifier",
35+
"start":50,"end":53,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":53}},
36+
"name": "div"
37+
}
38+
},
39+
"children": [
40+
{
41+
"type": "JSXExpressionContainer",
42+
"start":5,"end":48,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":48}},
43+
"expression": {
44+
"type": "SequenceExpression",
45+
"start":6,"end":47,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":47}},
46+
"expressions": [
47+
{
48+
"type": "CallExpression",
49+
"start":6,"end":24,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":24}},
50+
"callee": {
51+
"type": "MemberExpression",
52+
"start":6,"end":17,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":17}},
53+
"object": {
54+
"type": "Identifier",
55+
"start":6,"end":13,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":13},"identifierName":"console"},
56+
"name": "console"
57+
},
58+
"computed": false,
59+
"property": {
60+
"type": "Identifier",
61+
"start":14,"end":17,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":17},"identifierName":"log"},
62+
"name": "log"
63+
}
64+
},
65+
"arguments": [
66+
{
67+
"type": "StringLiteral",
68+
"start":18,"end":23,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":23}},
69+
"extra": {
70+
"rawValue": "foo",
71+
"raw": "'foo'"
72+
},
73+
"value": "foo"
74+
}
75+
]
76+
},
77+
{
78+
"type": "CallExpression",
79+
"start":26,"end":47,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":47}},
80+
"callee": {
81+
"type": "MemberExpression",
82+
"start":26,"end":40,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":40}},
83+
"object": {
84+
"type": "Identifier",
85+
"start":26,"end":30,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":30},"identifierName":"JSON"},
86+
"name": "JSON"
87+
},
88+
"computed": false,
89+
"property": {
90+
"type": "Identifier",
91+
"start":31,"end":40,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":40},"identifierName":"stringify"},
92+
"name": "stringify"
93+
}
94+
},
95+
"arguments": [
96+
{
97+
"type": "Identifier",
98+
"start":41,"end":46,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":46},"identifierName":"props"},
99+
"name": "props"
100+
}
101+
]
102+
}
103+
]
104+
}
105+
}
106+
]
107+
}
108+
}
109+
],
110+
"directives": []
111+
}
112+
}

0 commit comments

Comments
 (0)