Skip to content

Commit 0a98c07

Browse files
authored
Make optional props optional properly (#776)
1 parent 9793a5a commit 0a98c07

File tree

10 files changed

+97
-50
lines changed

10 files changed

+97
-50
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
44
# [ReasonReact](https://reasonml.github.io/reason-react/)
55

6-
[![npm version](https://badge.fury.io/js/reason-react.svg)](https://www.npmjs.com/package/reason-react)
7-
![npm](https://img.shields.io/npm/dt/reason-react)
86
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
97
![contributors](https://img.shields.io/github/contributors/reasonml/reason-react)
108
[![discord](https://img.shields.io/discord/235176658175262720.svg?logo=discord&colorb=blue)](https://discord.gg/reasonml)

dune-project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
(>= 1.0.0))
4040
reason-react-ppx
4141
(reason
42-
(>= 3.6.0))
42+
(and (>= 3.6.0) (< 3.10.0)))
4343
(ocaml-lsp-server :with-test)
4444
(merlin
4545
(and
@@ -62,7 +62,7 @@
6262
(ocaml
6363
(>= 4.13.0))
6464
(reason
65-
(>= 3.9.0))
65+
(and (>= 3.6.0) (< 3.10.0)))
6666
(ppxlib
6767
(>= 0.28.0))
6868
(merlin :with-test)

ppx/reason_react_ppx.ml

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ let safeTypeFromValue valueStr =
9999
match String.sub valueStr 0 1 with "_" -> "T" ^ valueStr | _ -> valueStr
100100
[@@raises Invalid_argument]
101101

102-
let keyType loc =
103-
Builder.ptyp_constr ~loc { loc; txt = optionIdent }
104-
[ Builder.ptyp_constr ~loc { loc; txt = Lident "string" } [] ]
102+
let keyType loc = Builder.ptyp_constr ~loc { loc; txt = Lident "string" } []
105103

106104
type 'a children = ListLiteral of 'a | Exact of 'a
107105
type componentConfig = { propsName : string }
@@ -318,21 +316,17 @@ let rec recursivelyMakeNamedArgsForExternal list args =
318316
(match (label, interiorType, default) with
319317
(* ~foo=1 *)
320318
| label, None, Some _ ->
321-
{
322-
ptyp_desc = Ptyp_var (safeTypeFromValue label);
323-
ptyp_loc = loc;
324-
ptyp_loc_stack = [];
325-
ptyp_attributes = [];
326-
}
319+
Builder.ptyp_var ~loc (safeTypeFromValue label)
327320
(* ~foo: int=1 *)
328321
| _label, Some type_, Some _ -> type_
329322
(* ~foo: option(int)=? *)
330-
| ( label,
323+
| ( _label,
331324
Some
332325
{
333326
ptyp_desc = Ptyp_constr ({ txt = Lident "option" }, [ type_ ]);
334327
},
335-
_ )
328+
_ ) ->
329+
Builder.ptyp_constr ~loc { loc; txt = optionIdent } [ type_ ]
336330
| ( label,
337331
Some
338332
{
@@ -341,27 +335,15 @@ let rec recursivelyMakeNamedArgsForExternal list args =
341335
({ txt = Ldot (Lident "*predef*", "option") }, [ type_ ]);
342336
},
343337
_ )
344-
(* ~foo: int=? - note this isnt valid. but we want to get a type
345-
error *)
346-
| label, Some type_, _
347338
when isOptional label ->
348339
type_
340+
(* ~foo: int=? - note this isnt valid. but we want to get a type error *)
341+
| label, Some type_, _ when isOptional label -> type_
349342
(* ~foo=? *)
350343
| label, None, _ when isOptional label ->
351-
{
352-
ptyp_desc = Ptyp_var (safeTypeFromValue label);
353-
ptyp_loc = loc;
354-
ptyp_loc_stack = [];
355-
ptyp_attributes = [];
356-
}
344+
Builder.ptyp_var ~loc (safeTypeFromValue label)
357345
(* ~foo *)
358-
| label, None, _ ->
359-
{
360-
ptyp_desc = Ptyp_var (safeTypeFromValue label);
361-
ptyp_loc_stack = [];
362-
ptyp_loc = loc;
363-
ptyp_attributes = [];
364-
}
346+
| label, None, _ -> Builder.ptyp_var ~loc (safeTypeFromValue label)
365347
| _label, Some type_, _ -> type_)
366348
args)
367349
| [] -> args

ppx/test/ppx.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ function usage() {
77
echo " $(basename "$0") --output ml [file.re]"
88
}
99

10-
# if [ -z "$3" ]; then
11-
# usage
12-
# exit
13-
# fi
10+
if [ -z "$3" ]; then
11+
usage
12+
exit
13+
fi
1414

1515
refmt --parse re --print ml "$3" > output.ml
1616
reason-react-ppx --impl output.ml -o temp.ml

ppx/test/ppx_with_refmt.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.

ppx/test/signature.t/input.re

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Example = {
2+
[@react.component]
3+
external make:
4+
(
5+
~cond: bool,
6+
~noWrap: bool=?,
7+
~href: string,
8+
~id: string=?,
9+
~color: color,
10+
~mode: linkMode=?,
11+
~children: React.element
12+
) =>
13+
React.element;
14+
};
15+
16+
module MyPropIsOptionBool = {
17+
[@react.component] external make: (~myProp: bool=?) => React.element = "A";
18+
};
19+
20+
module MyPropIsOptionOptionBool = {
21+
[@react.component] external make: (~myProp: option(bool)=?) => React.element = "B";
22+
};

ppx/test/signature.t/run.t

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
$ ../ppx.sh --output re input.re
2+
module Example = {
3+
[@bs.obj]
4+
external makeProps:
5+
(
6+
~cond: bool,
7+
~noWrap: bool=?,
8+
~href: string,
9+
~id: string=?,
10+
~color: color,
11+
~mode: linkMode=?,
12+
~children: React.element,
13+
~key: string=?,
14+
unit
15+
) =>
16+
{
17+
.
18+
"cond": bool,
19+
"noWrap": option(bool),
20+
"href": string,
21+
"id": option(string),
22+
"color": color,
23+
"mode": option(linkMode),
24+
"children": React.element,
25+
};
26+
external make:
27+
React.componentLike(
28+
{
29+
.
30+
"cond": bool,
31+
"noWrap": option(bool),
32+
"href": string,
33+
"id": option(string),
34+
"color": color,
35+
"mode": option(linkMode),
36+
"children": React.element,
37+
},
38+
React.element,
39+
);
40+
};
41+
module MyPropIsOptionBool = {
42+
[@bs.obj]
43+
external makeProps:
44+
(~myProp: bool=?, ~key: string=?, unit) => {. "myProp": option(bool)};
45+
external make:
46+
React.componentLike({. "myProp": option(bool)}, React.element) =
47+
"A";
48+
};
49+
module MyPropIsOptionOptionBool = {
50+
[@bs.obj]
51+
external makeProps:
52+
(~myProp: option(bool)=?, ~key: string=?, unit) =>
53+
{. "myProp": option(option(bool))};
54+
external make:
55+
React.componentLike({. "myProp": option(option(bool))}, React.element) =
56+
"B";
57+
};

reason-react-ppx.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ bug-reports: "https://github.com/reasonml/reason-react/issues"
1616
depends: [
1717
"dune" {>= "3.8"}
1818
"ocaml" {>= "4.13.0"}
19-
"reason" {>= "3.9.0"}
19+
"reason" {>= "3.6.0" & < "3.10.0"}
2020
"ppxlib" {>= "0.28.0"}
2121
"merlin" {with-test}
2222
"ocamlformat" {= "0.24.0" & with-dev-setup}

reason-react.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ depends: [
2121
"ocaml" {>= "4.13.0"}
2222
"melange" {>= "1.0.0"}
2323
"reason-react-ppx"
24-
"reason" {>= "3.6.0"}
24+
"reason" {>= "3.6.0" & < "3.10.0"}
2525
"ocaml-lsp-server" {with-test}
2626
"merlin" {= "4.10-414" & with-test}
2727
"opam-check-npm-deps" {= "1.0.0" & with-dev-setup}

website/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ReasonReact Website
1+
# ReasonReact Documentation Website
22

33
This code is used to generate [https://reasonml.github.io/reason-react](https://reasonml.github.io/reason-react). It pulls in files from `../docs` and the current directory to generate the final static html files that's served on the site.
44

0 commit comments

Comments
 (0)