-
Notifications
You must be signed in to change notification settings - Fork 471
Refactor jsx mode in parser #7751
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 11 commits
2699aa7
b60bcae
0e8b542
7d6a563
b0f4806
50d46ef
849d19f
28cfceb
bd46a3e
8bb4e41
9d8641e
69054c5
452301a
5d05a08
7665a05
0ea79fd
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -715,10 +715,42 @@ let parse_module_long_ident_tail ~lowercase p start_pos ident = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
loop p ident | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(* jsx allows for `-` token in the name, we need to combine some tokens into a single ident *) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let parse_jsx_ident (p : Parser.t) : unit = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(* check if the next tokens are minus and ident, if so, add them to the buffer *) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let rec visit buffer = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match p.Parser.token with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minus -> ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Parser.next p; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match p.Parser.token with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Lident txt | Uident txt -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Buffer.add_char buffer '-'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Buffer.add_string buffer txt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if Scanner.peekMinus p.scanner then visit buffer else buffer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ -> buffer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ -> buffer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match p.Parser.token with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Lident txt when Scanner.peekMinus p.scanner -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let buffer = Buffer.create (String.length txt) in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Buffer.add_string buffer txt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Parser.next p; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let name = visit buffer |> Buffer.contents in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let token = Token.Lident name in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p.token <- token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Uident txt when Scanner.peekMinus p.scanner -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let buffer = Buffer.create (String.length txt) in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Buffer.add_string buffer txt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Parser.next p; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let name = visit buffer |> Buffer.contents in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let token = Token.Uident name in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p.token <- token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
p.token <- token | |
| Uident txt when Scanner.peekMinus p.scanner -> | |
let buffer = Buffer.create (String.length txt) in | |
Buffer.add_string buffer txt; | |
Parser.next p; | |
let name = visit buffer |> Buffer.contents in | |
let token = Token.Uident name in | |
p.token <- token | |
set_token p token | |
| Uident txt when Scanner.peekMinus p.scanner -> | |
let buffer = Buffer.create (String.length txt) in | |
Buffer.add_string buffer txt; | |
Parser.next p; | |
let name = visit buffer |> Buffer.contents in | |
let token = Token.Uident name in | |
set_token p token |
Copilot uses AI. Check for mistakes.
Outdated
Copilot
AI
Aug 5, 2025
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.
[nitpick] Direct mutation of parser state (p.token <-
) breaks encapsulation and makes the code harder to reason about. Consider using a proper parser method or returning the modified token instead of mutating parser state directly.
p.token <- token | |
| Uident txt when Scanner.peekMinus p.scanner -> | |
let buffer = Buffer.create (String.length txt) in | |
Buffer.add_string buffer txt; | |
Parser.next p; | |
let name = visit buffer |> Buffer.contents in | |
let token = Token.Uident name in | |
p.token <- token | |
set_token p token | |
| Uident txt when Scanner.peekMinus p.scanner -> | |
let buffer = Buffer.create (String.length txt) in | |
Buffer.add_string buffer txt; | |
Parser.next p; | |
let name = visit buffer |> Buffer.contents in | |
let token = Token.Uident name in | |
set_token p token |
Copilot uses AI. Check for mistakes.
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.
This is rather confusing when you are in a nested jsx scenario:
<div>
<p>
{foo}
</p>
</div>
Popping Jsx
from p
requires the pop of div
also to happen to get out of Jsx
mode.
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.
This is what bother me a bit, there is LessThanSlash
above, yet we still need to do the reconsider_less_than
call.
Uh oh!
There was an error while loading. Please reload this page.