Skip to content

Commit 10421cd

Browse files
committed
avoid list relocation
1 parent 41382f7 commit 10421cd

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

res_syntax/src/reactjs_jsx_v4.ml

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

290290
let makeLabelDecls namedTypeList =
291-
let rec mem_fst x = function
291+
let rec mem_label ((_, la, _, _, _) as x) = function
292292
| [] -> false
293-
| (a, _) :: l -> compare a (fst x) = 0 || mem_fst x l
293+
| (_, lb, _, _, _) :: l -> compare lb la = 0 || mem_label x l
294294
in
295295
let rec duplicated = function
296296
| [] -> 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
297+
| hd :: tl -> if mem_label hd tl then Some hd else duplicated tl
304298
in
299+
(* Check if there are duplicated props in the namedTypeList without making list relocation. *)
300+
let duplicatedProp = namedTypeList |> duplicated in
305301
match duplicatedProp with
306-
| Some (label, loc) ->
307-
React_jsx_common.raiseError ~loc "JSX: found the duplicated prop `%s`" label
302+
| Some _ -> (
303+
(* If there are duplicated props, then find the one from the last. *)
304+
let duplicatedPropAtLast = namedTypeList |> List.rev |> duplicated in
305+
match duplicatedPropAtLast with
306+
| Some (_, label, _, loc, _) ->
307+
React_jsx_common.raiseError ~loc "JSX: found the duplicated prop `%s`"
308+
label
309+
| None ->
310+
(* Never reach here *)
311+
React_jsx_common.raiseError ~loc:Location.none
312+
"JSX: found the duplicated prop")
308313
| None ->
309314
namedTypeList
310315
|> List.map (fun (isOptional, label, attrs, loc, interiorType) ->

0 commit comments

Comments
 (0)