-
Notifications
You must be signed in to change notification settings - Fork 469
fix misparsing in/after JSX (#6677) #6686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2612,6 +2612,7 @@ and parseJsxOpeningOrSelfClosingElement ~startPos p = | |
let childrenStartPos = p.Parser.startPos in | ||
Parser.next p; | ||
let childrenEndPos = p.Parser.startPos in | ||
Scanner.popMode p.scanner Jsx; | ||
Parser.expect GreaterThan p; | ||
let loc = mkLoc childrenStartPos childrenEndPos in | ||
makeListExpression loc [] None (* no children *) | ||
|
@@ -2621,8 +2622,6 @@ and parseJsxOpeningOrSelfClosingElement ~startPos p = | |
Parser.next p; | ||
let spread, children = parseJsxChildren p in | ||
let childrenEndPos = p.Parser.startPos in | ||
Scanner.popMode p.scanner Jsx; | ||
Scanner.setJsxMode p.scanner; | ||
let () = | ||
match p.token with | ||
| LessThanSlash -> Parser.next p | ||
|
@@ -2634,12 +2633,14 @@ and parseJsxOpeningOrSelfClosingElement ~startPos p = | |
in | ||
match p.Parser.token with | ||
| (Lident _ | Uident _) when verifyJsxOpeningClosingName p name -> ( | ||
Scanner.popMode p.scanner Jsx; | ||
Parser.expect GreaterThan p; | ||
Comment on lines
+2636
to
2637
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
let loc = mkLoc childrenStartPos childrenEndPos in | ||
match (spread, children) with | ||
| true, child :: _ -> child | ||
| _ -> makeListExpression loc children None) | ||
| token -> ( | ||
Scanner.popMode p.scanner Jsx; | ||
let () = | ||
if Grammar.isStructureItemStart token then | ||
let closing = "</" ^ string_of_pexp_ident name ^ ">" in | ||
|
@@ -2660,6 +2661,7 @@ and parseJsxOpeningOrSelfClosingElement ~startPos p = | |
| true, child :: _ -> child | ||
| _ -> makeListExpression loc children None)) | ||
| token -> | ||
Scanner.popMode p.scanner Jsx; | ||
Parser.err p (Diagnostics.unexpected token p.breadcrumbs); | ||
makeListExpression Location.none [] None | ||
in | ||
|
@@ -2687,7 +2689,6 @@ and parseJsxOpeningOrSelfClosingElement ~startPos p = | |
* jsx-children ::= primary-expr* * => 0 or more | ||
*) | ||
and parseJsx p = | ||
Scanner.popMode p.scanner Jsx; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was not needed |
||
Scanner.setJsxMode p.Parser.scanner; | ||
Parser.leaveBreadcrumb p Grammar.Jsx; | ||
let startPos = p.Parser.startPos in | ||
|
@@ -2700,7 +2701,6 @@ and parseJsx p = | |
parseJsxFragment p | ||
| _ -> parseJsxName p | ||
in | ||
Scanner.popMode p.scanner Jsx; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed either |
||
Parser.eatBreadcrumb p; | ||
{jsxExpr with pexp_attributes = [jsxAttr]} | ||
|
||
|
@@ -2714,9 +2714,10 @@ and parseJsxFragment p = | |
Parser.expect GreaterThan p; | ||
let _spread, children = parseJsxChildren p in | ||
let childrenEndPos = p.Parser.startPos in | ||
if p.token = LessThan then p.token <- Scanner.reconsiderLessThan p.scanner; | ||
Parser.expect LessThanSlash p; | ||
Parser.expect GreaterThan p; | ||
Scanner.popMode p.scanner Jsx; | ||
Parser.expect GreaterThan p; | ||
Comment on lines
+2717
to
+2720
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we first check if the next token is not |
||
let loc = mkLoc childrenStartPos childrenEndPos in | ||
makeListExpression loc children None | ||
|
||
|
@@ -2768,6 +2769,7 @@ and parseJsxProp p = | |
Some (label, attrExpr)) | ||
(* {...props} *) | ||
| Lbrace -> ( | ||
Scanner.popMode p.scanner Jsx; | ||
Parser.next p; | ||
match p.Parser.token with | ||
| DotDotDot -> ( | ||
|
@@ -2786,6 +2788,7 @@ and parseJsxProp p = | |
match p.Parser.token with | ||
| Rbrace -> | ||
Parser.next p; | ||
Scanner.setJsxMode p.scanner; | ||
Some (label, attrExpr) | ||
| _ -> None) | ||
| _ -> None) | ||
|
@@ -2795,6 +2798,7 @@ and parseJsxProps p = | |
parseRegion ~grammar:Grammar.JsxAttribute ~f:parseJsxProp p | ||
|
||
and parseJsxChildren p = | ||
Scanner.popMode p.scanner Jsx; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSX children should not be parsed in JSX mode because they could be any kind of expression between curly braces. |
||
let rec loop p children = | ||
match p.Parser.token with | ||
| Token.Eof | LessThanSlash -> children | ||
|
@@ -2815,21 +2819,23 @@ and parseJsxChildren p = | |
let () = p.token <- token in | ||
children | ||
| token when Grammar.isJsxChildStart token -> | ||
let () = Scanner.popMode p.scanner Jsx in | ||
let child = | ||
parsePrimaryExpr ~operand:(parseAtomicExpr p) ~noCall:true p | ||
in | ||
loop p (child :: children) | ||
| _ -> children | ||
in | ||
match p.Parser.token with | ||
| DotDotDot -> | ||
Parser.next p; | ||
(true, [parsePrimaryExpr ~operand:(parseAtomicExpr p) ~noCall:true p]) | ||
| _ -> | ||
let children = List.rev (loop p []) in | ||
Scanner.popMode p.scanner Jsx; | ||
(false, children) | ||
let spread, children = | ||
match p.Parser.token with | ||
| DotDotDot -> | ||
Parser.next p; | ||
(true, [parsePrimaryExpr ~operand:(parseAtomicExpr p) ~noCall:true p]) | ||
| _ -> | ||
let children = List.rev (loop p []) in | ||
(false, children) | ||
in | ||
Scanner.setJsxMode p.scanner; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. once we're done parsing the children, we reset JSX mode because we have to parse the closing tag. |
||
(spread, children) | ||
|
||
and parseBracedOrRecordExpr p = | ||
let startPos = p.Parser.startPos in | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we expect the end of the JSX so we can pop JSX mode