Skip to content

Commit 33b9f5c

Browse files
zthGabriel Nordeborn
authored and
Gabriel Nordeborn
committed
prefer resi file when available, and prefer module signature vs impl
1 parent 9036955 commit 33b9f5c

File tree

7 files changed

+234
-9
lines changed

7 files changed

+234
-9
lines changed

analysis/src/DocExtraction.ml

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,42 @@ and stringifyDocsForModule ?(indentation = 0) ~originalEnv (d : docsForModule) =
212212
|> array) );
213213
]
214214

215+
exception Invalid_file_type
216+
215217
let extractDocs ~path ~debug =
216218
if debug then Printf.printf "extracting docs for %s\n" path;
219+
if
220+
FindFiles.isImplementation path = false
221+
&& FindFiles.isInterface path = false
222+
then raise Invalid_file_type;
223+
let path =
224+
if FindFiles.isImplementation path then
225+
let pathAsResi =
226+
(path |> Filename.dirname) ^ "/"
227+
^ (path |> Filename.basename |> Filename.chop_extension)
228+
^ ".resi"
229+
in
230+
if Sys.file_exists pathAsResi then (
231+
if debug then
232+
Printf.printf "preferring found resi file for impl: %s\n" pathAsResi;
233+
pathAsResi)
234+
else path
235+
else path
236+
in
217237
match Cmt.loadFullCmtFromPath ~path with
218238
| None -> ()
219239
| Some full ->
220240
let file = full.file in
221241
let structure = file.structure in
222-
let env = SharedTypes.QueryEnv.fromFile file in
223-
let rec extractDocs (structure : SharedTypes.Module.structure) =
242+
let open SharedTypes in
243+
let env = QueryEnv.fromFile file in
244+
let rec extractDocs (structure : Module.structure) =
224245
{
225246
docstring = structure.docstring |> List.map String.trim;
226247
name = structure.name;
227248
items =
228249
structure.items
229-
|> List.filter_map (fun (item : SharedTypes.Module.item) ->
250+
|> List.filter_map (fun (item : Module.item) ->
230251
match item.kind with
231252
| Value typ ->
232253
Some
@@ -263,8 +284,7 @@ let extractDocs ~path ~debug =
263284
{
264285
fieldDocs =
265286
fields
266-
|> List.map
267-
(fun (field : SharedTypes.field) ->
287+
|> List.map (fun (field : field) ->
268288
(field.fname.txt, field.docstring));
269289
})
270290
| Some (Tvariant {constructors}) ->
@@ -273,13 +293,17 @@ let extractDocs ~path ~debug =
273293
{
274294
constructorDocs =
275295
constructors
276-
|> List.map
277-
(fun (c : SharedTypes.Constructor.t)
278-
-> (c.cname.txt, c.docstring));
296+
|> List.map (fun (c : Constructor.t) ->
297+
(c.cname.txt, c.docstring));
279298
})
280299
| _ -> None);
281300
})
282-
| Module (Structure m) -> Some (Module (extractDocs m))
301+
| Module (Structure m) ->
302+
(* module Whatever = {} in res or module Whatever: {} in resi. *)
303+
Some (Module (extractDocs m))
304+
| Module (Constraint (Structure _impl, Structure interface)) ->
305+
(* module Whatever: { <interface> } = { <impl> }. Prefer the interface. *)
306+
Some (Module (extractDocs interface))
283307
| _ -> None);
284308
}
285309
in

analysis/tests/src/DocExtraction2.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
type t = string
2+
3+
let getStr = () => "123"
4+
5+
let make = getStr
6+
7+
module InnerModule = {
8+
type t = unit
9+
let make = () => ()
10+
}
11+
12+
// ^dex
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*** Module level doc here.*/
2+
3+
/** Type t is pretty cool.*/
4+
type t
5+
6+
/** Makerz of stuffz. */
7+
let make: unit => t
8+
9+
module InnerModule: {
10+
/*** This inner module is nice...*/
11+
12+
/** This type is also t. */
13+
type t
14+
15+
/** Maker of tea.*/
16+
let make: unit => t
17+
}
18+
19+
// ^dex

analysis/tests/src/DocExtractionRes.res

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,38 @@ module AnotherModule = {
5151
type domRoot = unit => Client.Root.t
5252
}
5353

