Skip to content

Commit f899f94

Browse files
aspeddrozth
authored andcommitted
[DocGen]: Polish (#796)
* emit items from module alias * add error msg * rename fields * remove double name field * add ocaml.text attr * search for all attrs * fix id module * update tests * emit valid id path * update tests
1 parent 58711a0 commit f899f94

File tree

6 files changed

+64
-43
lines changed

6 files changed

+64
-43
lines changed

analysis/src/DocExtraction.ml

+42-23
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ type constructorDoc = {
1313
deprecated: string option;
1414
}
1515

16-
type docsForModuleAlias = {
17-
id: string;
18-
docstring: string list;
19-
name: string;
20-
signature: string;
21-
}
22-
2316
type docItemDetail =
2417
| Record of {fieldDocs: fieldDoc list}
2518
| Variant of {constructorDocs: constructorDoc list}
@@ -41,7 +34,12 @@ type docItem =
4134
(** Additional documentation for constructors and record fields, if available. *)
4235
}
4336
| Module of docsForModule
44-
| ModuleAlias of docsForModuleAlias
37+
| ModuleAlias of {
38+
id: string;
39+
docstring: string list;
40+
name: string;
41+
items: docItem list;
42+
}
4543
and docsForModule = {
4644
id: string;
4745
docstring: string list;
@@ -72,13 +70,13 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
7270
stringifyObject ~startOnNewline:true ~indentation
7371
[
7472
("kind", Some (wrapInQuotes "record"));
75-
( "fieldDocs",
73+
( "items",
7674
Some
7775
(fieldDocs
7876
|> List.map (fun fieldDoc ->
7977
stringifyObject ~indentation:(indentation + 1)
8078
[
81-
("fieldName", Some (wrapInQuotes fieldDoc.fieldName));
79+
("name", Some (wrapInQuotes fieldDoc.fieldName));
8280
( "deprecated",
8381
match fieldDoc.deprecated with
8482
| Some d -> Some (wrapInQuotes d)
@@ -94,14 +92,14 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
9492
stringifyObject ~startOnNewline:true ~indentation
9593
[
9694
("kind", Some (wrapInQuotes "variant"));
97-
( "constructorDocs",
95+
( "items",
9896
Some
9997
(constructorDocs
10098
|> List.map (fun constructorDoc ->
10199
stringifyObject ~startOnNewline:true
102100
~indentation:(indentation + 1)
103101
[
104-
( "constructorName",
102+
( "name",
105103
Some (wrapInQuotes constructorDoc.constructorName) );
106104
( "deprecated",
107105
match constructorDoc.deprecated with
@@ -170,7 +168,12 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
170168
("kind", Some (wrapInQuotes "moduleAlias"));
171169
("name", Some (wrapInQuotes m.name));
172170
("docstrings", Some (stringifyDocstrings m.docstring));
173-
("signature", Some (m.signature |> Json.escape |> wrapInQuotes));
171+
( "items",
172+
Some
173+
(m.items
174+
|> List.map
175+
(stringifyDocItem ~originalEnv ~indentation:(indentation + 1))
176+
|> array) );
174177
]
175178

176179
and stringifyDocsForModule ?(indentation = 0) ~originalEnv (d : docsForModule) =
@@ -225,8 +228,6 @@ let typeDetail typ ~env ~full =
225228
})
226229
| _ -> None
227230

228-
exception Invalid_file_type
229-
230231
let makeId modulePath ~identifier =
231232
identifier :: modulePath |> List.rev |> SharedTypes.ident
232233

@@ -235,7 +236,10 @@ let extractDocs ~path ~debug =
235236
if
236237
FindFiles.isImplementation path = false
237238
&& FindFiles.isInterface path = false
238-
then raise Invalid_file_type;
239+
then (
240+
Printf.printf "error: failed to read %s, expected an .res or .resi file\n"
241+
path;
242+
exit 1);
239243
let path =
240244
if FindFiles.isImplementation path then
241245
let pathAsResi =
@@ -251,7 +255,10 @@ let extractDocs ~path ~debug =
251255
else path
252256
in
253257
match Cmt.loadFullCmtFromPath ~path with
254-
| None -> ()
258+
| None ->
259+
Printf.printf
260+
"error: failed to generate doc for %s, try to build the project\n" path;
261+
exit 1
255262
| Some full ->
256263
let file = full.file in
257264
let structure = file.structure in
@@ -260,7 +267,7 @@ let extractDocs ~path ~debug =
260267
let rec extractDocsForModule ?(modulePath = [env.file.moduleName])
261268
(structure : Module.structure) =
262269
{
263-
id = modulePath |> ident;
270+
id = modulePath |> List.rev |> ident;
264271
docstring = structure.docstring |> List.map String.trim;
265272
name = structure.name;
266273
deprecated = structure.deprecated;
@@ -297,15 +304,27 @@ let extractDocs ~path ~debug =
297304
| Module (Ident p) ->
298305
(* module Whatever = OtherModule *)
299306
let aliasToModule = p |> pathIdentToString in
300-
let modulePath = aliasToModule :: modulePath in
307+
let id =
308+
(modulePath |> List.rev |> List.hd) ^ "." ^ item.name
309+
in
310+
let items =
311+
match
312+
ProcessCmt.fileForModule ~package:full.package
313+
aliasToModule
314+
with
315+
| None -> []
316+
| Some file ->
317+
let docs =
318+
extractDocsForModule ~modulePath:[id] file.structure
319+
in
320+
docs.items
321+
in
301322
Some
302323
(ModuleAlias
303324
{
304-
id = modulePath |> List.rev |> SharedTypes.ident;
305-
signature =
306-
Printf.sprintf "module %s = %s" item.name
307-
aliasToModule;
325+
id;
308326
name = item.name;
327+
items;
309328
docstring = item.docstring |> List.map String.trim;
310329
})
311330
| Module (Structure m) ->

analysis/src/ProcessAttributes.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let rec findDocAttribute attributes =
55
let open Parsetree in
66
match attributes with
77
| [] -> None
8-
| ( {Asttypes.txt = "ocaml.doc" | "ns.doc" | "res.doc"},
8+
| ( {Asttypes.txt = "ocaml.doc" | "ocaml.text" | "ns.doc" | "res.doc"},
99
PStr
1010
[
1111
{

analysis/src/ProcessCmt.ml

+5-3
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,11 @@ and forStructure ~name ~env strItems =
593593
strItems []
594594
in
595595
let attributes =
596-
match strItems with
597-
| {str_desc = Tstr_attribute attribute} :: _ -> [attribute]
598-
| _ -> []
596+
strItems
597+
|> List.filter_map (fun (struc : Typedtree.structure_item) ->
598+
match struc with
599+
| {str_desc = Tstr_attribute attr} -> Some attr
600+
| _ -> None)
599601
in
600602
let docstring = attrsToDocstring attributes in
601603
let deprecated = ProcessAttributes.findDeprecatedAttribute attributes in

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ preferring found resi file for impl: src/DocExtraction2.resi
2121
"docstrings": ["Makerz of stuffz."]
2222
},
2323
{
24-
"id": "InnerModule.DocExtraction2",
24+
"id": "DocExtraction2.InnerModule",
2525
"name": "InnerModule",
2626
"kind": "module",
2727
"items": [

analysis/tests/src/expected/DocExtraction2.resi.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extracting docs for src/DocExtraction2.resi
2020
"docstrings": ["Makerz of stuffz."]
2121
},
2222
{
23-
"id": "InnerModule.DocExtraction2",
23+
"id": "DocExtraction2.InnerModule",
2424
"name": "InnerModule",
2525
"kind": "module",
2626
"items": [

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ extracting docs for src/DocExtractionRes.res
1414
"detail":
1515
{
1616
"kind": "record",
17-
"fieldDocs": [{
18-
"fieldName": "name",
17+
"items": [{
18+
"name": "name",
1919
"optional": false,
2020
"docstrings": ["The name of the stuff."],
2121
"signature": "string"
2222
}, {
23-
"fieldName": "online",
23+
"name": "online",
2424
"optional": false,
2525
"docstrings": ["Whether stuff is online."],
2626
"signature": "bool"
@@ -42,7 +42,7 @@ extracting docs for src/DocExtractionRes.res
4242
"docstrings": ["Stuff goes offline."]
4343
},
4444
{
45-
"id": "SomeInnerModule.DocExtractionRes",
45+
"id": "DocExtractionRes.SomeInnerModule",
4646
"name": "SomeInnerModule",
4747
"kind": "module",
4848
"items": [
@@ -55,19 +55,19 @@ extracting docs for src/DocExtractionRes.res
5555
"detail":
5656
{
5757
"kind": "variant",
58-
"constructorDocs": [
58+
"items": [
5959
{
60-
"constructorName": "Started",
60+
"name": "Started",
6161
"docstrings": ["If this is started or not"],
6262
"signature": "Started(t)"
6363
},
6464
{
65-
"constructorName": "Stopped",
65+
"name": "Stopped",
6666
"docstrings": ["Stopped?"],
6767
"signature": "Stopped"
6868
},
6969
{
70-
"constructorName": "Idle",
70+
"name": "Idle",
7171
"docstrings": ["Now idle."],
7272
"signature": "Idle"
7373
}]
@@ -89,16 +89,16 @@ extracting docs for src/DocExtractionRes.res
8989
}]
9090
},
9191
{
92-
"id": "AnotherModule.DocExtractionRes",
92+
"id": "DocExtractionRes.AnotherModule",
9393
"name": "AnotherModule",
9494
"kind": "module",
9595
"items": [
9696
{
97-
"id": "DocExtractionRes.AnotherModule.SomeInnerModule",
97+
"id": "DocExtractionRes.LinkedModule",
9898
"kind": "moduleAlias",
9999
"name": "LinkedModule",
100100
"docstrings": ["This links another module. Neat."],
101-
"signature": "module LinkedModule = SomeInnerModule"
101+
"items": []
102102
},
103103
{
104104
"id": "DocExtractionRes.AnotherModule.callback",
@@ -123,9 +123,9 @@ extracting docs for src/DocExtractionRes.res
123123
"detail":
124124
{
125125
"kind": "variant",
126-
"constructorDocs": [
126+
"items": [
127127
{
128-
"constructorName": "SomeStuff",
128+
"name": "SomeStuff",
129129
"docstrings": ["This has inline records..."],
130130
"signature": "SomeStuff"
131131
}]
@@ -140,7 +140,7 @@ extracting docs for src/DocExtractionRes.res
140140
}]
141141
},
142142
{
143-
"id": "ModuleWithThingsThatShouldNotBeExported.DocExtractionRes",
143+
"id": "DocExtractionRes.ModuleWithThingsThatShouldNotBeExported",
144144
"name": "ModuleWithThingsThatShouldNotBeExported",
145145
"kind": "module",
146146
"items": [

0 commit comments

Comments
 (0)