From b0a1504b54873c0a4cb30d512c89bfb02e2685a3 Mon Sep 17 00:00:00 2001 From: mununki Date: Tue, 12 Sep 2023 21:53:35 +0900 Subject: [PATCH] convert async component type --- jscomp/others/jsxPPXReactSupportC.res | 4 ++++ jscomp/others/jsxPPXReactSupportU.res | 4 ++++ jscomp/syntax/src/react_jsx_common.ml | 10 ++++++++++ jscomp/syntax/src/reactjs_jsx_v4.ml | 11 +++++++++++ .../tests/ppx/react/expected/asyncAwait.res.txt | 2 +- 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/jscomp/others/jsxPPXReactSupportC.res b/jscomp/others/jsxPPXReactSupportC.res index 9049fc47df..35528518bd 100644 --- a/jscomp/others/jsxPPXReactSupportC.res +++ b/jscomp/others/jsxPPXReactSupportC.res @@ -48,3 +48,7 @@ let createElementWithKey = (~key=?, component, props) => let createElementVariadicWithKey = (~key=?, component, props, elements) => createElementVariadic(component, addKeyProp(~key?, props), elements) + +external asyncComponent: ('props => promise) => Jsx.component< + 'props, +> = "%identity" \ No newline at end of file diff --git a/jscomp/others/jsxPPXReactSupportU.res b/jscomp/others/jsxPPXReactSupportU.res index f35c1911c3..65c73e7e7f 100644 --- a/jscomp/others/jsxPPXReactSupportU.res +++ b/jscomp/others/jsxPPXReactSupportU.res @@ -50,3 +50,7 @@ let createElementWithKey = (~key=?, component, props) => let createElementVariadicWithKey = (~key=?, component, props, elements) => createElementVariadic(component, addKeyProp(~key?, props), elements) + +external asyncComponent: ('props => promise) => Jsx.component< + 'props, +> = "%identity" \ No newline at end of file diff --git a/jscomp/syntax/src/react_jsx_common.ml b/jscomp/syntax/src/react_jsx_common.ml index c951bebd46..d751b21f05 100644 --- a/jscomp/syntax/src/react_jsx_common.ml +++ b/jscomp/syntax/src/react_jsx_common.ml @@ -76,3 +76,13 @@ let add_async_attribute ~async (body : Parsetree.expression) = let is_async : Parsetree.attribute -> bool = fun ({txt}, _) -> txt = "async" || txt = "res.async" + +let async_component expr = + let open Ast_helper in + Exp.apply + (Exp.ident + { + loc = Location.none; + txt = Ldot (Lident "JsxPPXReactSupport", "asyncComponent"); + }) + [(Nolabel, expr)] diff --git a/jscomp/syntax/src/reactjs_jsx_v4.ml b/jscomp/syntax/src/reactjs_jsx_v4.ml index 6c73da0949..a1d0c1cc54 100644 --- a/jscomp/syntax/src/reactjs_jsx_v4.ml +++ b/jscomp/syntax/src/reactjs_jsx_v4.ml @@ -969,6 +969,17 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding = |> Ast_uncurried.uncurriedFun ~loc:fullExpression.pexp_loc ~arity:1 else fullExpression in + let fullExpression = + if isAsync then + Exp.apply + (Exp.ident + { + loc = Location.none; + txt = Ldot (Lident "JsxPPXReactSupport", "asyncComponent"); + }) + [(Nolabel, fullExpression)] + else fullExpression + in let fullExpression = match fullModuleName with | "" -> fullExpression diff --git a/jscomp/syntax/tests/ppx/react/expected/asyncAwait.res.txt b/jscomp/syntax/tests/ppx/react/expected/asyncAwait.res.txt index 640dc5a2e0..6b18e4f91f 100644 --- a/jscomp/syntax/tests/ppx/react/expected/asyncAwait.res.txt +++ b/jscomp/syntax/tests/ppx/react/expected/asyncAwait.res.txt @@ -6,7 +6,7 @@ let make = async ({a, _}: props<_>) => { ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})}) } let make = { - let \"AsyncAwait" = (props: props<_>) => make(props) + let \"AsyncAwait" = JsxPPXReactSupport.asyncComponent((props: props<_>) => make(props)) \"AsyncAwait" }