1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
module Entry = struct
type t =
| Unhandled of
{ original_index : int
; sexp : Sexp.t
; source : string
}
| Re_export of
{ name : Dune.Library.Name.t
; source : string
}
| Library of
{ name : Dune.Library.Name.t
; source : string
}
[@@deriving sexp_of]
let library name = Library { name; source = Dune.Library.Name.to_string name }
let re_export name =
Re_export
{ name
; source = Printf.sprintf "(re_export %s)" (Dune.Library.Name.to_string name)
}
;;
let unhandled ~original_index ~sexp =
Unhandled { original_index; sexp; source = Sexp.to_string_hum sexp }
;;
module For_sort = struct
let compare t1 t2 =
match t1, t2 with
| Unhandled { original_index = i1; _ }, Unhandled { original_index = i2; _ } ->
Int.compare i1 i2
| Unhandled _, (Library _ | Re_export _) -> 1
| (Library _ | Re_export _), Unhandled _ -> -1
| ( (Library { name = n1; _ } | Re_export { name = n1; _ })
, (Library { name = n2; _ } | Re_export { name = n2; _ }) ) ->
Dune.Library.Name.compare n1 n2
;;
end
let library_name = function
| Unhandled { original_index = _; sexp = _; source = _ } -> None
| Re_export { name; source = _ } -> Some name
| Library { name; source = _ } -> Some name
;;
end
module Section = struct
type t = { mutable entries : Entry.t list } [@@deriving sexp_of]
end
type t = { mutable sections : Section.t list } [@@deriving sexp_of]
let create ~libraries =
match List.map libraries ~f:Entry.library with
| [] -> { sections = [] }
| _ :: _ as entries -> { sections = [ { entries } ] }
;;
let field_name = "libraries"
let is_empty t = List.for_all t.sections ~f:(fun section -> List.is_empty section.entries)
let entries t = List.concat_map t.sections ~f:(fun section -> section.entries)
let mem t ~library =
List.exists t.sections ~f:(fun section ->
List.exists section.entries ~f:(function
| Unhandled _ -> false
| Re_export { name; _ } | Library { name; _ } ->
Dune.Library.Name.equal name library))
;;
let dedup_and_sort t =
let names = Hash_set.create (module Dune.Library.Name) in
List.iter t.sections ~f:(fun section ->
let entries =
List.dedup_and_sort section.entries ~compare:Entry.For_sort.compare
|> List.filter ~f:(fun (entry : Entry.t) ->
match entry with
| Unhandled _ -> true
| Re_export { name; _ } | Library { name; _ } ->
let present = Hash_set.mem names name in
Hash_set.add names name;
not present)
in
section.entries <- entries)
;;
let add_entries t ~entries =
let names = Hash_set.create (module Dune.Library.Name) in
List.iter t.sections ~f:(fun section ->
List.iter section.entries ~f:(function
| Unhandled _ -> ()
| Re_export { name; _ } | Library { name; _ } -> Hash_set.add names name));
let section =
match List.last t.sections with
| Some section -> section
| None ->
let section = { Section.entries = [] } in
t.sections <- [ section ];
section
in
section.entries
<- section.entries
@ List.filter_map entries ~f:(fun entry ->
match (entry : Entry.t) with
| Unhandled _ -> None
| Re_export { name; _ } | Library { name; _ } ->
if Hash_set.mem names name
then None
else (
Hash_set.add names name;
Some entry))
;;
let add_libraries t ~libraries =
add_entries t ~entries:(List.map libraries ~f:Entry.library)
;;
let read ~sexps_rewriter ~field =
let sections =
Dunolinter.Sections_handler.read_sections
~field_name
~sexps_rewriter
~field
~f:(fun ~original_index ~loc:_ ~source ~arg ->
match arg with
| Atom name -> Entry.Library { name = Dune.Library.Name.v name; source }
| List [ Atom "re_export"; Atom name ] ->
Entry.Re_export { name = Dune.Library.Name.v name; source }
| List _ as sexp -> Entry.Unhandled { original_index; sexp; source })
|> List.map ~f:(fun entries -> { Section.entries })
in
{ sections }
;;
let write (t : t) =
Sexp.List
(Atom field_name
:: List.concat_map t.sections ~f:(fun section ->
List.map section.entries ~f:(function
| Library { name; _ } -> Sexp.Atom (Dune.Library.Name.to_string name)
| Re_export { name; _ } ->
Sexp.List [ Atom "re_export"; Atom (Dune.Library.Name.to_string name) ]
| Unhandled { original_index = _; sexp; source = _ } -> sexp)))
;;
let rewrite t ~sexps_rewriter ~field =
let write_arg = function
| Entry.Library { name = _; source } -> source
| Entry.Re_export { name = _; source } -> source
| Entry.Unhandled { original_index = _; sexp = _; source } -> source
in
Dunolinter.Sections_handler.rewrite_sections
~field_name
~sexps_rewriter
~field
~write_arg
~sections:(List.map t.sections ~f:(fun { entries } -> entries))
;;
type predicate = Nothing.t
let eval _t ~predicate =
match[@coverage off] (predicate : predicate) with
| x -> Nothing.unreachable_code x
;;
let enforce =
Dunolinter.Linter.enforce
(module Nothing)
~eval
~enforce:(fun _ predicate ->
match[@coverage off] predicate with
| T x | Not x -> Nothing.unreachable_code x)
;;
module Private = struct
module Entry = Entry
end