Skip to content

Commit 2749941

Browse files
authored
Merge pull request #4128 from rickyvetter/bs_4099
[rrppx] Add error when using non-optional type to define an optional arg
2 parents 0af0b6c + 57e0e0f commit 2749941

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

jscomp/syntax/reactjs_jsx_ppx.cppo.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,19 @@ let jsxMapper () =
509509
| Pexp_fun (Labelled "ref", _, _, _)
510510
| Pexp_fun (Optional "ref", _, _, _) -> raise (Invalid_argument "Ref cannot be passed as a normal prop. Please use `forwardRef` API instead.")
511511
| Pexp_fun (arg, default, pattern, expression) when isOptional arg || isLabelled arg ->
512+
let () =
513+
(match (isOptional arg, pattern, default) with
514+
| (true, { ppat_desc = Ppat_constraint (_, { ptyp_desc })}, None) ->
515+
(match ptyp_desc with
516+
| Ptyp_constr({txt=(Lident "option")}, [{ ptyp_desc }]) -> ()
517+
| _ ->
518+
let currentType = (match ptyp_desc with
519+
| Ptyp_constr({txt}, []) -> String.concat "." (Longident.flatten txt)
520+
| Ptyp_constr({txt}, _innerTypeArgs) -> String.concat "." (Longident.flatten txt) ^ "(...)"
521+
| _ -> "...")
522+
in
523+
Location.raise_errorf ~loc:pattern.ppat_loc "ReasonReact: optional argument annotations must have explicit `option`. Did you mean `option(%s)=?`?" currentType)
524+
| _ -> ()) in
512525
let alias = (match pattern with
513526
| {ppat_desc = Ppat_alias (_, {txt}) | Ppat_var {txt}} -> txt
514527
| {ppat_desc = Ppat_any} -> "_"

0 commit comments

Comments
 (0)