Skip to content

Jsx ast #7286

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

Merged
merged 107 commits into from
Apr 4, 2025
Merged

Jsx ast #7286

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
db7eee6
Intial exploration of JSX ast
nojaf Feb 6, 2025
28aa287
Initial mapping from0
nojaf Feb 24, 2025
b57f2df
Format code
nojaf Feb 24, 2025
32161a3
Merge branch 'master' into jsx-ast
nojaf Mar 3, 2025
d714829
Fix jsx fragment mapping
nojaf Mar 3, 2025
e27b339
Remove fragment
nojaf Mar 3, 2025
92657ee
Update test output
nojaf Mar 3, 2025
441de6e
Introducing Pexp_jsx_unary_element & Pexp_jsx_container_element
nojaf Mar 6, 2025
bf1b411
Refactor fragment transformation.
nojaf Mar 6, 2025
5fe5f12
Initial transform of Pexp_jsx_unary_element in automatic mode
nojaf Mar 7, 2025
9251609
Make props for unary element in automatic mode.
nojaf Mar 7, 2025
1aaa686
Initial custom component unary tag
nojaf Mar 8, 2025
b374212
lowercase container element with children.
nojaf Mar 8, 2025
c91aeda
Uppercase container elements
nojaf Mar 8, 2025
85ccab4
Streamline automatic element calls
nojaf Mar 9, 2025
86c9f5f
lowercase container element in classic mode
nojaf Mar 9, 2025
0dfd5b6
Deal with uppercase tags in classic mode.
nojaf Mar 10, 2025
203ff25
Remove old code
nojaf Mar 10, 2025
a273f2e
Improve recovery of incomplete jsx tags.
nojaf Mar 11, 2025
998edbe
Correct range of incomplete jsx elements
nojaf Mar 11, 2025
fcf2d90
Update semantic tokens
nojaf Mar 11, 2025
162b7a2
Make the closing tag optional for jsx_container_element.
nojaf Mar 11, 2025
9b815a0
Add tighter pattern match for edge case in jsx props completion.
nojaf Mar 11, 2025
9408d06
Update analysis tests for jsx elements ast.
nojaf Mar 11, 2025
cc5beb4
print_jsx_unary_tag
nojaf Mar 11, 2025
02e9fc9
Merge branch 'master' into jsx-ast
nojaf Mar 11, 2025
5e060ea
Rough print_jsx_container_tag
nojaf Mar 12, 2025
e57ab30
Add ml printing
nojaf Mar 12, 2025
b6407d0
Wing the sexp thing
nojaf Mar 17, 2025
e565ca7
First step towards ast mapping
nojaf Mar 18, 2025
84e9cbe
prop punning conversion
nojaf Mar 19, 2025
bbfe816
Map prop value
nojaf Mar 19, 2025
63fc87b
Map prop spreading
nojaf Mar 19, 2025
d6c9fa8
Initial container element mapping.
nojaf Mar 19, 2025
e446ae1
Try support children spreading
nojaf Mar 19, 2025
5fef337
Only print space when there are props
nojaf Mar 19, 2025
251244b
Add space after children.
nojaf Mar 19, 2025
aec0d8e
Restore braces in props and children
nojaf Mar 20, 2025
6a3f4c9
Inline is_jsx_expression and remove old code
nojaf Mar 21, 2025
506dda3
Better indentation of children
nojaf Mar 21, 2025
96839f4
Handle unary tag
shulhi Mar 21, 2025
ff1bb65
Refactor
shulhi Mar 21, 2025
91c283e
WIP: Fix unary tag comments handling
shulhi Mar 22, 2025
2b9b5ce
Fix comments inside prop expression
shulhi Mar 22, 2025
0133f91
Formats
shulhi Mar 22, 2025
aaef8ac
Fix closing tag indentation
shulhi Mar 22, 2025
b863888
Fix closing tag indentation for cases with break vs inline
shulhi Mar 22, 2025
7f8bf01
Handle some edge cases
shulhi Mar 22, 2025
4e0951c
Refactor
shulhi Mar 22, 2025
7843795
Merge pull request #3 from shulhi/shulhi-jsx-ast
nojaf Mar 22, 2025
9e68c1a
WIP: Handle punning
shulhi Mar 22, 2025
53616a7
Fix optional printing with braces
shulhi Mar 22, 2025
25e20b8
WIP: Handle comments attachment in a different way
shulhi Mar 23, 2025
ad72bf2
Handle empty props
shulhi Mar 23, 2025
7ba4996
Handle props spread
shulhi Mar 24, 2025
f09cd70
Fix optional with punning
shulhi Mar 24, 2025
aa1d978
Merge pull request #4 from shulhi/shulhi-jsx-ast-2
nojaf Mar 24, 2025
0ee7601
Indent props if they don't fit on one line.
nojaf Mar 24, 2025
bd45146
Fix indentation of children when props are multiline.
nojaf Mar 24, 2025
2d50ff8
Refactor to Pexp_jsx_element
nojaf Mar 26, 2025
fcabcff
Merge branch 'master' into jsx-ast
nojaf Mar 26, 2025
ddcdaa2
Don't always append make when uppercase component
nojaf Mar 26, 2025
52ecf03
Use Doc.line for child spreading
nojaf Mar 26, 2025
cd9059f
Exotic prop name
nojaf Mar 26, 2025
2e9fb66
Don't create record if only spreading prop, use that expression instead.
nojaf Mar 26, 2025
37755ae
Key prop can be optional
nojaf Mar 27, 2025
3a434c9
Keep comment after opening greater than
nojaf Mar 27, 2025
6cf1e71
Print prop value with comments
nojaf Mar 27, 2025
ae9b428
Update generic jsx completion tests
nojaf Mar 27, 2025
f3b96df
Revert isJsxComponent
nojaf Mar 27, 2025
b1f8dfe
Assign comment to the opening element tag name.
nojaf Mar 29, 2025
64001e2
Attach comments to closing > and closing tag
nojaf Mar 30, 2025
8148cf5
Extract helpers to parsetree_viewer
nojaf Mar 30, 2025
cfc4669
setup to walk jsx props
nojaf Mar 30, 2025
2946ea0
Correct range for prop spreading
nojaf Mar 30, 2025
4fcc3ce
Complete walk_jsx_prop
nojaf Mar 30, 2025
161b12c
Finish comment assignment for container elements
nojaf Mar 30, 2025
cd98c9f
Revisit comment attachment for unary tags
nojaf Mar 30, 2025
67da647
Improve behaviour for comments in unary tag
nojaf Mar 31, 2025
86fea57
Keep location as is for prop spreading
nojaf Mar 31, 2025
f10bc55
print leading comments of closing unary token
nojaf Mar 31, 2025
b40c019
Attach comments between empty container tag
nojaf Mar 31, 2025
28497d2
comments between opening and closing tag
nojaf Mar 31, 2025
e7bf344
Correct opening_greater_than_doc
nojaf Mar 31, 2025
b8d609e
Deal with container element comment edge cases.
nojaf Mar 31, 2025
6d15369
Ensure comments inside braced property values are correct.
nojaf Mar 31, 2025
88534ba
More accurate locations
nojaf Mar 31, 2025
210fa87
Update syntax roundtrip test files
nojaf Mar 31, 2025
8373814
Update mapping test results
nojaf Mar 31, 2025
d92d7c5
comment after prop name
nojaf Mar 31, 2025
d9b59dd
For them older camls
nojaf Mar 31, 2025
4e160f3
More older caml stuff
nojaf Mar 31, 2025
93eb5af
Remove unreached code in typecore
nojaf Apr 1, 2025
6152b83
Remove leftover comments
nojaf Apr 1, 2025
2126fc9
Preserve lines between jsx elements
nojaf Apr 1, 2025
645b244
Preserve newline between jsx children, streamline jsx fragment childr…
nojaf Apr 1, 2025
a0c0d0d
Update analysis expected
nojaf Apr 1, 2025
f1da36f
Another location change in expected
nojaf Apr 1, 2025
80c307a
One more update?
nojaf Apr 1, 2025
5e5dabd
Merge branch 'master' into jsx-ast
nojaf Apr 1, 2025
091f1a3
Merge branch 'master' into jsx-ast
nojaf Apr 2, 2025
11ced99
Remove leftover comments
nojaf Apr 2, 2025
0c825d5
Remove obsolete comment
nojaf Apr 2, 2025
8ff481d
Ensure fragment starts after arrow
nojaf Apr 3, 2025
8723426
Add changelog entry
nojaf Apr 3, 2025
a1e4ca9
Merge branch 'master' into jsx-ast
nojaf Apr 4, 2025
e1ad251
Add missing Pexp_await
nojaf Apr 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revisit comment attachment for unary tags
  • Loading branch information
