-
Notifications
You must be signed in to change notification settings - Fork 456
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
Jsx ast #7286
base: master
Are you sure you want to change the base?
Jsx ast #7286
Conversation
|
||
(* | ||
* jsx-fragment ::= | ||
* | <> </> | ||
* | <> jsx-children </> | ||
*) | ||
and parse_jsx_fragment p = | ||
and parse_jsx_fragment start_pos p = |
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.
I prefer ranges to be accurate. The location should start at the opening <
token.
compiler/syntax/src/res_printer.ml
Outdated
Doc.group | ||
(Doc.concat | ||
[ | ||
line_sep; |
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.
@shulhi this part isn't 100% correct.
I'm not familiar enough with this part of the codebase.
let z1 = <> <SectionHeader> {React.string("abc")} </SectionHeader> </>
will be formatted to
let z1 =
<>
<SectionHeader> {React.string("abc")} </SectionHeader>
</>
so I'm missing some indent here. Not sure how that part works.
Would love to hear your thoughts on this.
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.
@nojaf I checked, the indentation is handled here,
rescript/compiler/syntax/src/res_printer.ml
Lines 2085 to 2103 in 9da9b19
let should_indent = | |
match opt_braces with | |
| Some _ -> false | |
| _ -> ( | |
ParsetreeViewer.is_binary_expression expr | |
|| | |
match vb.pvb_expr with | |
| { | |
pexp_attributes = [({Location.txt = "res.ternary"}, _)]; | |
pexp_desc = Pexp_ifthenelse (if_expr, _, _); | |
} -> | |
ParsetreeViewer.is_binary_expression if_expr | |
|| ParsetreeViewer.has_attributes if_expr.pexp_attributes | |
| {pexp_desc = Pexp_newtype _} -> false | |
| {pexp_attributes = [({Location.txt = "res.taggedTemplate"}, _)]} -> | |
false | |
| e -> | |
ParsetreeViewer.has_attributes e.pexp_attributes | |
|| ParsetreeViewer.is_array_access e) |
You might need to handle the case for Pexp_jsx_fragment
here.
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.
Thanks, the old code will instead have matched ParsetreeViewer.has_attributes e.pexp_attributes
. Indentation now works as before.
@@ -1000,7 +1000,7 @@ Path Objects.Rec. | |||
|
|||
Complete src/Completion.res 120:7 | |||
posCursor:[120:7] posNoWhite:[120:6] Found expr:[119:11->123:1] | |||
posCursor:[120:7] posNoWhite:[120:6] Found expr:[120:5->122:5] | |||
posCursor:[120:7] posNoWhite:[120:6] Found expr:[120:5->123:0] |
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.
Changes like this are to be expected as the range of a fragment spans from <>
till end </>
.
posCursor:[9:56] posNoWhite:[9:55] Found expr:[9:13->9:66] | ||
JSX <SectionHeader:[9:13->9:26] > _children:9:26 | ||
posCursor:[9:56] posNoWhite:[9:55] Found expr:__ghost__[9:10->9:67] |
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 ghost expression is a part of the AstHelper.make_list_expression
result.
It is no longer present in the new AST, but it also didn't serve any purpose.
I believe it is okay that this test is slightly different.
In the end the result didn't change.
(* [%id] *) | ||
(* . *) | ||
(* represents <> foo </> , the entire range is stored in the expression , we keep track of >, children and </ *) | ||
| Pexp_jsx_fragment of |
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.
The reason the store the >
and </
token is this edge case:
https://rescript-lang.org/try?version=v12.0.0-alpha.8&module=esmodule&code=DYUwLgBA+hC8ECgCQAeCB6AVBAzmATgIYB2A5iBJuhAHzJIDeASiIQMZgB0e+AlmQAoARAFsQACyEBKAL7IU1LBEIiIASQh9S4yFVpA
If we ever want to restore comments I suppose we need the proper anchors.
|
||
(* | ||
* jsx-fragment ::= | ||
* | <> </> | ||
* | <> jsx-children </> | ||
*) | ||
and parse_jsx_fragment p = | ||
and parse_jsx_fragment start_pos p = | ||
let children_start_pos = p.Parser.start_pos in | ||
Parser.expect GreaterThan p; |
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.
@shulhi I believe the comment capture isn't working here for:
let _ =
<> // foobar
{React.string("m")}
{React.int(4)}
</>
I got nothing for:
dune exec res_parser -- A.res -print comments
leading comments:
comments inside:
trailing comments:
This is a part of #7283.
I'm introducing
Pexp_jsx_fragment
to represent fragment syntax<></>
.I found it insightful to just try it out and see what code changes are necessary.
In short a fragment is now parsed as:
after this change it becomes:
I'll add some comment to relevant changes.