Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore to make JSX component abstract #6304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jscomp/others/jsxC.res
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ external string: string => element = "%identity"
external array: array<element> => element = "%identity"

type componentLike<'props, 'return> = 'props => 'return
type component<'props> = componentLike<'props, element>
type component<'props>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will eventually affect JSX3 as well.


/* this function exists to prepare for making `component` abstract */
external component: componentLike<'props, element> => component<'props> = "%identity"
2 changes: 1 addition & 1 deletion jscomp/others/jsxU.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ external string: string => element = "%identity"
external array: array<element> => element = "%identity"

type componentLike<'props, 'return> = 'props => 'return
type component<'props> = componentLike<'props, element>
type component<'props>

/* this function exists to prepare for making `component` abstract */
external component: componentLike<'props, element> => component<'props> = "%identity"
18 changes: 12 additions & 6 deletions jscomp/syntax/src/jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,12 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
innerExpression
else innerExpression)
in
(* making component abstract *)
let fullExpression =
Exp.apply
(Exp.ident (Location.mknoloc @@ Ldot (Lident "React", "component")))
[(Nolabel, fullExpression)]
in
let fullExpression =
if !Config.uncurried = Uncurried then
fullExpression
Expand Down Expand Up @@ -1174,7 +1180,7 @@ let transformStructureItem ~config item =
(name, ptyp_attributes, returnValue.ptyp_loc, type_) :: types )
| _ -> (fullType, types)
in
let innerType, propTypes = getPropTypes [] pval_type in
let _innerType, propTypes = getPropTypes [] pval_type in
let namedTypeList = List.fold_left argToConcreteType [] propTypes in
let retPropsType =
Typ.constr ~loc:pstr_loc
Expand All @@ -1194,8 +1200,8 @@ let transformStructureItem ~config item =
(* can't be an arrow because it will defensively uncurry *)
let newExternalType =
Ptyp_constr
( {loc = pstr_loc; txt = moduleAccessName config "componentLike"},
[retPropsType; innerType] )
( {loc = pstr_loc; txt = moduleAccessName config "component"},
[retPropsType] )
in
let newStructure =
{
Expand Down Expand Up @@ -1282,7 +1288,7 @@ let transformSignatureItem ~config item =
(returnValue, (name, attrs, returnValue.ptyp_loc, type_) :: types)
| _ -> (fullType, types)
in
let innerType, propTypes = getPropTypes [] pval_type in
let _innerType, propTypes = getPropTypes [] pval_type in
let namedTypeList = List.fold_left argToConcreteType [] propTypes in
let retPropsType =
Typ.constr
Expand All @@ -1301,8 +1307,8 @@ let transformSignatureItem ~config item =
(* can't be an arrow because it will defensively uncurry *)
let newExternalType =
Ptyp_constr
( {loc = psig_loc; txt = moduleAccessName config "componentLike"},
[retPropsType; innerType] )
( {loc = psig_loc; txt = moduleAccessName config "component"},
[retPropsType] )
in
let newStructure =
{
Expand Down
12 changes: 12 additions & 0 deletions jscomp/syntax/tests/ppx/react/abstract.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module SomeComp2 = {
type props = {x: int}
@module("SomeModule")
external make: props => React.element = "SomeComp2"
}

let _ = <SomeComp2 x=42 />

@@jsxConfig({version: 3})

@react.component
let rec make = (~foo, ()) => React.createElement(make, makeProps(~foo, ()))
21 changes: 21 additions & 0 deletions jscomp/syntax/tests/ppx/react/expected/abstract.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module SomeComp2 = {
type props = {x: int}
@module("SomeModule")
external make: props => React.element = "SomeComp2"
}

let _ = React.jsx(SomeComp2.make, {x: 42})

@@jsxConfig({version: 3})
@obj external makeProps: (~foo: 'foo, ~key: string=?, unit) => {"foo": 'foo} = ""

let rec make = {
@merlin.focus
let rec \"make$Internal" =
@warning("-16") (~foo, ()) => React.createElement(make, makeProps(~foo, ()))
and make = {
let \"Abstract" = (\"Props": {"foo": 'foo}) => \"make$Internal"(~foo=\"Props"["foo"], ())
\"Abstract"
}
make
}
16 changes: 8 additions & 8 deletions jscomp/syntax/tests/ppx/react/expected/aliasProps.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module C0 = {
React.string(text)
}
let make = {
let \"AliasProps$C0" = (props: props<_>) => make(props)
let \"AliasProps$C0" = React.component((props: props<_>) => make(props))

\"AliasProps$C0"
}
Expand All @@ -30,7 +30,7 @@ module C1 = {
React.string(p ++ text)
}
let make = {
let \"AliasProps$C1" = (props: props<_>) => make(props)
let \"AliasProps$C1" = React.component((props: props<_>) => make(props))

\"AliasProps$C1"
}
Expand All @@ -48,7 +48,7 @@ module C2 = {
React.string(bar)
}
let make = {
let \"AliasProps$C2" = (props: props<_>) => make(props)
let \"AliasProps$C2" = React.component((props: props<_>) => make(props))

\"AliasProps$C2"
}
Expand All @@ -72,7 +72,7 @@ module C3 = {
}
}
let make = {
let \"AliasProps$C3" = (props: props<_>) => make(props)
let \"AliasProps$C3" = React.component((props: props<_>) => make(props))

\"AliasProps$C3"
}
Expand All @@ -90,7 +90,7 @@ module C4 = {
ReactDOM.jsx("div", {children: ?ReactDOM.someElement(b)})
}
let make = {
let \"AliasProps$C4" = (props: props<_>) => make(props)
let \"AliasProps$C4" = React.component((props: props<_>) => make(props))

\"AliasProps$C4"
}
Expand All @@ -108,7 +108,7 @@ module C5 = {
x + y + z
}
let make = {
let \"AliasProps$C5" = (props: props<_>) => make(props)
let \"AliasProps$C5" = React.component((props: props<_>) => make(props))

\"AliasProps$C5"
}
Expand All @@ -118,13 +118,13 @@ module C6 = {
module type Comp = {
type props = {}

let make: React.componentLike<props, React.element>
let make: React.component<props>
}
type props<'comp, 'x> = {comp: 'comp, x: 'x}

let make = ({comp: module(Comp: Comp), x: (a, b), _}: props<_, _>) => React.jsx(Comp.make, {})
let make = {
let \"AliasProps$C6" = (props: props<_>) => make(props)
let \"AliasProps$C6" = React.component((props: props<_>) => make(props))

\"AliasProps$C6"
}
Expand Down
8 changes: 6 additions & 2 deletions jscomp/syntax/tests/ppx/react/expected/asyncAwait.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ module C0 = {
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
}
let make = {
let \"AsyncAwait$C0" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
let \"AsyncAwait$C0" = React.component((props: props<_>) =>
JsxPPXReactSupport.asyncComponent(make(props))
)

\"AsyncAwait$C0"
}
Expand All @@ -24,7 +26,9 @@ module C1 = {
}
}
let make = {
let \"AsyncAwait$C1" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
let \"AsyncAwait$C1" = React.component((props: props<_>) =>
JsxPPXReactSupport.asyncComponent(make(props))
)

\"AsyncAwait$C1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let make = ({msg, _}: props<_>) => {
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
}
let make = {
let \"CommentAtTop" = (props: props<_>) => make(props)
let \"CommentAtTop" = React.component((props: props<_>) => make(props))

\"CommentAtTop"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module C0 = {
React.int(a + b)
}
let make = {
let \"DefaultValueProp$C0" = (props: props<_>) => make(props)
let \"DefaultValueProp$C0" = React.component((props: props<_>) => make(props))
\"DefaultValueProp$C0"
}
}
Expand All @@ -30,7 +30,7 @@ module C1 = {
React.int(a + b)
}
let make = {
let \"DefaultValueProp$C1" = (props: props<_>) => make(props)
let \"DefaultValueProp$C1" = React.component((props: props<_>) => make(props))

\"DefaultValueProp$C1"
}
Expand All @@ -49,7 +49,7 @@ module C2 = {
React.string(a)
}
let make = {
let \"DefaultValueProp$C2" = (props: props<_>) => make(props)
let \"DefaultValueProp$C2" = React.component((props: props<_>) => make(props))

\"DefaultValueProp$C2"
}
Expand All @@ -69,7 +69,7 @@ module C3 = {
}
}
let make = {
let \"DefaultValueProp$C3" = (props: props<_>) => make(props)
let \"DefaultValueProp$C3" = React.component((props: props<_>) => make(props))

\"DefaultValueProp$C3"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Foo = {
type props<'a, 'b> = {a: 'a, b: 'b}

@module("Foo")
external component: React.componentLike<props<int, string>, React.element> = "component"
external component: React.component<props<int, string>> = "component"
}

let t = React.createElement(Foo.component, {a: 1, b: "1"})
Expand All @@ -27,7 +27,7 @@ module Foo = {
type props<'a, 'b> = {a: 'a, b: 'b}

@module("Foo")
external component: React.componentLike<props<int, string>, React.element> = "component"
external component: React.component<props<int, string>> = "component"
}

let t = React.jsx(Foo.component, {a: 1, b: "1"})
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ module V4C = {
}

@module("componentForwardRef")
external make: React.componentLike<props<string, ReactDOM.Ref.currentDomRef>, React.element> =
"component"
external make: React.component<props<string, ReactDOM.Ref.currentDomRef>> = "component"
}

@@jsxConfig({version: 4, mode: "automatic"})
Expand All @@ -37,6 +36,5 @@ module V4C = {
}

@module("componentForwardRef")
external make: React.componentLike<props<string, ReactDOM.Ref.currentDomRef>, React.element> =
"component"
external make: React.component<props<string, ReactDOM.Ref.currentDomRef>> = "component"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module V4C = {
}

@module("c")
external make: React.componentLike<props<t<'a>, React.element>, React.element> = "component"
external make: React.component<props<t<'a>, React.element>> = "component"
}

@@jsxConfig({version: 4, mode: "automatic"})
Expand All @@ -34,5 +34,5 @@ module V4C = {
}

@module("c")
external make: React.componentLike<props<t<'a>, React.element>, React.element> = "component"
external make: React.component<props<t<'a>, React.element>> = "component"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module V4C = {
ReactDOM.createDOMElementVariadic("div", [{msg->React.string}])
}
let make = {
let \"FileLevelConfig$V4C" = (props: props<_>) => make(props)
let \"FileLevelConfig$V4C" = React.component((props: props<_>) => make(props))

\"FileLevelConfig$V4C"
}
Expand All @@ -39,7 +39,7 @@ module V4A = {
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
}
let make = {
let \"FileLevelConfig$V4A" = (props: props<_>) => make(props)
let \"FileLevelConfig$V4A" = React.component((props: props<_>) => make(props))

\"FileLevelConfig$V4A"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module Select = {
ReactDOM.createDOMElementVariadic("div", [])
}
let make = {
let \"FirstClassModules$Select" = (props: props<_>) => make(props)
let \"FirstClassModules$Select" = React.component((props: props<_>) => make(props))

\"FirstClassModules$Select"
}
Expand All @@ -96,13 +96,12 @@ module External = {
}

@module("c")
external make: React.componentLike<
external make: React.component<
props<
module(T with type t = 'a and type key = 'key),
option<'key>,
(option<'key> => unit),
array<'a>,
>,
React.element,
> = "default"
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ module Select: {
items: 'items,
}

let make: React.componentLike<
let make: React.component<
props<
module(T with type t = 'a and type key = 'key),
option<'key>,
(option<'key> => unit),
array<'a>,
>,
React.element,
>
}
Loading
Loading