Skip to content

Commit

Permalink
refactor: share more code
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Aug 16, 2024
1 parent e5eeb3f commit 90ee9f8
Showing 1 changed file with 15 additions and 47 deletions.
62 changes: 15 additions & 47 deletions ppx/reason_react_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1403,23 +1403,8 @@ let jsxMapper =
This will match either <> </> or <> foo </> *)
| {
pexp_desc =
( Pexp_construct ({ txt = Lident "[]"; loc }, None)
| Pexp_construct
( { txt = Lident "::"; loc },
Some
{
pexp_desc =
Pexp_tuple
[
_;
{
pexp_desc =
Pexp_construct ({ txt = Lident "[]"; _ }, None);
_;
};
];
_;
} ) );
( Pexp_construct ({ txt = Lident "[]"; loc }, lst)
| Pexp_construct ({ txt = Lident "::"; loc }, lst) );
pexp_attributes;
_;
} as listItems -> (
Expand All @@ -1431,50 +1416,33 @@ let jsxMapper =
match (jsxAttribute, nonJSXAttributes) with
(* no JSX attribute *)
| [], _ -> super#expression ctxt expr
| _, nonJSXAttributes ->
| _, nonJSXAttributes -> (
let childrenExpr =
transformChildrenIfList ~loc ~ctxt ~mapper:self listItems
in
(* throw away the [@JSX] attribute and keep the others, if any *)
Binding.React.jsxFragment ~loc ~attrs:nonJSXAttributes
(Binding.React.array ~loc childrenExpr))
(* Fragment with two or more children: <> foo bar </> *)
| {
pexp_desc =
Pexp_construct
( { txt = Lident "::"; loc },
Some
match lst with
| None
| Some
{
pexp_desc =
Pexp_tuple
[
_firstElement;
_;
{
pexp_desc =
Pexp_construct ({ txt = Lident "::"; _ }, Some _);
Pexp_construct ({ txt = Lident "[]"; _ }, None);
_;
};
];
_;
} );
pexp_attributes;
_;
} as listItems -> (
let jsxAttribute, nonJSXAttributes =
List.partition
(fun { attr_name = attribute; _ } -> attribute.txt = "JSX")
pexp_attributes
in
match (jsxAttribute, nonJSXAttributes) with
(* no JSX attribute *)
| [], _ -> super#expression ctxt expr
| _, nonJSXAttributes ->
let childrenExpr =
transformChildrenIfList ~loc ~ctxt ~mapper:self listItems
in
(* throw away the [@JSX] attribute and keep the others, if any *)
Binding.React.jsxsFragment ~loc ~attrs:nonJSXAttributes
(Binding.React.array ~loc childrenExpr))
} ->
Binding.React.jsxFragment ~loc ~attrs:nonJSXAttributes
(Binding.React.array ~loc childrenExpr)
| Some _ ->
(* Fragment with two or more children: <> foo bar </> *)
Binding.React.jsxsFragment ~loc ~attrs:nonJSXAttributes
(Binding.React.array ~loc childrenExpr)))
(* Delegate to the default mapper, a deep identity traversal *)
| e -> super#expression ctxt e
[@@raises Invalid_argument]
Expand Down

0 comments on commit 90ee9f8

Please sign in to comment.