forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_async.ml
42 lines (38 loc) · 1.45 KB
/
ast_async.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
let is_async : Parsetree.attribute -> bool =
fun ({txt}, _) -> txt = "async" || txt = "res.async"
let add_promise_type ?(loc = Location.none) ~async
(result : Parsetree.expression) =
if async then
let unsafe_async =
Ast_helper.Exp.ident ~loc
{txt = Ldot (Lident Js_runtime_modules.promise, "unsafe_async"); loc}
in
Ast_helper.Exp.apply ~loc unsafe_async [(Nolabel, result)]
else result
let add_async_attribute ~async (body : Parsetree.expression) =
if async then
(
match body.pexp_desc with
| Pexp_construct (x, Some e) when Ast_uncurried.expr_is_uncurried_fun body ->
{body with pexp_desc = Pexp_construct (x, Some {e with pexp_attributes =
({txt = "res.async"; loc = Location.none}, PStr []) :: e.pexp_attributes} )}
| _ ->
{
body with
pexp_attributes =
({txt = "res.async"; loc = Location.none}, PStr [])
:: body.pexp_attributes;
})
else body
let rec add_promise_to_result ~loc (e : Parsetree.expression) =
match e.pexp_desc with
| Pexp_fun (label, eo, pat, body) ->
let body = add_promise_to_result ~loc body in
{e with pexp_desc = Pexp_fun (label, eo, pat, body)}
| _ -> add_promise_type ~loc ~async:true e
let make_function_async ~async (e : Parsetree.expression) =
if async then
match e.pexp_desc with
| Pexp_fun (_, _, {ppat_loc}, _) -> add_promise_to_result ~loc:ppat_loc e
| _ -> assert false
else e