Skip to content

Commit 25bb7af

Browse files
aspeddrojfrolich
authored andcommitted
add moduletypeid field for modules with explicitly annotated module type (rescript-lang#1019)
* add `moduletype` field for modules with explicitly annotated module type * add full id * remove modulePath * update CHANGELOG.md * fix moduletypeid * ignore parent module name * add tests * remove comment
1 parent 746c2e3 commit 25bb7af

File tree

6 files changed

+147
-5
lines changed

6 files changed

+147
-5
lines changed

tools/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
## master
1414

15+
#### :rocket: New Feature
16+
17+
- Add `moduletypeid` field for explicitly annotated module type. https://github.com/rescript-lang/rescript-vscode/pull/1019
18+
1519
### :bug: Bug Fix
1620

1721
- Print module structure with signature to module path. https://github.com/rescript-lang/rescript-vscode/pull/1018

tools/npm/Tools_Docgen.res

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type rec item =
5656
docstrings: array<string>,
5757
deprecated?: string,
5858
name: string,
59+
moduletypeid?: string,
5960
source: source,
6061
items: array<item>,
6162
})

tools/npm/Tools_Docgen.resi

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type rec item =
5555
docstrings: array<string>,
5656
deprecated?: string,
5757
name: string,
58+
moduletypeid?: string,
5859
source: source,
5960
items: array<item>,
6061
})

tools/src/tools.ml

+40-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ type docItem =
4444
(** Additional documentation for constructors and record fields, if available. *)
4545
}
4646
| Module of docsForModule
47-
| ModuleType of docsForModule
47+
| ModuleType of {
48+
id: string;
49+
docstring: string list;
50+
deprecated: string option;
51+
name: string;
52+
source: source;
53+
items: docItem list;
54+
}
4855
| ModuleAlias of {
4956
id: string;
5057
docstring: string list;
@@ -57,6 +64,7 @@ and docsForModule = {
5764
docstring: string list;
5865
deprecated: string option;
5966
name: string;
67+
moduletypeid: string option;
6068
source: source;
6169
items: docItem list;
6270
}
@@ -195,6 +203,10 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
195203
match m.deprecated with
196204
| Some d -> Some (wrapInQuotes d)
197205
| None -> None );
206+
( "moduletypeid",
207+
match m.moduletypeid with
208+
| Some path -> Some (wrapInQuotes path)
209+
| None -> None );
198210
("docstrings", Some (stringifyDocstrings m.docstring));
199211
( "source",
200212
Some (stringifySource ~indentation:(indentation + 1) m.source) );
@@ -356,6 +368,7 @@ let extractDocs ~entryPointFile ~debug =
356368
id = modulePath |> List.rev |> ident;
357369
docstring = structure.docstring |> List.map String.trim;
358370
name = structure.name;
371+
moduletypeid = None;
359372
deprecated = structure.deprecated;
360373
source =
361374
{
@@ -439,6 +452,7 @@ let extractDocs ~entryPointFile ~debug =
439452
{
440453
id = modulePath |> List.rev |> ident;
441454
name = m.name;
455+
moduletypeid = None;
442456
docstring = item.docstring @ m.docstring;
443457
deprecated = item.deprecated;
444458
source;
@@ -469,12 +483,33 @@ let extractDocs ~entryPointFile ~debug =
469483
(extractDocsForModule
470484
~modulePath:(interface.name :: modulePath)
471485
interface))
472-
| Module {type_ = Constraint (Structure m, Ident _)} ->
473-
(* module M: T = { }. Print M *)
486+
| Module {type_ = Constraint (Structure m, Ident p)} ->
487+
(* module M: T = { <impl> }. Print M *)
488+
let docs =
489+
extractDocsForModule ~modulePath:(m.name :: modulePath)
490+
m
491+
in
492+
let identModulePath = p |> Path.head |> Ident.name in
493+
494+
let moduleTypeIdPath =
495+
match
496+
ProcessCmt.fileForModule ~package:full.package
497+
identModulePath
498+
|> Option.is_none
499+
with
500+
| false -> []
501+
| true -> [modulePath |> List.rev |> List.hd]
502+
in
503+
474504
Some
475505
(Module
476-
(extractDocsForModule
477-
~modulePath:(m.name :: modulePath) m))
506+
{
507+
docs with
508+
moduletypeid =
509+
Some
510+
(makeId ~identifier:(Path.name p)
511+
moduleTypeIdPath);
512+
})
478513
| _ -> None);
479514
}
480515
in

