Skip to content

Commit 9916513

Browse files
committed
fix issue overlapping argument with default value
, using alias with default value together
1 parent a415dbe commit 9916513

File tree

4 files changed

+54
-37
lines changed

4 files changed

+54
-37
lines changed

res_syntax/src/reactjs_jsx_v4.ml

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,10 @@ let argToType ~newtypes ~(typeConstraints : core_type option) types
729729
:: types
730730
| _ -> types
731731

732-
let argWithDefaultValue (name, default, _, _, _, _) =
733-
match default with
734-
| Some default when isOptional name -> Some (getLabel name, default)
735-
| _ -> None
732+
let hasDefaultValue nameArgList =
733+
nameArgList
734+
|> List.exists (fun (name, default, _, _, _, _) ->
735+
Option.is_some default && isOptional name)
736736

737737
let argToConcreteType types (name, attrs, loc, type_) =
738738
match name with
@@ -1037,26 +1037,34 @@ let transformStructureItem ~config mapper item =
10371037
(argToType ~newtypes ~typeConstraints)
10381038
[] namedArgList
10391039
in
1040-
let namedArgWithDefaultValueList =
1041-
List.filter_map argWithDefaultValue namedArgList
1040+
let vbMatch (name, default, _, alias, loc, _) =
1041+
let label = getLabel name in
1042+
match default with
1043+
| Some default ->
1044+
Vb.mk
1045+
(Pat.var (Location.mkloc alias loc))
1046+
(Exp.match_
1047+
(Exp.ident
1048+
{txt = Ldot (Lident "props", label); loc = Location.none})
1049+
[
1050+
Exp.case
1051+
(Pat.construct
1052+
(Location.mknoloc @@ Lident "Some")
1053+
(Some (Pat.var (Location.mknoloc label))))
1054+
(Exp.ident (Location.mknoloc @@ Lident label));
1055+
Exp.case
1056+
(Pat.construct (Location.mknoloc @@ Lident "None") None)
1057+
default;
1058+
])
1059+
| None ->
1060+
Vb.mk
1061+
(Pat.var (Location.mkloc alias loc))
1062+
(Exp.ident @@ Location.mknoloc @@ Ldot (Lident "props", label))
10421063
in
1043-
let vbMatch (label, default) =
1044-
Vb.mk
1045-
(Pat.var (Location.mknoloc label))
1046-
(Exp.match_
1047-
(Exp.ident {txt = Lident label; loc = Location.none})
1048-
[
1049-
Exp.case
1050-
(Pat.construct
1051-
(Location.mknoloc @@ Lident "Some")
1052-
(Some (Pat.var (Location.mknoloc label))))
1053-
(Exp.ident (Location.mknoloc @@ Lident label));
1054-
Exp.case
1055-
(Pat.construct (Location.mknoloc @@ Lident "None") None)
1056-
default;
1057-
])
1064+
let vbMatchList =
1065+
if hasDefaultValue namedArgList then List.map vbMatch namedArgList
1066+
else []
10581067
in
1059-
let vbMatchList = List.map vbMatch namedArgWithDefaultValueList in
10601068
(* type props = { ... } *)
10611069
let propsRecordType =
10621070
makePropsRecordType ~coreTypeOfAttr ~typVarsOfCoreType "props"
@@ -1184,11 +1192,17 @@ let transformStructureItem ~config mapper item =
11841192
(fun expr (_, pattern) -> Exp.fun_ Nolabel None pattern expr)
11851193
expression patternsWithNolabel
11861194
in
1195+
(* ({a, b, _}: props<'a, 'b>) *)
11871196
let recordPattern =
11881197
match patternsWithLabel with
11891198
| [] -> Pat.any ()
11901199
| _ -> Pat.record (List.rev patternsWithLabel) Open
11911200
in
1201+
let recordPattern =
1202+
if hasDefaultValue namedArgList then
1203+
Pat.var {txt = "props"; loc = emptyLoc}
1204+
else recordPattern
1205+
in
11921206
let expression =
11931207
Exp.fun_ Nolabel None
11941208
(Pat.constraint_ recordPattern

res_syntax/tests/ppx/react/expected/aliasProps.res.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ module C0 = {
44
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
55

66
@react.component
7-
let make = ({priority: _, ?text, _}: props<'priority, 'text>) => {
8-
let text = switch text {
7+
let make = (props: props<'priority, 'text>) => {
8+
let text = switch props.text {
99
| Some(text) => text
1010
| None => "Test"
1111
}
12+
and _ = props.priority
1213

1314
React.string(text)
1415
}
@@ -23,11 +24,12 @@ module C1 = {
2324
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
2425

2526
@react.component
26-
let make = ({priority: p, ?text, _}: props<'priority, 'text>) => {
27-
let text = switch text {
27+
let make = (props: props<'priority, 'text>) => {
28+
let text = switch props.text {
2829
| Some(text) => text
2930
| None => "Test"
3031
}
32+
and p = props.priority
3133

3234
React.string(p ++ text)
3335
}
@@ -42,8 +44,8 @@ module C2 = {
4244
type props<'foo> = {foo?: 'foo}
4345

4446
@react.component
45-
let make = ({foo: ?bar, _}: props<'foo>) => {
46-
let foo = switch foo {
47+
let make = (props: props<'foo>) => {
48+
let bar = switch props.foo {
4749
| Some(foo) => foo
4850
| None => ""
4951
}

res_syntax/tests/ppx/react/expected/defaultValueProp.res.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module C0 = {
22
type props<'a, 'b> = {a?: 'a, b?: 'b}
33
@react.component
4-
let make = ({?a, ?b, _}: props<'a, 'b>) => {
5-
let b = switch b {
4+
let make = (props: props<'a, 'b>) => {
5+
let b = switch props.b {
66
| Some(b) => b
77
| None => a * 2
88
}
9-
and a = switch a {
9+
and a = switch props.a {
1010
| Some(a) => a
1111
| None => 2
1212
}
@@ -23,8 +23,9 @@ module C1 = {
2323
type props<'a, 'b> = {a?: 'a, b: 'b}
2424

2525
@react.component
26-
let make = ({?a, b, _}: props<'a, 'b>) => {
27-
let a = switch a {
26+
let make = (props: props<'a, 'b>) => {
27+
let b = props.b
28+
and a = switch props.a {
2829
| Some(a) => a
2930
| None => 2
3031
}

res_syntax/tests/ppx/react/expected/uncurriedProps.res.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
type props<'a> = {a?: 'a}
33

44
@react.component
5-
let make = ({?a, _}: props<(. unit) => unit>) => {
6-
let a = switch a {
5+
let make = (props: props<(. unit) => unit>) => {
6+
let a = switch props.a {
77
| Some(a) => a
88
| None => (. ()) => ()
99
}
@@ -30,8 +30,8 @@ module Foo = {
3030
type props<'callback> = {callback?: 'callback}
3131

3232
@react.component
33-
let make = ({?callback, _}: props<(. string, bool, bool) => unit>) => {
34-
let callback = switch callback {
33+
let make = (props: props<(. string, bool, bool) => unit>) => {
34+
let callback = switch props.callback {
3535
| Some(callback) => callback
3636
| None => (. _, _, _) => ()
3737
}

0 commit comments

Comments
 (0)