nojaf committed Mar 30, 2025
commit cd98c9f5f0ff0d545bc286841f4e35f4d37141f5
72 changes: 44 additions & 28 deletions compiler/syntax/src/res_comments_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ let partition_adjacent_trailing loc1 comments =
* This function is useful for precisely attaching comments between specific tokens
* in constructs like JSX props, function arguments, and other multi-token expressions.
*)
let partition_adjacent_trailing_before_next_token_on_same_line (loc : Warnings.loc)
(next_token : Warnings.loc) (comments : Comment.t list) :
Comment.t list * Comment.t list =
let partition_adjacent_trailing_before_next_token_on_same_line
(loc : Warnings.loc) (next_token : Warnings.loc) (comments : Comment.t list)
: Comment.t list * Comment.t list =
let open Location in
let open Lexing in
let rec loop after_loc comments =
Expand Down Expand Up @@ -1594,28 +1594,42 @@ and walk_expression expr t comments =
walk_list xs t rest
| Pexp_jsx_element
(Jsx_unary_element
{jsx_unary_element_tag_name = tag; jsx_unary_element_props = []}) ->
let _, _, trailing = partition_by_loc comments tag.loc in
let after_expr, _ = partition_adjacent_trailing tag.loc trailing in
attach t.trailing tag.loc after_expr
| Pexp_jsx_element
(Jsx_unary_element
{jsx_unary_element_tag_name = tag; jsx_unary_element_props = props}) ->
let _leading, inside, trailing = partition_by_loc comments tag.loc in
walk_list
(props
|> List.filter_map (fun prop ->
match prop with
| Parsetree.JSXPropPunning (_, {loc}) ->
Some (ExprArgument {expr; loc})
| Parsetree.JSXPropValue ({loc}, _, expr) ->
let loc = {loc with loc_end = expr.pexp_loc.loc_end} in
Some (ExprArgument {expr; loc})
| Parsetree.JSXPropSpreading (loc, expr) ->
let loc = {loc with loc_end = expr.pexp_loc.loc_end} in
Some (ExprArgument {expr; loc})))
t trailing;
walk_expression expr t inside
{
jsx_unary_element_tag_name = tag_name;
jsx_unary_element_props = props;
}) -> (
let closing_token_loc =
ParsetreeViewer.unary_element_closing_token expr.pexp_loc
in

let after_opening_tag_name, rest =
(* Either the first prop or the closing /> token *)
let next_token =
match props with
| [] -> closing_token_loc
| head :: _ -> ParsetreeViewer.get_jsx_prop_loc head
in
partition_adjacent_trailing_before_next_token_on_same_line tag_name.loc
next_token comments
in

(* Only attach comments to the element name if they are on the same line *)
attach t.trailing tag_name.loc after_opening_tag_name;
match props with
| [] ->
let before_closing_token, _rest =
partition_leading_trailing rest closing_token_loc
in
(* attach comments to the closing /> token *)
attach t.leading closing_token_loc before_closing_token
(* the _rest comments are going to be attached after the entire expression,
dealt with in the parent node. *)
| props ->
let comments_for_props, _rest =
partition_leading_trailing rest closing_token_loc
in
let prop_nodes = List.map (fun prop -> JsxProp prop) props in
walk_list prop_nodes t comments_for_props)
| Pexp_jsx_element
(Jsx_container_element
{
Expand All @@ -1639,8 +1653,8 @@ and walk_expression expr t comments =
| [] -> opening_greater_than_loc
| head :: _ -> ParsetreeViewer.get_jsx_prop_loc head
in
partition_adjacent_trailing_before_next_token_on_same_line tag_name_start.loc
next_token comments
partition_adjacent_trailing_before_next_token_on_same_line
tag_name_start.loc next_token comments
in
(* Only attach comments to the element name if they are on the same line *)
attach t.trailing tag_name_start.loc after_opening_tag_name;
Expand Down Expand Up @@ -1672,7 +1686,9 @@ and walk_expression expr t comments =
match closing_tag with
| None -> (rest, [])
| Some closing_tag ->
let closing_tag_loc = ParsetreeViewer.closing_tag_loc closing_tag in
let closing_tag_loc =
ParsetreeViewer.container_element_closing_tag_loc closing_tag
in
partition_leading_trailing rest closing_tag_loc
in

Expand Down
15 changes: 14 additions & 1 deletion compiler/syntax/src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,22 @@ let get_jsx_prop_loc = function
{name.loc with loc_end = value.pexp_loc.loc_end}
| Parsetree.JSXPropSpreading (loc, _) -> loc

let closing_tag_loc (tag : Parsetree.jsx_closing_container_tag) =
let container_element_closing_tag_loc
(tag : Parsetree.jsx_closing_container_tag) =
{
tag.jsx_closing_container_tag_name.loc with
loc_start = tag.jsx_closing_container_tag_start;
loc_end = tag.jsx_closing_container_tag_end;
}

(** returns the location of the /> token in a unary element *)
let unary_element_closing_token (expression_loc : Warnings.loc) =
{
expression_loc with
loc_start =
{
expression_loc.loc_end with
pos_cnum = expression_loc.loc_end.pos_cnum - 2;
pos_bol = expression_loc.loc_end.pos_bol - 2;
};
}
5 changes: 4 additions & 1 deletion compiler/syntax/src/res_parsetree_viewer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,7 @@ val is_tuple_array : Parsetree.expression -> bool

val get_jsx_prop_loc : Parsetree.jsx_prop -> Warnings.loc

val closing_tag_loc : Parsetree.jsx_closing_container_tag -> Warnings.loc
val container_element_closing_tag_loc :
Parsetree.jsx_closing_container_tag -> Warnings.loc

val unary_element_closing_token : Warnings.loc -> Warnings.loc
Loading