Skip to content

Commit f99f00f

Browse files
authored
Remove duplicate types and class types signature items (#105)
* Update class and class types tests * Add remove and has functions in Sig_item_map module * Remove unused remove function for Sig_item_map module * Add a changelog entry * Fix changelog entry
1 parent 80637c4 commit f99f00f

File tree

8 files changed

+27
-28
lines changed

8 files changed

+27
-28
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
### Fixed
1515

1616
- Ignore hidden signature items (#102, @NchamJosephMuam)
17+
- Remove duplicate items in class and class types (#105, @azzsal)
1718

1819
### Removed
1920

lib/diff.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,21 @@ let extract_items items =
6464
| Sig_value (id, val_des, Exported) ->
6565
Sig_item_map.add ~name:(Ident.name id) Sig_item_map.Value val_des tbl
6666
| Sig_type (id, type_decl, _, Exported) ->
67-
Sig_item_map.add ~name:(Ident.name id) Sig_item_map.Type
68-
(type_decl, id) tbl
67+
if
68+
Sig_item_map.has ~name:(Ident.name id) Sig_item_map.Class tbl
69+
|| Sig_item_map.has ~name:(Ident.name id) Sig_item_map.Classtype tbl
70+
then tbl
71+
else
72+
Sig_item_map.add ~name:(Ident.name id) Sig_item_map.Type
73+
(type_decl, id) tbl
6974
| Sig_class (id, cls_decl, _, Exported) ->
7075
Sig_item_map.add ~name:(Ident.name id) Sig_item_map.Class cls_decl tbl
7176
| Sig_class_type (id, class_type_decl, _, Exported) ->
72-
Sig_item_map.add ~name:(Ident.name id) Sig_item_map.Classtype
73-
class_type_decl tbl
77+
if Sig_item_map.has ~name:(Ident.name id) Sig_item_map.Class tbl then
78+
tbl
79+
else
80+
Sig_item_map.add ~name:(Ident.name id) Sig_item_map.Classtype
81+
class_type_decl tbl
7482
| _ -> tbl)
7583
Sig_item_map.empty items
7684

lib/sig_item_map.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ let add (type a) ~name (item_type : a item_type) (item : a) maps : t =
4242
class_type_map = String_map.add name item maps.class_type_map;
4343
}
4444

45+
let has (type a) ~name (item_type : a item_type) maps : bool =
46+
match item_type with
47+
| Value -> String_map.mem name maps.values_map
48+
| Module -> String_map.mem name maps.modules_map
49+
| Modtype -> String_map.mem name maps.modtypes_map
50+
| Type -> String_map.mem name maps.types_map
51+
| Class -> String_map.mem name maps.class_map
52+
| Classtype -> String_map.mem name maps.class_type_map
53+
4554
type ('a, 'diff) diff_item =
4655
'a item_type -> string -> 'a option -> 'a option -> 'diff option
4756

lib/sig_item_map.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type _ item_type =
1212

1313
val empty : t
1414
val add : name:string -> 'a item_type -> 'a -> t -> t
15+
val has : name:string -> 'a item_type -> t -> bool
1516

1617
type ('a, 'diff) diff_item =
1718
'a item_type -> string -> 'a option -> 'a option -> 'diff option

tests/api-diff/class_detection.t

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ Compile the new .mli file to a .cmi file
3232
Run api-diff and check the output
3333
$ api-diff ref_class.cmi add_class.cmi
3434
diff module Add_class:
35-
+type add_class = < calculate : float -> float >
3635
+class add_class : object method calculate : float -> float end
37-
+class type add_class = object method calculate : float -> float end
3836

3937
[1]
4038

@@ -50,8 +48,6 @@ Compile the new .mli file to a .cmi file
5048
Run api-diff and check the output
5149
$ api-diff ref_class.cmi remove_class.cmi
5250
diff module Remove_class:
53-
-type ref_class = < get : int; set : int -> unit >
5451
-class ref_class : object method get : int method set : int -> unit end
55-
-class type ref_class = object method get : int method set : int -> unit end
5652

5753
[1]

tests/api-diff/cltype_tests.t

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ Compile the new .mli file to a .cmi file
3333
Run api-diff and check the output
3434
$ api-diff ref_cltype.cmi add_cltype.cmi
3535
diff module Add_cltype:
36-
+type new_cltype = < mk : int -> unit; mn : int -> int >
3736
+class type new_cltype =
3837
+ object method mk : int -> unit method mn : int -> int end
3938

@@ -51,7 +50,6 @@ Compile the new .mli file to a .cmi file
5150
Run api-diff and check the output
5251
$ api-diff ref_cltype.cmi remove_cltype.cmi
5352
diff module Remove_cltype:
54-
-type ref_cltype = < m1 : string; m2 : string -> unit >
5553
-class type ref_cltype =
5654
- object method m1 : string method m2 : string -> unit end
5755

tests/api-watch/test_diff_class.ml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ let%expect_test "Class Addition" =
3434
in
3535
Format.printf "%a" pp_diff_option result;
3636
[%expect
37-
{|
38-
Some (Module Main: {Modified (Supported [ Type (cls3, Added);
39-
Class (cls3, Added);
40-
Class_type (cls3, Added)])})
41-
|}]
37+
{| Some (Module Main: {Modified (Supported [ Class (cls3, Added)])}) |}]
4238
with e ->
4339
Format.printf "Error: %s" (Printexc.to_string e);
4440
[%expect.unreachable]
@@ -68,11 +64,7 @@ let%expect_test "Class Removal" =
6864
in
6965
Format.printf "%a" pp_diff_option result;
7066
[%expect
71-
{|
72-
Some (Module Main: {Modified (Supported [ Type (cls2, Removed);
73-
Class (cls2, Removed);
74-
Class_type (cls2, Removed)])})
75-
|}]
67+
{| Some (Module Main: {Modified (Supported [ Class (cls2, Removed)])}) |}]
7668
with e ->
7769
Format.printf "Error: %s" (Printexc.to_string e);
7870
[%expect.unreachable]

tests/api-watch/test_diff_cltype.ml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ let%expect_test "Class type addition" =
1616
let result = Diff.interface ~module_name:"Main" ~reference ~current in
1717
Format.printf "%a" pp_diff_option result;
1818
[%expect
19-
{|
20-
Some (Module Main: {Modified (Supported [ Type (cltype, Added);
21-
Class_type (cltype, Added)])})
22-
|}]
19+
{| Some (Module Main: {Modified (Supported [ Class_type (cltype, Added)])}) |}]
2320

2421
let%expect_test "Class type removal" =
2522
let reference =
@@ -36,7 +33,4 @@ let%expect_test "Class type removal" =
3633
let result = Diff.interface ~module_name:"Main" ~reference ~current in
3734
Format.printf "%a" pp_diff_option result;
3835
[%expect
39-
{|
40-
Some (Module Main: {Modified (Supported [ Type (cltype, Removed);
41-
Class_type (cltype, Removed)])})
42-
|}]
36+
{| Some (Module Main: {Modified (Supported [ Class_type (cltype, Removed)])}) |}]

0 commit comments

Comments
 (0)