Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add moduletypeid field for modules with explicitly annotated module type #1019

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tools/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

## master

#### :rocket: New Feature

- Add `moduletypeid` field for explicitly annotated module type. https://github.com/rescript-lang/rescript-vscode/pull/1019

### :bug: Bug Fix

- Print module structure with signature to module path. https://github.com/rescript-lang/rescript-vscode/pull/1018
Expand Down
1 change: 1 addition & 0 deletions tools/npm/Tools_Docgen.res
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type rec item =
docstrings: array<string>,
deprecated?: string,
name: string,
moduletypeid?: string,
source: source,
items: array<item>,
})
Expand Down
1 change: 1 addition & 0 deletions tools/npm/Tools_Docgen.resi
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type rec item =
docstrings: array<string>,
deprecated?: string,
name: string,
moduletypeid?: string,
source: source,
items: array<item>,
})
Expand Down
45 changes: 40 additions & 5 deletions tools/src/tools.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ type docItem =
(** Additional documentation for constructors and record fields, if available. *)
}
| Module of docsForModule
| ModuleType of docsForModule
| ModuleType of {
id: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you update the rescript types accordingly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstring: string list;
deprecated: string option;
name: string;
source: source;
items: docItem list;
}
| ModuleAlias of {
id: string;
docstring: string list;
Expand All @@ -57,6 +64,7 @@ and docsForModule = {
docstring: string list;
deprecated: string option;
name: string;
moduletypeid: string option;
source: source;
items: docItem list;
}
Expand Down Expand Up @@ -195,6 +203,10 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
match m.deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
( "moduletypeid",
match m.moduletypeid with
| Some path -> Some (wrapInQuotes path)
| None -> None );
("docstrings", Some (stringifyDocstrings m.docstring));
( "source",
Some (stringifySource ~indentation:(indentation + 1) m.source) );
Expand Down Expand Up @@ -356,6 +368,7 @@ let extractDocs ~entryPointFile ~debug =
id = modulePath |> List.rev |> ident;
docstring = structure.docstring |> List.map String.trim;
name = structure.name;
moduletypeid = None;
deprecated = structure.deprecated;
source =
{
Expand Down Expand Up @@ -439,6 +452,7 @@ let extractDocs ~entryPointFile ~debug =
{
id = modulePath |> List.rev |> ident;
name = m.name;
moduletypeid = None;
docstring = item.docstring @ m.docstring;
deprecated = item.deprecated;
source;
Expand Down Expand Up @@ -469,12 +483,33 @@ let extractDocs ~entryPointFile ~debug =
(extractDocsForModule
~modulePath:(interface.name :: modulePath)
interface))
| Module {type_ = Constraint (Structure m, Ident _)} ->
(* module M: T = { }. Print M *)
| Module {type_ = Constraint (Structure m, Ident p)} ->
(* module M: T = { <impl> }. Print M *)
let docs =
extractDocsForModule ~modulePath:(m.name :: modulePath)
m
in
let identModulePath = p |> Path.head |> Ident.name in

let moduleTypeIdPath =
match
ProcessCmt.fileForModule ~package:full.package
identModulePath
|> Option.is_none
with
| false -> []
| true -> [modulePath |> List.rev |> List.hd]
in

Some
(Module
(extractDocsForModule
~modulePath:(m.name :: modulePath) m))
{
docs with
moduletypeid =
Some
(makeId ~identifier:(Path.name p)
moduleTypeIdPath);
})
| _ -> None);
}
in
Expand Down
14 changes: 14 additions & 0 deletions tools/tests/src/DocExtractionRes.res
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,18 @@ module M: Example = {
let f = (x: int) => x
}

module type MT = {
let x: int
}

module A: MT = {
let x = 42
}

module C = {
module D: MT = {
let x = 42
}
}

// ^dex
87 changes: 87 additions & 0 deletions tools/tests/src/expected/DocExtractionRes.res.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
"id": "DocExtractionRes.M",
"name": "M",
"kind": "module",
"moduletypeid": "DocExtractionRes.Example",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@woeps, same id of module type

"docstrings": ["implementation of Example module type"],
"source": {
"filepath": "src/DocExtractionRes.res",
Expand Down Expand Up @@ -342,5 +343,91 @@
"col": 7
}
}]
},
{
"id": "DocExtractionRes.MT",
"name": "MT",
"kind": "moduleType",
"docstrings": [],
"source": {
"filepath": "src/DocExtractionRes.res",
"line": 131,
"col": 13
},
"items": [
{
"id": "DocExtractionRes.MT.x",
"kind": "value",
"name": "x",
"signature": "let x: int",
"docstrings": [],
"source": {
"filepath": "src/DocExtractionRes.res",
"line": 132,
"col": 3
}
}]
},
{
"id": "DocExtractionRes.A",
"name": "A",
"kind": "module",
"moduletypeid": "DocExtractionRes.MT",
"docstrings": [],
"source": {
"filepath": "src/DocExtractionRes.res",
"line": 1,
"col": 1
},
"items": [
{
"id": "DocExtractionRes.A.x",
"kind": "value",
"name": "x",
"signature": "let x: int",
"docstrings": [],
"source": {
"filepath": "src/DocExtractionRes.res",
"line": 136,
"col": 7
}
}]
},
{
"id": "DocExtractionRes.C",
"name": "C",
"kind": "module",
"docstrings": [],
"source": {
"filepath": "src/DocExtractionRes.res",
"line": 139,
"col": 8
},
"items": [
{
"id": "DocExtractionRes.C.D",
"name": "D",
"kind": "module",
"moduletypeid": "DocExtractionRes.MT",
"docstrings": [],
"source": {
"filepath": "src/DocExtractionRes.res",
"line": 1,
"col": 1
},
"items": [
{
"id": "DocExtractionRes.C.D.x",
"kind": "value",
"name": "x",
"signature": "let x: int",
"docstrings": [],
"source": {
"filepath": "src/DocExtractionRes.res",
"line": 141,
"col": 9
}
}]
}]
}]
}