@@ -34,100 +34,101 @@ let handle_debugger loc (payload : Ast_payload.t) =
3434 | _ ->
3535 Location. raise_errorf ~loc " %%debugger extension doesn't accept arguments"
3636
37+ let raw_type () =
38+ Ast_helper.Typ. arrow
39+ [Parg_type {attrs = [] ; lbl = Nolabel ; typ = Ast_helper.Typ. any () }]
40+ (Ast_helper.Typ. any () )
41+
42+ (* [exp] is the string constant holding the JavaScript source. *)
43+ let raw_expression loc ~is_function (exp : Parsetree.expression ) =
44+ {
45+ exp with
46+ pexp_desc =
47+ Ast_external_mk. local_external_apply loc
48+ ~pval_prim: (Prim_name " #raw_expr" ) ~pval_type: (raw_type () ) [exp];
49+ pexp_attributes =
50+ (match ! is_function with
51+ | None -> exp.pexp_attributes
52+ | Some _ -> Ast_attributes. internal_expansive :: exp.pexp_attributes);
53+ }
54+
55+ let ffi_error loc =
56+ Location. raise_errorf ~loc
57+ " %%ffi extension can only be applied to a string containing a JavaScript \
58+ function such as \" (x) => ...\" "
59+
60+ let ffi_expression loc ~is_function (exp : Parsetree.expression ) =
61+ (* Wrap a type constraint based on arity.
62+ E.g. for arity 2 constrain to type (_, _) => _ *)
63+ let wrap_type_constraint (e : Parsetree.expression ) =
64+ let loc = e.pexp_loc in
65+ let any = Ast_helper.Typ. any ~loc: e.pexp_loc () in
66+ let arrow ~arity =
67+ let effective_arity = if arity = 0 then 1 else arity in
68+ let args =
69+ Ext_list. init effective_arity (fun _ ->
70+ Parsetree. Parg_type {attrs = [] ; lbl = Nolabel ; typ = any})
71+ in
72+ Ast_helper.Typ. arrow ~loc args any
73+ in
74+ match ! is_function with
75+ | Some arity -> Ast_helper.Exp. constraint_ ~loc e (arrow ~arity )
76+ | _ -> ffi_error loc
77+ in
78+ match ! is_function with
79+ | None -> ffi_error loc
80+ | Some _ -> wrap_type_constraint (raw_expression loc ~is_function exp)
81+
82+ let raw_structure loc (exp : Parsetree.expression ) =
83+ Ast_helper.Str. eval
84+ {
85+ exp with
86+ pexp_desc =
87+ Ast_external_mk. local_external_apply loc
88+ ~pval_prim: (Prim_name " #raw_stmt" ) ~pval_type: (raw_type () ) [exp];
89+ }
90+
3791let handle_raw loc payload =
3892 let is_function = ref None in
3993 match
4094 Ast_payload. raw_as_string_exp_exn ~kind: Raw_exp ~is_function payload
4195 with
4296 | None ->
4397 Location. raise_errorf ~loc " %%raw extension can only be applied to a string"
44- | Some exp ->
45- {
46- exp with
47- pexp_desc =
48- Ast_external_mk. local_external_apply loc
49- ~pval_prim: (Prim_name " #raw_expr" )
50- ~pval_type:
51- (Ast_helper.Typ. arrow
52- [
53- Parg_type
54- {attrs = [] ; lbl = Nolabel ; typ = Ast_helper.Typ. any () };
55- ]
56- (Ast_helper.Typ. any () ))
57- [exp];
58- pexp_attributes =
59- (match ! is_function with
60- | None -> exp.pexp_attributes
61- | Some _ -> Ast_attributes. internal_expansive :: exp.pexp_attributes);
62- }
98+ | Some exp -> raw_expression loc ~is_function exp
6399
64100let handle_ffi ~loc ~payload =
65101 let is_function = ref None in
66- let err () =
67- Location. raise_errorf ~loc
68- " %%ffi extension can only be applied to a string containing a JavaScript \
69- function such as \" (x) => ...\" "
70- in
71102 match
72103 Ast_payload. raw_as_string_exp_exn ~kind: Raw_exp ~is_function payload
73104 with
74- | None -> err ()
75- | Some exp ->
76- (* Wrap a type constraint based on arity.
77- E.g. for arity 2 constrain to type (_, _) => _ *)
78- let wrap_type_constraint (e : Parsetree.expression ) =
79- let loc = e.pexp_loc in
80- let any = Ast_helper.Typ. any ~loc: e.pexp_loc () in
81- let arrow ~arity =
82- let effective_arity = if arity = 0 then 1 else arity in
83- let args =
84- Ext_list. init effective_arity (fun _ ->
85- Parsetree. Parg_type {attrs = [] ; lbl = Nolabel ; typ = any})
86- in
87- Ast_helper.Typ. arrow ~loc args any
88- in
89- match ! is_function with
90- | Some arity -> Ast_helper.Exp. constraint_ ~loc e (arrow ~arity )
91- | _ -> err ()
92- in
93- wrap_type_constraint
94- {
95- exp with
96- pexp_desc =
97- Ast_external_mk. local_external_apply loc
98- ~pval_prim: (Prim_name " #raw_expr" )
99- ~pval_type:
100- (Ast_helper.Typ. arrow
101- [
102- Parg_type
103- {attrs = [] ; lbl = Nolabel ; typ = Ast_helper.Typ. any () };
104- ]
105- (Ast_helper.Typ. any () ))
106- [exp];
107- pexp_attributes =
108- (match ! is_function with
109- | None -> exp.pexp_attributes
110- | Some _ -> Ast_attributes. internal_expansive :: exp.pexp_attributes);
111- }
105+ | None -> ffi_error loc
106+ | Some exp -> ffi_expression loc ~is_function exp
112107
113108let handle_raw_structure loc payload =
114109 match Ast_payload. raw_as_string_exp_exn ~kind: Raw_program payload with
115- | Some exp ->
116- Ast_helper.Str. eval
117- {
118- exp with
119- pexp_desc =
120- Ast_external_mk. local_external_apply loc
121- ~pval_prim: (Prim_name " #raw_stmt" )
122- ~pval_type:
123- (Ast_helper.Typ. arrow
124- [
125- Parg_type
126- {attrs = [] ; lbl = Nolabel ; typ = Ast_helper.Typ. any () };
127- ]
128- (Ast_helper.Typ. any () ))
129- [exp];
130- }
110+ | Some exp -> raw_structure loc exp
131111 | None ->
132112 Location. raise_errorf ~loc
133113 " %%%%raw extension can only be applied to a string"
114+
115+ (* The string constant holding the JavaScript source of a raw node, validated
116+ like a raw payload. *)
117+ let raw_source_expression ~(kind : Js_raw_info.raw_kind ) ?is_function
118+ (source : Parsetree.raw_source ) =
119+ let str = Ast_raw. js_source source in
120+ let loc = Ast_raw. payload_loc source in
121+ Ast_payload. validate_raw_source ~kind ?is_function ~loc
122+ ~offset: (Bs_flow_ast_utils. flow_deli_offset (Some " js" ))
123+ str;
124+ Ast_helper.Exp. constant ~loc (Ast_helper.Const. string str)
125+
126+ let handle_raw_node ~loc (kind : Parsetree.raw_kind ) source =
127+ let is_function = ref None in
128+ let exp = raw_source_expression ~kind: Raw_exp ~is_function source in
129+ match kind with
130+ | Raw_expression -> raw_expression loc ~is_function exp
131+ | Raw_function -> ffi_expression loc ~is_function exp
132+
133+ let handle_raw_structure_node ~loc source =
134+ raw_structure loc (raw_source_expression ~kind: Raw_program source)
0 commit comments