2 * Copyright (c) 2019, Facebook, Inc.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
13 let id_if_string expr
: 'a
option =
14 let (pos
, expr_
) = expr
in
16 | String s
-> Some
(pos
, s
)
19 let check_duplicates (ids
: Aast.sid list
) : unit =
21 List.fold ids ~init
:SMap.empty ~f
:(fun acc
(pos
, name
) ->
22 (match SMap.find_opt name acc
with
23 | Some prev_pos
-> Errors.repeated_record_field name pos prev_pos
25 SMap.add name pos acc
)
31 inherit Nast_visitor.handler_base
as super
33 (* Ban duplicate fields in declarations: record Foo { x: int, x: int } *)
34 method! at_record_def _env rd
=
35 let field_names = List.map rd
.rd_fields ~f
:(fun (id
, _, _) -> id
) in
36 check_duplicates field_names
38 (* Ban duplicate fields in instantiations: Foo['x' => 1, 'x' => 2]; *)
39 method! at_expr env
(pos
, e
) =
41 | Aast.Record
(_, fields
) ->
43 List.map fields ~f
:(fun (id
, _) -> id_if_string id
) |> List.filter_opt
45 check_duplicates field_names
46 | _ -> super#at_expr env
(pos
, e
)