@@ -1105,14 +1105,22 @@ and print_indented_list (f : P.t) (parent_expr_level : int) (cxt : cxt)
11051105and print_jsx cxt ?(spread_props : J.expression option )
11061106 ?(key : J.expression option ) ~(level : int ) f (fnName : string )
11071107 (tag : J.expression ) (fields : (string * J.expression) list ) : cxt =
1108+ (* TODO: make fragment detection respect custom JSX runtime modules instead of
1109+ assuming "JsxRuntime". *)
1110+ let is_fragment =
1111+ match tag.expression_desc with
1112+ | J. Var (J. Qualified ({id = {name = "JsxRuntime" } } , Some "Fragment" )) ->
1113+ true
1114+ | _ -> false
1115+ in
11081116 let print_tag cxt =
11091117 match tag.expression_desc with
11101118 (* "div" or any other primitive tag *)
11111119 | J. Str {txt} ->
11121120 P. string f txt;
11131121 cxt
11141122 (* fragment *)
1115- | J. Var ( J. Qualified ( { id = { name = "JsxRuntime" } } , Some "Fragment" )) -> cxt
1123+ | _ when is_fragment -> cxt
11161124 (* A user defined component or external component *)
11171125 | _ -> expression ~level cxt f tag
11181126 in
@@ -1129,6 +1137,11 @@ and print_jsx cxt ?(spread_props : J.expression option)
11291137 else Some [e]
11301138 else None )
11311139 fields
1140+ (* For fragments without children we normalize to an empty list so they
1141+ print as <></> instead of </> which is invalid JSX. *)
1142+ |> function
1143+ | None when is_fragment -> Some []
1144+ | other -> other
11321145 in
11331146 let print_props cxt props =
11341147 (* If a key is present, should be printed before the spread props,
@@ -1169,7 +1182,7 @@ and print_jsx cxt ?(spread_props : J.expression option)
11691182 match children_opt with
11701183 | Some _ -> cxt
11711184 | None ->
1172- (* Put a space the tag name and /> *)
1185+ (* Put a space between the tag name and /> *)
11731186 P. space f;
11741187 cxt)
11751188 else
0 commit comments