Skip to content

Commit 6a3f4c9

Browse files
committed
Inline is_jsx_expression and remove old code
1 parent aec0d8e commit 6a3f4c9

File tree

5 files changed

+56
-397
lines changed

5 files changed

+56
-397
lines changed

compiler/syntax/src/res_comments_table.ml

Lines changed: 33 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,67 +1397,21 @@ and walk_expression expr t comments =
13971397
walk_expression call_expr t inside;
13981398
after)
13991399
in
1400-
if ParsetreeViewer.is_jsx_expression expr then (
1401-
let props =
1402-
arguments
1403-
|> List.filter (fun (label, _) ->
1404-
match label with
1405-
| Asttypes.Labelled {txt = "children"} -> false
1406-
| Asttypes.Nolabel -> false
1407-
| _ -> true)
1408-
in
1409-
let maybe_children =
1410-
arguments
1411-
|> List.find_opt (fun (label, _) ->
1412-
match label with
1413-
| Asttypes.Labelled {txt = "children"} -> true
1414-
| _ -> false)
1415-
in
1416-
match maybe_children with
1417-
(* There is no need to deal with this situation as the children cannot be NONE *)
1418-
| None -> ()
1419-
| Some (_, children) ->
1420-
let leading, inside, _ = partition_by_loc after children.pexp_loc in
1421-
if props = [] then
1422-
(* All comments inside a tag are trailing comments of the tag if there are no props
1423-
<A
1424-
// comment
1425-
// comment
1426-
/>
1427-
*)
1428-
let after_expr, _ =
1429-
partition_adjacent_trailing call_expr.pexp_loc after
1430-
in
1431-
attach t.trailing call_expr.pexp_loc after_expr
1432-
else
1433-
walk_list
1434-
(props
1435-
|> List.map (fun (lbl, expr) ->
1436-
let loc =
1437-
match lbl with
1438-
| Asttypes.Labelled {loc} | Optional {loc} ->
1439-
{loc with loc_end = expr.Parsetree.pexp_loc.loc_end}
1440-
| _ -> expr.pexp_loc
1441-
in
1442-
ExprArgument {expr; loc}))
1443-
t leading;
1444-
walk_expression children t inside)
1445-
else
1446-
let after_expr, rest =
1447-
partition_adjacent_trailing call_expr.pexp_loc after
1448-
in
1449-
attach t.trailing call_expr.pexp_loc after_expr;
1450-
walk_list
1451-
(arguments
1452-
|> List.map (fun (lbl, expr) ->
1453-
let loc =
1454-
match lbl with
1455-
| Asttypes.Labelled {loc} | Optional {loc} ->
1456-
{loc with loc_end = expr.Parsetree.pexp_loc.loc_end}
1457-
| _ -> expr.pexp_loc
1458-
in
1459-
ExprArgument {expr; loc}))
1460-
t rest
1400+
let after_expr, rest =
1401+
partition_adjacent_trailing call_expr.pexp_loc after
1402+
in
1403+
attach t.trailing call_expr.pexp_loc after_expr;
1404+
walk_list
1405+
(arguments
1406+
|> List.map (fun (lbl, expr) ->
1407+
let loc =
1408+
match lbl with
1409+
| Asttypes.Labelled {loc} | Optional {loc} ->
1410+
{loc with loc_end = expr.Parsetree.pexp_loc.loc_end}
1411+
| _ -> expr.pexp_loc
1412+
in
1413+
ExprArgument {expr; loc}))
1414+
t rest
14611415
| Pexp_fun _ | Pexp_newtype _ -> (
14621416
let _, parameters, return_expr = fun_expr expr in
14631417
let comments =
@@ -1518,8 +1472,24 @@ and walk_expression expr t comments =
15181472
in
15191473
let xs = exprs |> List.map (fun e -> Expression e) in
15201474
walk_list xs t rest
1521-
| Pexp_jsx_unary_element _ -> failwith "Pexp_jsx_unary_element 3"
1522-
| Pexp_jsx_container_element _ -> failwith "Pexp_jsx_container_element 3"
1475+
| Pexp_jsx_unary_element _ ->
1476+
(* TODO: save me shulhi, not sure what needs to be done here *)
1477+
()
1478+
| Pexp_jsx_container_element
1479+
{
1480+
jsx_container_element_opening_tag_end = opening_greater_than;
1481+
jsx_container_element_children = children;
1482+
} ->
1483+
let opening_token = {expr.pexp_loc with loc_end = opening_greater_than} in
1484+
let on_same_line, rest = partition_by_on_same_line opening_token comments in
1485+
attach t.trailing opening_token on_same_line;
1486+
let exprs =
1487+
match children with
1488+
| Parsetree.JSXChildrenSpreading e -> [e]
1489+
| Parsetree.JSXChildrenItems xs -> xs
1490+
in
1491+
let xs = exprs |> List.map (fun e -> Expression e) in
1492+
walk_list xs t rest
15231493
| Pexp_send _ -> ()
15241494

15251495
and walk_expr_parameter (_attrs, _argLbl, expr_opt, pattern) t comments =

compiler/syntax/src/res_parens.ml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ let structure_expr expr =
6666
| Some ({Location.loc = braces_loc}, _) -> Braced braces_loc
6767
| None -> (
6868
match expr with
69-
| _
70-
when ParsetreeViewer.has_attributes expr.pexp_attributes
71-
&& not (ParsetreeViewer.is_jsx_expression expr) ->
69+
| {
70+
pexp_desc =
71+
( Pexp_jsx_fragment _ | Pexp_jsx_unary_element _
72+
| Pexp_jsx_container_element _ );
73+
} ->
74+
Nothing
75+
| _ when ParsetreeViewer.has_attributes expr.pexp_attributes ->
7276
Parenthesized
7377
| {
7478
Parsetree.pexp_desc =
@@ -396,7 +400,12 @@ let jsx_child_expr expr =
396400
pexp_attributes = [];
397401
} ->
398402
Nothing
399-
| expr when ParsetreeViewer.is_jsx_expression expr -> Nothing
403+
| {
404+
pexp_desc =
405+
( Pexp_jsx_fragment _ | Pexp_jsx_unary_element _
406+
| Pexp_jsx_container_element _ );
407+
} ->
408+
Nothing
400409
| _ -> Parenthesized))
401410

402411
let binary_expr expr =

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,6 @@ let filter_fragile_match_attributes attrs =
484484
| _ -> true)
485485
attrs
486486

487-
let is_jsx_expression expr =
488-
match expr.pexp_desc with
489-
| Pexp_jsx_fragment _ | Pexp_jsx_unary_element _
490-
| Pexp_jsx_container_element _ ->
491-
true
492-
| _ -> false
493-
494487
let should_indent_binary_expr expr =
495488
let same_precedence_sub_expression operator sub_expression =
496489
match sub_expression with

compiler/syntax/src/res_parsetree_viewer.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ val filter_ternary_attributes : Parsetree.attributes -> Parsetree.attributes
8787
val filter_fragile_match_attributes :
8888
Parsetree.attributes -> Parsetree.attributes
8989

90-
val is_jsx_expression : Parsetree.expression -> bool
91-
9290
val should_indent_binary_expr : Parsetree.expression -> bool
9391
val should_inline_rhs_binary_expr : Parsetree.expression -> bool
9492
val has_printable_attributes : Parsetree.attributes -> bool

0 commit comments

Comments
 (0)