Skip to content

Commit 02d2403

Browse files
vouillonhhugo
authored andcommitted
ppx_js: make the code simpler
Use Ast_builder.Default.pexp_fun, which takes care of extending a function with an additional parameter.
1 parent b8b2146 commit 02d2403

File tree

1 file changed

+47
-83
lines changed

1 file changed

+47
-83
lines changed

ppx/ppx_js/lib_internal/ppx_js_internal.ml

Lines changed: 47 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,21 @@ end = struct
215215
let args l = List.map ~f:(fun x -> label x, typ x) l
216216
end
217217

218+
let lift_function_body_constraint expr = expr [@@if ast_version < 502]
219+
220+
let lift_function_body_constraint expr =
221+
match expr.pexp_desc with
222+
| Pexp_function
223+
( params
224+
, None
225+
, Pfunction_body { pexp_desc = Pexp_constraint (body, ty); pexp_attributes = []; _ }
226+
) ->
227+
{ expr with
228+
pexp_desc = Pexp_function (params, Some (Pconstraint ty), Pfunction_body body)
229+
}
230+
| _ -> expr
231+
[@@if ast_version >= 502]
232+
218233
let js_dot_t_the_first_arg args =
219234
match args with
220235
| [] -> assert false
@@ -250,31 +265,20 @@ let invoker ?(extra_types = []) uplift downlift body arguments =
250265
in
251266
let make_fun (label, pat) (label', typ) expr =
252267
assert (label' = label);
253-
match expr.pexp_desc with
254-
| ((Pexp_function (params, c, b)) [@if ast_version >= 502]) ->
255-
let params =
256-
{ pparam_desc = Pparam_val (nolabel, None, Pat.constraint_ pat typ)
257-
; pparam_loc = { expr.pexp_loc with loc_ghost = true }
258-
}
259-
:: params
260-
in
261-
let c, b =
262-
match c, b with
263-
| ( None
264-
, Pfunction_body
265-
{ pexp_desc = Pexp_constraint (e, ty); pexp_attributes = []; _ } ) ->
266-
Some (Pconstraint ty), Pfunction_body e
267-
| _ -> c, b
268-
in
269-
{ expr with pexp_desc = Pexp_function (params, c, b) }
270-
| _ -> Exp.fun_ label None (Pat.constraint_ pat typ) expr
268+
Ast_builder.Default.pexp_fun
269+
~loc:!Ast_helper.default_loc
270+
label
271+
None
272+
(Pat.constraint_ pat typ)
273+
expr
271274
in
272275
let invoker =
273-
List.fold_right2
274-
labels_and_pats
275-
tfunc_args
276-
~f:make_fun
277-
~init:(make_fun (nolabel, Pat.any ()) (nolabel, twrap) annotated_ebody)
276+
lift_function_body_constraint
277+
(List.fold_right2
278+
labels_and_pats
279+
tfunc_args
280+
~f:make_fun
281+
~init:(make_fun (nolabel, Pat.any ()) (nolabel, twrap) annotated_ebody))
278282
in
279283
(* Introduce all local types:
280284
{[ fun (type res t0 t1 ..) arg1 arg2 -> e ]}
@@ -342,7 +346,7 @@ let method_call ~loc ~apply_loc obj (meth, meth_loc) args =
342346
{ invoker with pexp_attributes = merlin_hide :: invoker.pexp_attributes }
343347
((app_arg obj :: args)
344348
@ [ app_arg
345-
(Exp.fun_
349+
(Ast_builder.Default.pexp_fun
346350
~loc:gloc
347351
nolabel
348352
None
@@ -385,7 +389,7 @@ let prop_get ~loc obj prop =
385389
invoker
386390
[ app_arg obj
387391
; app_arg
388-
(Exp.fun_
392+
(Ast_builder.Default.pexp_fun
389393
~loc:gloc
390394
nolabel
391395
None
@@ -435,7 +439,7 @@ let prop_set ~loc ~prop_loc obj prop value =
435439
[ app_arg obj
436440
; app_arg value
437441
; app_arg
438-
(Exp.fun_
442+
(Ast_builder.Default.pexp_fun
439443
~loc:{ loc with loc_ghost = true }
440444
nolabel
441445
None
@@ -530,7 +534,7 @@ type field_desc =
530534
string Asttypes.loc
531535
* Asttypes.private_flag
532536
* Asttypes.override_flag
533-
* (Parsetree.expression * Parsetree.core_type option)
537+
* Parsetree.expression
534538
* Arg.t list
535539
| Val of
536540
string Asttypes.loc * Prop_kind.t * Asttypes.override_flag * Parsetree.expression
@@ -636,10 +640,10 @@ let preprocess_literal_object mappper fields :
636640

637641
let body =
638642
match body_ty with
639-
| None -> body, None
643+
| None -> body
640644
| Some { ptyp_desc = Ptyp_poly _; _ } ->
641645
raise_errorf ~loc:exp.pcf_loc "Polymorphic method not supported."
642-
| Some ty -> body, Some ty
646+
| Some ty -> Exp.constraint_ body ty
643647
in
644648
names, Meth (id, priv, bang, body, fun_ty) :: fields
645649
| _ ->
@@ -692,43 +696,14 @@ let literal_object self_id (fields : field_desc list) =
692696
in
693697
let body = function
694698
| Val (_, _, _, body) -> body
695-
| Meth (_, _, _, (body, ty), _) -> (
696-
match body.pexp_desc, ty with
697-
| ((Pexp_function (params, c, b), None) [@if ast_version >= 502]) ->
698-
let params =
699-
{ pparam_desc = Pparam_val (nolabel, None, self_id)
700-
; pparam_loc = { body.pexp_loc with loc_ghost = true }
701-
}
702-
:: params
703-
in
704-
{ body with pexp_desc = Pexp_function (params, c, b) }
705-
| ((_, Some ty) [@if ast_version >= 502]) -> (
706-
let e =
707-
Exp.fun_
708-
~loc:{ body.pexp_loc with loc_ghost = true }
709-
Nolabel
710-
None
711-
self_id
712-
body
713-
in
714-
match e.pexp_desc with
715-
| Pexp_function ([ param ], None, b) ->
716-
{ e with pexp_desc = Pexp_function ([ param ], Some (Pconstraint ty), b) }
717-
| _ -> assert false)
718-
| ((_, Some ty) [@if ast_version < 502]) ->
719-
Exp.fun_
720-
~loc:{ body.pexp_loc with loc_ghost = true }
721-
Nolabel
722-
None
723-
self_id
724-
(Exp.constraint_ body ty)
725-
| _, None ->
726-
Exp.fun_
727-
~loc:{ body.pexp_loc with loc_ghost = true }
728-
Nolabel
729-
None
730-
self_id
731-
body)
699+
| Meth (_, _, _, body, _) ->
700+
lift_function_body_constraint
701+
(Ast_builder.Default.pexp_fun
702+
~loc:{ body.pexp_loc with loc_ghost = true }
703+
Nolabel
704+
None
705+
self_id
706+
body)
732707
in
733708
let extra_types =
734709
List.concat
@@ -816,23 +791,12 @@ let literal_object self_id (fields : field_desc list) =
816791
(self :: List.map fields ~f:(fun f -> (name f).txt))
817792
~init:fake_object
818793
~f:(fun name fun_ ->
819-
match fun_.pexp_desc with
820-
| ((Pexp_function (params, c, b)) [@if ast_version >= 502]) ->
821-
let params =
822-
{ pparam_desc =
823-
Pparam_val (nolabel, None, Pat.var ~loc:gloc (mknoloc name))
824-
; pparam_loc = { fun_.pexp_loc with loc_ghost = true }
825-
}
826-
:: params
827-
in
828-
{ fun_ with pexp_desc = Pexp_function (params, c, b) }
829-
| _ ->
830-
Exp.fun_
831-
~loc:gloc
832-
nolabel
833-
None
834-
(Pat.var ~loc:gloc (mknoloc name))
835-
fun_))
794+
Ast_builder.Default.pexp_fun
795+
~loc:gloc
796+
nolabel
797+
None
798+
(Pat.var ~loc:gloc (mknoloc name))
799+
fun_))
836800
with
837801
pexp_attributes = [ merlin_hide ]
838802
}

0 commit comments

Comments
 (0)