Skip to content

Commit c738f67

Browse files
committed
jsx component as an abstract type
1 parent 8100772 commit c738f67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+230
-164
lines changed

jscomp/others/jsxC.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ external string: string => element = "%identity"
3636
external array: array<element> => element = "%identity"
3737

3838
type componentLike<'props, 'return> = 'props => 'return
39-
type component<'props> = componentLike<'props, element>
39+
type component<'props>
4040

4141
/* this function exists to prepare for making `component` abstract */
4242
external component: componentLike<'props, element> => component<'props> = "%identity"

jscomp/others/jsxU.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ external string: string => element = "%identity"
1414
external array: array<element> => element = "%identity"
1515

1616
type componentLike<'props, 'return> = 'props => 'return
17-
type component<'props> = componentLike<'props, element>
17+
type component<'props>
1818

1919
/* this function exists to prepare for making `component` abstract */
2020
external component: componentLike<'props, element> => component<'props> = "%identity"

jscomp/syntax/src/jsx_v4.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,12 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
977977
innerExpression
978978
else innerExpression)
979979
in
980+
(* making component abstract *)
981+
let fullExpression =
982+
Exp.apply
983+
(Exp.ident (Location.mknoloc @@ Ldot (Lident "React", "component")))
984+
[(Nolabel, fullExpression)]
985+
in
980986
let fullExpression =
981987
if !Config.uncurried = Uncurried then
982988
fullExpression
@@ -1174,7 +1180,7 @@ let transformStructureItem ~config item =
11741180
(name, ptyp_attributes, returnValue.ptyp_loc, type_) :: types )
11751181
| _ -> (fullType, types)
11761182
in
1177-
let innerType, propTypes = getPropTypes [] pval_type in
1183+
let _innerType, propTypes = getPropTypes [] pval_type in
11781184
let namedTypeList = List.fold_left argToConcreteType [] propTypes in
11791185
let retPropsType =
11801186
Typ.constr ~loc:pstr_loc
@@ -1194,8 +1200,8 @@ let transformStructureItem ~config item =
11941200
(* can't be an arrow because it will defensively uncurry *)
11951201
let newExternalType =
11961202
Ptyp_constr
1197-
( {loc = pstr_loc; txt = moduleAccessName config "componentLike"},
1198-
[retPropsType; innerType] )
1203+
( {loc = pstr_loc; txt = moduleAccessName config "component"},
1204+
[retPropsType] )
11991205
in
12001206
let newStructure =
12011207
{
@@ -1282,7 +1288,7 @@ let transformSignatureItem ~config item =
12821288
(returnValue, (name, attrs, returnValue.ptyp_loc, type_) :: types)
12831289
| _ -> (fullType, types)
12841290
in
1285-
let innerType, propTypes = getPropTypes [] pval_type in
1291+
let _innerType, propTypes = getPropTypes [] pval_type in
12861292
let namedTypeList = List.fold_left argToConcreteType [] propTypes in
12871293
let retPropsType =
12881294
Typ.constr
@@ -1301,8 +1307,8 @@ let transformSignatureItem ~config item =
13011307
(* can't be an arrow because it will defensively uncurry *)
13021308
let newExternalType =
13031309
Ptyp_constr
1304-
( {loc = psig_loc; txt = moduleAccessName config "componentLike"},
1305-
[retPropsType; innerType] )
1310+
( {loc = psig_loc; txt = moduleAccessName config "component"},
1311+
[retPropsType] )
13061312
in
13071313
let newStructure =
13081314
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module SomeComp2 = {
2+
type props = {x: int}
3+
@module("SomeModule")
4+
external make: props => React.element = "SomeComp2"
5+
}
6+
7+
let _ = <SomeComp2 x=42 />
8+
9+
@@jsxConfig({version: 3})
10+
11+
@react.component
12+
let rec make = (~foo, ()) => React.createElement(make, makeProps(~foo, ()))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module SomeComp2 = {
2+
type props = {x: int}
3+
@module("SomeModule")
4+
external make: props => React.element = "SomeComp2"
5+
}
6+
7+
let _ = React.jsx(SomeComp2.make, {x: 42})
8+
9+
@@jsxConfig({version: 3})
10+
@obj external makeProps: (~foo: 'foo, ~key: string=?, unit) => {"foo": 'foo} = ""
11+
12+
let rec make = {
13+
@merlin.focus
14+
let rec \"make$Internal" =
15+
@warning("-16") (~foo, ()) => React.createElement(make, makeProps(~foo, ()))
16+
and make = {
17+
let \"Abstract" = (\"Props": {"foo": 'foo}) => \"make$Internal"(~foo=\"Props"["foo"], ())
18+
\"Abstract"
19+
}
20+
make
21+
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module C0 = {
1212
React.string(text)
1313
}
1414
let make = {
15-
let \"AliasProps$C0" = (props: props<_>) => make(props)
15+
let \"AliasProps$C0" = React.component((props: props<_>) => make(props))
1616

1717
\"AliasProps$C0"
1818
}
@@ -30,7 +30,7 @@ module C1 = {
3030
React.string(p ++ text)
3131
}
3232
let make = {
33-
let \"AliasProps$C1" = (props: props<_>) => make(props)
33+
let \"AliasProps$C1" = React.component((props: props<_>) => make(props))
3434

3535
\"AliasProps$C1"
3636
}
@@ -48,7 +48,7 @@ module C2 = {
4848
React.string(bar)
4949
}
5050
let make = {
51-
let \"AliasProps$C2" = (props: props<_>) => make(props)
51+
let \"AliasProps$C2" = React.component((props: props<_>) => make(props))
5252

5353
\"AliasProps$C2"
5454
}
@@ -72,7 +72,7 @@ module C3 = {
7272
}
7373
}
7474
let make = {
75-
let \"AliasProps$C3" = (props: props<_>) => make(props)
75+
let \"AliasProps$C3" = React.component((props: props<_>) => make(props))
7676

7777
\"AliasProps$C3"
7878
}
@@ -90,7 +90,7 @@ module C4 = {
9090
ReactDOM.jsx("div", {children: ?ReactDOM.someElement(b)})
9191
}
9292
let make = {
93-
let \"AliasProps$C4" = (props: props<_>) => make(props)
93+
let \"AliasProps$C4" = React.component((props: props<_>) => make(props))
9494

9595
\"AliasProps$C4"
9696
}
@@ -108,7 +108,7 @@ module C5 = {
108108
x + y + z
109109
}
110110
let make = {
111-
let \"AliasProps$C5" = (props: props<_>) => make(props)
111+
let \"AliasProps$C5" = React.component((props: props<_>) => make(props))
112112

113113
\"AliasProps$C5"
114114
}
@@ -118,13 +118,13 @@ module C6 = {
118118
module type Comp = {
119119
type props = {}
120120

121-
let make: React.componentLike<props, React.element>
121+
let make: React.component<props>
122122
}
123123
type props<'comp, 'x> = {comp: 'comp, x: 'x}
124124

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

129129
\"AliasProps$C6"
130130
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ module C0 = {
88
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
99
}
1010
let make = {
11-
let \"AsyncAwait$C0" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
11+
let \"AsyncAwait$C0" = React.component((props: props<_>) =>
12+
JsxPPXReactSupport.asyncComponent(make(props))
13+
)
1214

1315
\"AsyncAwait$C0"
1416
}
@@ -24,7 +26,9 @@ module C1 = {
2426
}
2527
}
2628
let make = {
27-
let \"AsyncAwait$C1" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
29+
let \"AsyncAwait$C1" = React.component((props: props<_>) =>
30+
JsxPPXReactSupport.asyncComponent(make(props))
31+
)
2832

2933
\"AsyncAwait$C1"
3034
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let make = ({msg, _}: props<_>) => {
44
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
55
}
66
let make = {
7-
let \"CommentAtTop" = (props: props<_>) => make(props)
7+
let \"CommentAtTop" = React.component((props: props<_>) => make(props))
88

99
\"CommentAtTop"
1010
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module C0 = {
1313
React.int(a + b)
1414
}
1515
let make = {
16-
let \"DefaultValueProp$C0" = (props: props<_>) => make(props)
16+
let \"DefaultValueProp$C0" = React.component((props: props<_>) => make(props))
1717
\"DefaultValueProp$C0"
1818
}
1919
}
@@ -30,7 +30,7 @@ module C1 = {
3030
React.int(a + b)
3131
}
3232
let make = {
33-
let \"DefaultValueProp$C1" = (props: props<_>) => make(props)
33+
let \"DefaultValueProp$C1" = React.component((props: props<_>) => make(props))
3434

3535
\"DefaultValueProp$C1"
3636
}
@@ -49,7 +49,7 @@ module C2 = {
4949
React.string(a)
5050
}
5151
let make = {
52-
let \"DefaultValueProp$C2" = (props: props<_>) => make(props)
52+
let \"DefaultValueProp$C2" = React.component((props: props<_>) => make(props))
5353

5454
\"DefaultValueProp$C2"
5555
}
@@ -69,7 +69,7 @@ module C3 = {
6969
}
7070
}
7171
let make = {
72-
let \"DefaultValueProp$C3" = (props: props<_>) => make(props)
72+
let \"DefaultValueProp$C3" = React.component((props: props<_>) => make(props))
7373

7474
\"DefaultValueProp$C3"
7575
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module Foo = {
1616
type props<'a, 'b> = {a: 'a, b: 'b}
1717

1818
@module("Foo")
19-
external component: React.componentLike<props<int, string>, React.element> = "component"
19+
external component: React.component<props<int, string>> = "component"
2020
}
2121

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

2929
@module("Foo")
30-
external component: React.componentLike<props<int, string>, React.element> = "component"
30+
external component: React.component<props<int, string>> = "component"
3131
}
3232

3333
let t = React.jsx(Foo.component, {a: 1, b: "1"})

0 commit comments

Comments
 (0)