Skip to content

Commit

Permalink
Fix parsing of < after object literals with the jsx plugin (#14557)
Browse files Browse the repository at this point in the history
fix: manipulate state.canStartJSXElement before eating braceR
  • Loading branch information
JLHwung committed May 17, 2022
1 parent 4266fef commit 9156230
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 19 deletions.
9 changes: 5 additions & 4 deletions packages/babel-parser/src/plugins/jsx/index.js
Expand Up @@ -353,6 +353,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.next(); // ellipsis
node.expression = this.parseExpression();
this.setContext(tc.j_oTag);
this.state.canStartJSXElement = true;
this.expect(tt.braceR);

return this.finishNode(node, "JSXSpreadChild");
Expand Down Expand Up @@ -383,6 +384,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.expression = expression;
}
this.setContext(previousContext);
this.state.canStartJSXElement = true;
this.expect(tt.braceR);

return this.finishNode(node, "JSXExpressionContainer");
Expand All @@ -398,6 +400,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.expect(tt.ellipsis);
node.argument = this.parseMaybeAssignAllowIn();
this.setContext(tc.j_oTag);
this.state.canStartJSXElement = true;
this.expect(tt.braceR);
return this.finishNode(node, "JSXSpreadAttribute");
}
Expand All @@ -413,8 +416,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startLoc: Position,
): N.JSXOpeningElement {
const node = this.startNodeAt(startPos, startLoc);
if (this.match(tt.jsxTagEnd)) {
this.expect(tt.jsxTagEnd);
if (this.eat(tt.jsxTagEnd)) {
return this.finishNode(node, "JSXOpeningFragment");
}
node.name = this.jsxParseElementName();
Expand All @@ -441,8 +443,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startLoc: Position,
): N.JSXClosingElement {
const node = this.startNodeAt(startPos, startLoc);
if (this.match(tt.jsxTagEnd)) {
this.expect(tt.jsxTagEnd);
if (this.eat(tt.jsxTagEnd)) {
return this.finishNode(node, "JSXClosingFragment");
}
node.name = this.jsxParseElementName();
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/tokenizer/types.js
Expand Up @@ -142,7 +142,7 @@ export const tt: { [name: string]: TokenType } = {
braceL: createToken("{", { beforeExpr, startsExpr }),
braceBarL: createToken("{|", { beforeExpr, startsExpr }),
braceHashL: createToken("#{", { beforeExpr, startsExpr }),
braceR: createToken("}", { beforeExpr }),
braceR: createToken("}"),
braceBarR: createToken("|}"),
parenL: createToken("(", { beforeExpr, startsExpr }),
parenR: createToken(")"),
Expand Down
Expand Up @@ -124,7 +124,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
Expand Up @@ -105,7 +105,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
Expand Up @@ -234,7 +234,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
Expand Up @@ -226,7 +226,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
@@ -0,0 +1 @@
({}<x)
@@ -0,0 +1,3 @@
{
"plugins": ["jsx"]
}
@@ -0,0 +1,36 @@
{
"type": "File",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}},
"program": {
"type": "Program",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}},
"expression": {
"type": "BinaryExpression",
"start":1,"end":5,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":5,"index":5}},
"left": {
"type": "ObjectExpression",
"start":1,"end":3,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":3,"index":3}},
"properties": []
},
"operator": "<",
"right": {
"type": "Identifier",
"start":4,"end":5,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":5,"index":5},"identifierName":"x"},
"name": "x"
},
"extra": {
"parenthesized": true,
"parenStart": 0
}
}
}
],
"directives": []
}
}
Expand Up @@ -37,7 +37,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
Expand Up @@ -37,7 +37,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
Expand Up @@ -145,7 +145,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down Expand Up @@ -207,7 +207,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
Expand Up @@ -212,7 +212,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down Expand Up @@ -274,7 +274,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
Expand Up @@ -212,7 +212,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down Expand Up @@ -274,7 +274,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
Expand Up @@ -260,7 +260,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down
Expand Up @@ -254,7 +254,7 @@
{
"type": {
"label": "}",
"beforeExpr": true,
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
Expand Down

0 comments on commit 9156230

Please sign in to comment.