Skip to content

Commit 55a8e5f

Browse files
davesnxandreypopp
authored andcommitted
Make sure sub expressions don't have par
1 parent 43322bb commit 55a8e5f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/Fmt_ast.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,11 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
22732273
| Pexp_apply (e0, e1N1) -> (
22742274
match pexp_attributes with
22752275
| [{attr_name={txt="JSX";loc=_}; attr_payload=PStr []; _}] ->
2276+
let is_jsx_element e =
2277+
match e.pexp_attributes with
2278+
| [{attr_name={txt="JSX";_}; attr_payload=PStr []; _}] -> true
2279+
| _ -> false
2280+
in
22762281
let children = ref None in
22772282
let props = List.filter_map e1N1 ~f:(function
22782283
| Labelled {txt="children";_}, {pexp_desc=Pexp_list es;pexp_loc;_} ->
@@ -2305,11 +2310,6 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
23052310
match props with
23062311
| [] -> str ""
23072312
| props ->
2308-
let is_jsx_element e =
2309-
match e.pexp_attributes with
2310-
| [{attr_name={txt="JSX";_}; attr_payload=PStr []; _}] -> true
2311-
| _ -> false
2312-
in
23132313
let fmt_labelled ?(prefix="") label e =
23142314
let flabel = str (Printf.sprintf "%s%s" prefix label.txt) in
23152315
match e.pexp_desc with
@@ -2338,7 +2338,11 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
23382338
let children =
23392339
hvbox 0 (
23402340
list children (break 1 0)
2341-
(fun e -> fmt_expression c ~parens:false (sub_exp ~ctx e))
2341+
(fun e ->
2342+
if is_jsx_element e then
2343+
fmt_expression c ~parens:false (sub_exp ~ctx e)
2344+
else
2345+
fmt_expression c (sub_exp ~ctx e))
23422346
$ Cmts.fmt_after c children_loc)
23432347
in
23442348
hvbox 2 (head $ break 0 0 $ children $ break 0 (-2) $ end_tag ())

test/mlx/mlx.t

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,15 @@ JSX elements as props:
180180
let _ = <div element=(<Componient />) />
181181
$ echo 'let _ = <Big element=(<Component />) />' | fmt
182182
let _ = <Big element=(<Component />) />
183+
184+
$ echo 'let _ = <Big>(<Component />)</Big>' | fmt
185+
let _ = <Big><Component /></Big>
186+
187+
$ echo 'let _ = <Big>(React.string children)</Big>' | fmt
188+
let _ = <Big>(React.string children)</Big>
189+
190+
$ echo 'let _ = <Big>(match x with | A -> "33" | B -> "44")</Big>' | fmt
191+
let _ = <Big>(match x with A -> "33" | B -> "44")</Big>
192+
193+
$ echo 'let _ = <Big>(<Lola />)</Big>' | fmt
194+
let _ = <Big><Lola /></Big>

0 commit comments

Comments
 (0)