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

Commit 5c112cb

Browse files
committed
add pattern constraint in destructuring args
1 parent 021922b commit 5c112cb

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

cli/reactjs_jsx_ppx_v4.ml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,20 @@ let transformComponentDefinition nestedModules mapper structure returnStructures
985985
| Pexp_fun (arg_label, _default, {ppat_loc}, expr) ->
986986
if isLabelled arg_label || isOptional arg_label then
987987
returnedExpression
988-
(( {loc = ppat_loc; txt = Lident (getLabel arg_label)},
989-
Pat.var {txt = getLabel arg_label; loc = ppat_loc} )
988+
((if isOptional arg_label then
989+
(* { name: @optional name } *)
990+
( {loc = ppat_loc; txt = Lident (getLabel arg_label)},
991+
Pat.constraint_
992+
(Pat.var {txt = getLabel arg_label; loc = ppat_loc})
993+
(Typ.constr ~attrs:optionalAttr
994+
{
995+
txt = Lident (getLabel arg_label);
996+
loc = Location.none;
997+
}
998+
[]) )
999+
else
1000+
( {loc = ppat_loc; txt = Lident (getLabel arg_label)},
1001+
Pat.var {txt = getLabel arg_label; loc = ppat_loc} ))
9901002
:: patterns)
9911003
expr
9921004
else returnedExpression patterns expr
@@ -1005,7 +1017,7 @@ let transformComponentDefinition nestedModules mapper structure returnStructures
10051017
let pattern =
10061018
match patternsWithLid with
10071019
| [] -> Pat.any ()
1008-
| _ -> Pat.record patternsWithLid Closed
1020+
| _ -> Pat.record patternsWithLid Open
10091021
in
10101022
(* add patttern matching for optional prop value *)
10111023
let expression =

0 commit comments

Comments
 (0)