54+
module ModuleWithThingsThatShouldNotBeExported: {
55+
/*** BROKEN: This docstring isn't picked up Doesn't seem to be parsed at all, no attributes found.*/
56+
57+
/** The type t is stuff. */
58+
type t
59+
60+
/** The maker of stuff!*/
61+
let make: unit => t
62+
} = {
63+
/*** Mighty fine module here too!*/
64+
type t = string
65+
type x = int
66+
type f = bool
67+
68+
let m1 = (x: x) => {
69+
x + 1
70+
}
71+
72+
let m2 = (f: f) =>
73+
if f {
74+
true
75+
} else {
76+
false
77+
}
78+
79+
let make = () => {
80+
if m2(true) && m1(1) > 2 {
81+
"1"
82+
} else {
83+
"2"
84+
}
85+
}
86+
}
87+
5488
// ^dex
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Documentation extraction src/DocExtraction2.res
2+
extracting docs for src/DocExtraction2.res
3+
preferring found resi file for impl: src/DocExtraction2.resi
4+
5+
{
6+
"name": "DocExtraction2",
7+
"docstrings": ["Module level doc here."],
8+
"items": [
9+
{
10+
"kind": "type",
11+
"name": "t",
12+
"signature": "type t",
13+
"docstrings": ["Type t is pretty cool."],
14+
"linkables": []
15+
},
16+
{
17+
"kind": "value",
18+
"name": "make",
19+
"signature": "let make: unit => t",
20+
"docstrings": ["Makerz of stuffz."],
21+
"linkables": [{
22+
"path": "t",
23+
"moduleName": "DocExtraction2",
24+
"external": false
25+
}]
26+
},
27+
{
28+
"kind": "module",
29+
"item":
30+
{
31+
"name": "InnerModule",
32+
"docstrings": [],
33+
"items": [
34+
{
35+
"kind": "type",
36+
"name": "t",
37+
"signature": "type t",
38+
"docstrings": ["This type is also t."],
39+
"linkables": []
40+
},
41+
{
42+
"kind": "value",
43+
"name": "make",
44+
"signature": "let make: unit => t",
45+
"docstrings": ["Maker of tea."],
46+
"linkables": [{
47+
"path": "t",
48+
"moduleName": "DocExtraction2",
49+
"external": false
50+
}]
51+
}]
52+
}
53+
}]
54+
}
55+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Documentation extraction src/DocExtraction2.resi
2+
extracting docs for src/DocExtraction2.resi
3+
4+
{
5+
"name": "DocExtraction2",
6+
"docstrings": ["Module level doc here."],
7+
"items": [
8+
{
9+
"kind": "type",
10+
"name": "t",
11+
"signature": "type t",
12+
"docstrings": ["Type t is pretty cool."],
13+
"linkables": []
14+
},
15+
{
16+
"kind": "value",
17+
"name": "make",
18+
"signature": "let make: unit => t",
19+
"docstrings": ["Makerz of stuffz."],
20+
"linkables": [{
21+
"path": "t",
22+
"moduleName": "DocExtraction2",
23+
"external": false
24+
}]
25+
},
26+
{
27+
"kind": "module",
28+
"item":
29+
{
30+
"name": "InnerModule",
31+
"docstrings": [],
32+
"items": [
33+
{
34+
"kind": "type",
35+
"name": "t",
36+
"signature": "type t",
37+
"docstrings": ["This type is also t."],
38+
"linkables": []
39+
},
40+
{
41+
"kind": "value",
42+
"name": "make",
43+
"signature": "let make: unit => t",
44+
"docstrings": ["Maker of tea."],
45+
"linkables": [{
46+
"path": "t",
47+
"moduleName": "DocExtraction2",
48+
"external": false
49+
}]
50+
}]
51+
}
52+
}]
53+
}
54+

analysis/tests/src/expected/DocExtractionRes.res.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,33 @@ extracting docs for src/DocExtractionRes.res
161161
}]
162162
}]
163163
}
164+
},
165+
{
166+
"kind": "module",
167+
"item":
168+
{
169+
"name": "ModuleWithThingsThatShouldNotBeExported",
170+
"docstrings": [],
171+
"items": [
172+
{
173+
"kind": "type",
174+
"name": "t",
175+
"signature": "type t",
176+
"docstrings": ["The type t is stuff."],
177+
"linkables": []
178+
},
179+
{
180+
"kind": "value",
181+
"name": "make",
182+
"signature": "let make: unit => t",
183+
"docstrings": ["The maker of stuff!"],
184+
"linkables": [{
185+
"path": "t",
186+
"moduleName": "DocExtractionRes",
187+
"external": false
188+
}]
189+
}]
190+
}
164191
}]
165192
}
166193

0 commit comments

Comments
 (0)