Skip to content

Commit 0446032

Browse files
committed
support async in JSX ppx
1 parent 6ef2769 commit 0446032

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Add builtin abstract types for File and Blob APIs. https://github.com/rescript-lang/rescript-compiler/pull/6383
1919
- Untagged variants: Support `promise`, RegExes, Dates, File and Blob. https://github.com/rescript-lang/rescript-compiler/pull/6383
2020
- Support aliased types as payloads to untagged variants. https://github.com/rescript-lang/rescript-compiler/pull/6394
21+
- Support the async component for React Server Component in JSX ppx
2122

2223
# 11.0.0-rc.3
2324

jscomp/syntax/src/react_jsx_common.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,16 @@ let removeArity binding =
6363
| _ -> expr
6464
in
6565
{binding with pvb_expr = removeArityRecord binding.pvb_expr}
66+
67+
let add_async_attribute ~async (body : Parsetree.expression) =
68+
if async then
69+
{
70+
body with
71+
pexp_attributes =
72+
({txt = "res.async"; loc = Location.none}, PStr [])
73+
:: body.pexp_attributes;
74+
}
75+
else body
76+
77+
let is_async : Parsetree.attribute -> bool =
78+
fun ({txt}, _) -> txt = "async" || txt = "res.async"

jscomp/syntax/src/reactjs_jsx_v4.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,11 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
909909
let bindingWrapper, hasForwardRef, expression =
910910
modifiedBinding ~bindingLoc ~bindingPatLoc ~fnName binding
911911
in
912+
let isAsync =
913+
Ext_list.find_first binding.pvb_expr.pexp_attributes
914+
React_jsx_common.is_async
915+
|> Option.is_some
916+
in
912917
(* do stuff here! *)
913918
let namedArgList, newtypes, _typeConstraints =
914919
recursivelyTransformNamedArgsForMake
@@ -1083,6 +1088,9 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
10831088
| _ -> [Typ.any ()]))))
10841089
expression
10851090
in
1091+
let expression =
1092+
React_jsx_common.add_async_attribute ~async:isAsync expression
1093+
in
10861094
let expression =
10871095
(* Add new tupes (type a,b,c) to make's definition *)
10881096
newtypes

jscomp/syntax/tests/ppx/react/expected/asyncAwait.res.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let f = a => Js.Promise.resolve(a + a)
22
type props<'a> = {a: 'a}
33

4-
let make = ({a, _}: props<_>) => {
4+
let make = async ({a, _}: props<_>) => {
55
let a = await f(a)
66
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
77
}

0 commit comments

Comments
 (0)