Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 9a275c7

Browse files
committed
remove ref from props except forwardRef
1 parent 0bbb5f8 commit 9a275c7

File tree

7 files changed

+35
-76
lines changed

7 files changed

+35
-76
lines changed

cli/reactjs_jsx_ppx_v3.ml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ let makePropsRecordType propsName loc namedTypeList =
205205
namedTypeList
206206
|> List.map (fun (isOptional, label, _, _interiorType) ->
207207
if label = "key" then Type.field ~loc ~attrs:optionalAttr { txt = label; loc } (keyType Location.none)
208-
else if label = "ref" then Type.field ~loc ~attrs:optionalAttr { txt = label; loc } (refType Location.none)
208+
else if label = "ref" then Type.field ~loc ~attrs:(if isOptional then optionalAttr else []) { txt = label; loc } (refType Location.none)
209209
else if isOptional then Type.field ~loc ~attrs:optionalAttr { txt = label; loc } (Typ.var label)
210210
else Type.field ~loc { txt = label; loc } (Typ.var label))
211211
in
@@ -485,7 +485,7 @@ let jsxMapper () =
485485
(* type props<'id, 'name> = { @optional key: string, @optional id: 'id, ... } *)
486486
let propsRecordType =
487487
makePropsRecordType "props" Location.none
488-
((true, "key", [], keyType pstr_loc) :: (true, "ref", [], refType pstr_loc) :: namedTypeList)
488+
((true, "key", [], keyType pstr_loc) :: namedTypeList)
489489
in
490490
(* can't be an arrow because it will defensively uncurry *)
491491
let newExternalType =
@@ -645,7 +645,8 @@ let jsxMapper () =
645645
(* type props = { ... } *)
646646
let propsRecordType =
647647
makePropsRecordType "props" emptyLoc
648-
((true, "key", [], keyType emptyLoc) :: (true, "ref", [], refType pstr_loc) :: namedTypeList)
648+
(((true, "key", [], keyType emptyLoc) :: namedTypeList)
649+
@ if hasForwardRef then [ (true, "ref", [], refType pstr_loc) ] else [])
649650
in
650651
let innerExpression = if hasForwardRef then
651652
Exp.apply (Exp.ident @@ Location.mknoloc @@ Lident "make")
@@ -693,13 +694,23 @@ let jsxMapper () =
693694
| _ -> (patterns, expr)
694695
in
695696
let patternsWithLid, expression = returnedExpression [] expression in
696-
let pattern = (Pat.record ((List.rev patternsWithLid) @ [(Location.mknoloc (Lident "ref"), Pat.var (Location.mknoloc "ref"))]) Closed)
697+
let patternsWithLid = ((List.rev patternsWithLid)
698+
@
699+
(if hasForwardRef then [(Location.mknoloc (Lident "ref"), Pat.var (Location.mknoloc "ref"))] else []))
700+
in
701+
let pattern = match patternsWithLid with
702+
| [] -> Pat.any ()
703+
| _ -> (Pat.record patternsWithLid Closed)
697704
in
698705
(* add patttern matching for optional prop value *)
699706
let expression = if List.length vbMatchList = 0 then expression else (Exp.let_ Nonrecursive vbMatchList expression) in
700707
(* add let _ = ref to ignore unused warning *)
701-
let expression = Exp.let_ Nonrecursive [ vbIgnoreUnusedRef ] expression in
702-
let expression = Exp.let_ Nonrecursive [ vbRefFromOption ] expression in
708+
let expression = match hasForwardRef with
709+
| true ->
710+
let expression = Exp.let_ Nonrecursive [ vbIgnoreUnusedRef ] expression in
711+
Exp.let_ Nonrecursive [ vbRefFromOption ] expression
712+
| false -> expression
713+
in
703714
let expression = Exp.fun_ Nolabel None
704715
begin
705716
Pat.constraint_ pattern
@@ -776,7 +787,7 @@ let jsxMapper () =
776787
in
777788
let propsRecordType =
778789
makePropsRecordTypeSig "props" Location.none
779-
((true, "key", [], keyType Location.none) :: (true, "ref", [], refType Location.none) :: namedTypeList)
790+
((true, "key", [], keyType Location.none) :: namedTypeList)
780791
in
781792
(* can't be an arrow because it will defensively uncurry *)
782793
let newExternalType =

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
type props<'msg> = {@optional key: string, @optional ref: ReactDOM.Ref.currentDomRef, msg: 'msg} // test React JSX file
1+
type props<'msg> = {@optional key: string, msg: 'msg} // test React JSX file
22

3-
let make = ({msg, ref}: props<'msg>) => {
4-
let ref = Js.Nullable.fromOption(ref)
5-
let _ = ref
6-
7-
{
8-
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
9-
}
3+
let make = ({msg}: props<'msg>) => {
4+
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
105
}
116
let make = {
127
let \"CommentAtTop" = (props: props<_>) => make(props)

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
module Foo = {
2-
type props<'a, 'b> = {
3-
@optional key: string,
4-
@optional ref: ReactDOM.Ref.currentDomRef,
5-
a: 'a,
6-
b: 'b,
7-
}
2+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
83
@module("Foo")
94
external component: React.componentLike<props<'a, 'b>, React.element> = "component"
105
}

tests/ppx/react/expected/innerModule.res.txt

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
11
module Bar = {
2-
type props<'a, 'b> = {
3-
@optional key: string,
4-
@optional ref: ReactDOM.Ref.currentDomRef,
5-
a: 'a,
6-
b: 'b,
7-
}
8-
let make = ({a, b, ref}: props<'a, 'b>) => {
9-
let ref = Js.Nullable.fromOption(ref)
10-
let _ = ref
11-
2+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
3+
let make = ({a, b}: props<'a, 'b>) => {
124
Js.log("This function should be named `InnerModule.react$Bar`")
135
ReactDOMRe.createDOMElementVariadic("div", [])
146
}
157
let make = {
168
let \"InnerModule$Bar" = (props: props<_>) => make(props)
179
\"InnerModule$Bar"
1810
}
19-
type props<'a, 'b> = {
20-
@optional key: string,
21-
@optional ref: ReactDOM.Ref.currentDomRef,
22-
a: 'a,
23-
b: 'b,
24-
}
25-
26-
let component = ({a, b, ref}: props<'a, 'b>) => {
27-
let ref = Js.Nullable.fromOption(ref)
28-
let _ = ref
11+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
2912

13+
let component = ({a, b}: props<'a, 'b>) => {
3014
Js.log("This function should be named `InnerModule.react$Bar$component`")
3115
ReactDOMRe.createDOMElementVariadic("div", [])
3216
}

tests/ppx/react/expected/newtype.res.txt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
type props<'a, 'b, 'c> = {
2-
@optional key: string,
3-
@optional ref: ReactDOM.Ref.currentDomRef,
4-
a: 'a,
5-
b: 'b,
6-
c: 'c,
7-
}
8-
let make = ({ref}: props<'a, 'b, 'c>) => {
9-
let ref = Js.Nullable.fromOption(ref)
10-
let _ = ref
11-
(type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) =>
12-
ReactDOMRe.createDOMElementVariadic("div", [])
13-
}
1+
type props<'a, 'b, 'c> = {@optional key: string, a: 'a, b: 'b, c: 'c}
2+
let make = (_: props<'a, 'b, 'c>, type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) =>
3+
ReactDOMRe.createDOMElementVariadic("div", [])
144
let make = {
155
let \"Newtype" = (props: props<_>) => make(props)
166
\"Newtype"

tests/ppx/react/expected/topLevel.res.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
type props<'a, 'b> = {
2-
@optional key: string,
3-
@optional ref: ReactDOM.Ref.currentDomRef,
4-
a: 'a,
5-
b: 'b,
6-
}
7-
let make = ({a, b, ref}: props<'a, 'b>) => {
8-
let ref = Js.Nullable.fromOption(ref)
9-
let _ = ref
10-
1+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
2+
let make = ({a, b}: props<'a, 'b>) => {
113
Js.log("This function should be named 'TopLevel.react'")
124
ReactDOMRe.createDOMElementVariadic("div", [])
135
}

tests/ppx/react/expected/typeConstraint.res.txt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
type props<'a, 'b> = {
2-
@optional key: string,
3-
@optional ref: ReactDOM.Ref.currentDomRef,
4-
a: 'a,
5-
b: 'b,
6-
}
7-
let make: 'a. (~a: 'a, ~b: 'a, 'a) => React.element = ({ref}: props<'a, 'b>) => {
8-
let ref = Js.Nullable.fromOption(ref)
9-
let _ = ref
10-
(type a): ((~a: a, ~b: a, a) => React.element) =>
11-
(~a, ~b, _) => ReactDOMRe.createDOMElementVariadic("div", [])
12-
}
1+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
2+
let make: 'a. (~a: 'a, ~b: 'a, 'a) => React.element = (_: props<'a, 'b>, type a): (
3+
(~a: a, ~b: a, a) => React.element
4+
) => (~a, ~b, _) => ReactDOMRe.createDOMElementVariadic("div", [])
135
let make = {
146
let \"TypeConstraint" = (props: props<_>) => make(props)
157
\"TypeConstraint"

0 commit comments

Comments
 (0)