Skip to content

Commit b825937

Browse files
committed
find the duplicated props in ppx JSX4
1 parent ced073d commit b825937

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

res_syntax/src/reactjs_jsx_v4.ml

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,35 @@ let makePropsTypeParams ?(stripExplicitOption = false)
288288
else Some interiorType)
289289

290290
let makeLabelDecls namedTypeList =
291-
namedTypeList
292-
|> List.map (fun (isOptional, label, attrs, loc, interiorType) ->
293-
if label = "key" then
294-
Type.field ~loc ~attrs:(optionalAttrs @ attrs) {txt = label; loc}
295-
interiorType
296-
else if isOptional then
297-
Type.field ~loc ~attrs:(optionalAttrs @ attrs) {txt = label; loc}
298-
(Typ.var @@ safeTypeFromValue @@ Labelled label)
299-
else
300-
Type.field ~loc ~attrs {txt = label; loc}
301-
(Typ.var @@ safeTypeFromValue @@ Labelled label))
291+
let rec mem_fst x = function
292+
| [] -> false
293+
| (a, _) :: l -> compare a (fst x) = 0 || mem_fst x l
294+
in
295+
let rec duplicated = function
296+
| [] -> None
297+
| hd :: tl -> if mem_fst hd tl then Some hd else duplicated tl
298+
in
299+
let duplicatedProp =
300+
(* Find the duplicated prop from the back *)
301+
namedTypeList |> List.rev
302+
|> List.map (fun (_, label, _, loc, _) -> (label, loc))
303+
|> duplicated
304+
in
305+
match duplicatedProp with
306+
| Some (label, loc) ->
307+
React_jsx_common.raiseError ~loc "JSX: Duplicated prop %s" label
308+
| None ->
309+
namedTypeList
310+
|> List.map (fun (isOptional, label, attrs, loc, interiorType) ->
311+
if label = "key" then
312+
Type.field ~loc ~attrs:(optionalAttrs @ attrs) {txt = label; loc}
313+
interiorType
314+
else if isOptional then
315+
Type.field ~loc ~attrs:(optionalAttrs @ attrs) {txt = label; loc}
316+
(Typ.var @@ safeTypeFromValue @@ Labelled label)
317+
else
318+
Type.field ~loc ~attrs {txt = label; loc}
319+
(Typ.var @@ safeTypeFromValue @@ Labelled label))
302320

303321
let makeTypeDecls propsName loc namedTypeList =
304322
let labelDeclList = makeLabelDecls namedTypeList in

0 commit comments

Comments
 (0)