From 55379e666ec8cafef7856985cf11753eafff7543 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 9 Nov 2019 23:53:48 +0100 Subject: [PATCH] allow labelled arguments after () --- jsx/brisk_jsx.ml | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/jsx/brisk_jsx.ml b/jsx/brisk_jsx.ml index 5783a91..402c13a 100644 --- a/jsx/brisk_jsx.ml +++ b/jsx/brisk_jsx.ml @@ -78,24 +78,32 @@ module Declaration_ppx = struct "nativeComponent" let transform_component_expr ~useDynamicKey ~attribute ~component_name expr = - let rec map_component_expression ({P.pexp_loc= loc} as expr) = - match_ func_pattern loc expr - ~with_:(fun lbl opt_arg pat child_expression -> - let make_fun_with_expr ~expr = - Ppxlib.Ast_builder.Default.pexp_fun ~loc lbl opt_arg pat expr - in - let loc = pat.Ppxlib.ppat_loc in - match (lbl, pat) with - | (Ppxlib.Labelled _ | Optional _), _ -> - make_fun_with_expr - ~expr:(map_component_expression child_expression) - | Ppxlib.Nolabel, [%pat? ()] -> - let loc = child_expression.pexp_loc in - make_fun_with_expr - ~expr:[%expr component ~key [%e child_expression]] - | _ -> - Location.raise_errorf ~loc - "A labelled argument or () was expected") + let map_component_expression expr = + let rec loop ~seenUnit ({P.pexp_loc= loc} as expr) = + try + match_ func_pattern loc expr + ~with_:(fun lbl opt_arg pat child_expression -> + let make_fun_with_expr ~expr = + Ppxlib.Ast_builder.Default.pexp_fun ~loc lbl opt_arg pat expr + in + let loc = pat.Ppxlib.ppat_loc in + match (lbl, pat) with + | (Ppxlib.Labelled _ | Optional _), _ -> + make_fun_with_expr + ~expr:(loop ~seenUnit child_expression) + | Ppxlib.Nolabel, [%pat? ()] -> + make_fun_with_expr + ~expr:(loop ~seenUnit:true child_expression) + | _ -> + if seenUnit then + [%expr component ~key [%e make_fun_with_expr ~expr:child_expression]] + else + Location.raise_errorf ~loc + "A labelled argument or () was expected") + with + | _ -> [%expr component ~key [%e expr]] + in + loop ~seenUnit:false expr in let open P in let loc = expr.P.pexp_loc in