Skip to content

Commit edcf127

Browse files
committed
fix handling default value
1 parent ec49a4b commit edcf127

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

jscomp/test/alias_default_value_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33

44
function Alias_default_value_test$C0(props) {
5-
var b = props.b;
6-
var b$1 = b !== undefined ? b : 2;
75
var a = props.a;
86
var a$1 = a !== undefined ? a : 2;
7+
var b = props.b;
8+
var b$1 = b !== undefined ? b : (a$1 << 1);
99
return a$1 + b$1 | 0;
1010
}
1111

res_syntax/src/reactjs_jsx_v4.ml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,9 +1064,15 @@ let transformStructureItem ~config mapper item =
10641064
(Exp.ident {txt = Lident "props"; loc = Location.none})
10651065
(Location.mknoloc @@ Lident label))
10661066
in
1067-
let vbMatchList =
1068-
if hasDefaultValue namedArgList then List.map vbMatch namedArgList
1069-
else []
1067+
let vbMatchExpr namedArgList expr =
1068+
let rec aux namedArgList =
1069+
match namedArgList with
1070+
| [] -> expr
1071+
| [namedArg] -> Exp.let_ Nonrecursive [vbMatch namedArg] expr
1072+
| namedArg :: rest ->
1073+
Exp.let_ Nonrecursive [vbMatch namedArg] (aux rest)
1074+
in
1075+
aux (List.rev namedArgList)
10701076
in
10711077
(* type props = { ... } *)
10721078
let propsRecordType =
@@ -1186,8 +1192,9 @@ let transformStructureItem ~config mapper item =
11861192
in
11871193
(* add pattern matching for optional prop value *)
11881194
let expression =
1189-
if List.length vbMatchList = 0 then expression
1190-
else Exp.let_ Nonrecursive vbMatchList expression
1195+
if hasDefaultValue namedArgList then
1196+
vbMatchExpr namedArgList expression
1197+
else expression
11911198
in
11921199
(* (ref) => expr *)
11931200
let expression =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ module C0 = {
55

66
@react.component
77
let make = (props: props<'priority, 'text>) => {
8+
let _ = props.priority
89
let text = switch props.text {
910
| Some(text) => text
1011
| None => "Test"
1112
}
12-
and _ = props.priority
1313

1414
React.string(text)
1515
}
@@ -25,11 +25,11 @@ module C1 = {
2525

2626
@react.component
2727
let make = (props: props<'priority, 'text>) => {
28+
let p = props.priority
2829
let text = switch props.text {
2930
| Some(text) => text
3031
| None => "Test"
3132
}
32-
and p = props.priority
3333

3434
React.string(p ++ text)
3535
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ module C0 = {
44

55
@react.component
66
let make = (props: props<'a, 'b>) => {
7+
let a = switch props.a {
8+
| Some(a) => a
9+
| None => 2
10+
}
711
let b = switch props.b {
812
| Some(b) => b
913
| None => a * 2
1014
}
11-
and a = switch props.a {
12-
| Some(a) => a
13-
| None => 2
14-
}
1515

1616
React.int(a + b)
1717
}
@@ -27,11 +27,11 @@ module C1 = {
2727

2828
@react.component
2929
let make = (props: props<'a, 'b>) => {
30-
let b = props.b
31-
and a = switch props.a {
30+
let a = switch props.a {
3231
| Some(a) => a
3332
| None => 2
3433
}
34+
let b = props.b
3535

3636
React.int(a + b)
3737
}

0 commit comments

Comments
 (0)