tools/tests/src/DocExtractionRes.res

+14
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,18 @@ module M: Example = {
128128
let f = (x: int) => x
129129
}
130130

131+
module type MT = {
132+
let x: int
133+
}
134+
135+
module A: MT = {
136+
let x = 42
137+
}
138+
139+
module C = {
140+
module D: MT = {
141+
let x = 42
142+
}
143+
}
144+
131145
// ^dex

tools/tests/src/expected/DocExtractionRes.res.json

+87
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
"id": "DocExtractionRes.M",
312312
"name": "M",
313313
"kind": "module",
314+
"moduletypeid": "DocExtractionRes.Example",
314315
"docstrings": ["implementation of Example module type"],
315316
"source": {
316317
"filepath": "src/DocExtractionRes.res",
@@ -342,5 +343,91 @@
342343
"col": 7
343344
}
344345
}]
346+
},
347+
{
348+
"id": "DocExtractionRes.MT",
349+
"name": "MT",
350+
"kind": "moduleType",
351+
"docstrings": [],
352+
"source": {
353+
"filepath": "src/DocExtractionRes.res",
354+
"line": 131,
355+
"col": 13
356+
},
357+
"items": [
358+
{
359+
"id": "DocExtractionRes.MT.x",
360+
"kind": "value",
361+
"name": "x",
362+
"signature": "let x: int",
363+
"docstrings": [],
364+
"source": {
365+
"filepath": "src/DocExtractionRes.res",
366+
"line": 132,
367+
"col": 3
368+
}
369+
}]
370+
},
371+
{
372+
"id": "DocExtractionRes.A",
373+
"name": "A",
374+
"kind": "module",
375+
"moduletypeid": "DocExtractionRes.MT",
376+
"docstrings": [],
377+
"source": {
378+
"filepath": "src/DocExtractionRes.res",
379+
"line": 1,
380+
"col": 1
381+
},
382+
"items": [
383+
{
384+
"id": "DocExtractionRes.A.x",
385+
"kind": "value",
386+
"name": "x",
387+
"signature": "let x: int",
388+
"docstrings": [],
389+
"source": {
390+
"filepath": "src/DocExtractionRes.res",
391+
"line": 136,
392+
"col": 7
393+
}
394+
}]
395+
},
396+
{
397+
"id": "DocExtractionRes.C",
398+
"name": "C",
399+
"kind": "module",
400+
"docstrings": [],
401+
"source": {
402+
"filepath": "src/DocExtractionRes.res",
403+
"line": 139,
404+
"col": 8
405+
},
406+
"items": [
407+
{
408+
"id": "DocExtractionRes.C.D",
409+
"name": "D",
410+
"kind": "module",
411+
"moduletypeid": "DocExtractionRes.MT",
412+
"docstrings": [],
413+
"source": {
414+
"filepath": "src/DocExtractionRes.res",
415+
"line": 1,
416+
"col": 1
417+
},
418+
"items": [
419+
{
420+
"id": "DocExtractionRes.C.D.x",
421+
"kind": "value",
422+
"name": "x",
423+
"signature": "let x: int",
424+
"docstrings": [],
425+
"source": {
426+
"filepath": "src/DocExtractionRes.res",
427+
"line": 141,
428+
"col": 9
429+
}
430+
}]
431+
}]
345432
}]
346433
}

0 commit comments

Comments
 (0)