Skip to content

tools: add content for local module alias #904

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
97 changes: 68 additions & 29 deletions tools/src/tools.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type docItem =
signature: string;
name: string;
deprecated: string option;
source: source;
source: source option;
}
| Type of {
id: string;
Expand All @@ -40,23 +40,23 @@ type docItem =
name: string;
deprecated: string option;
detail: docItemDetail option;
source: source;
source: source option;
(** Additional documentation for constructors and record fields, if available. *)
}
| Module of docsForModule
| ModuleAlias of {
id: string;
docstring: string list;
name: string;
source: source;
source: source option;
items: docItem list;
}
and docsForModule = {
id: string;
docstring: string list;
deprecated: string option;
name: string;
source: source;
source: source option;
items: docItem list;
}

Expand Down Expand Up @@ -163,7 +163,10 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
| None -> None );
("signature", Some (signature |> String.trim |> wrapInQuotes));
("docstrings", Some (stringifyDocstrings docstring));
("source", Some (stringifySource ~indentation:(indentation + 1) source));
( "source",
match source with
| Some s -> Some (stringifySource ~indentation:(indentation + 1) s)
| None -> None );
]
| Type {id; docstring; signature; name; deprecated; detail; source} ->
stringifyObject ~startOnNewline:true ~indentation
Expand All @@ -177,7 +180,10 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
| None -> None );
("signature", Some (signature |> wrapInQuotes));
("docstrings", Some (stringifyDocstrings docstring));
("source", Some (stringifySource ~indentation:(indentation + 1) source));
( "source",
match source with
| Some s -> Some (stringifySource ~indentation:(indentation + 1) s)
| None -> None );
( "detail",
match detail with
| None -> None
Expand All @@ -196,7 +202,9 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
| None -> None );
("docstrings", Some (stringifyDocstrings m.docstring));
( "source",
Some (stringifySource ~indentation:(indentation + 1) m.source) );
match m.source with
| Some s -> Some (stringifySource ~indentation:(indentation + 1) s)
| None -> None );
( "items",
Some
(m.items
Expand All @@ -212,7 +220,9 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
("name", Some (wrapInQuotes m.name));
("docstrings", Some (stringifyDocstrings m.docstring));
( "source",
Some (stringifySource ~indentation:(indentation + 1) m.source) );
match m.source with
| Some s -> Some (stringifySource ~indentation:(indentation + 1) s)
| None -> None );
( "items",
Some
(m.items
Expand All @@ -231,7 +241,10 @@ and stringifyDocsForModule ?(indentation = 0) ~originalEnv (d : docsForModule) =
| Some d -> Some (wrapInQuotes d)
| None -> None );
("docstrings", Some (stringifyDocstrings d.docstring));
("source", Some (stringifySource ~indentation:(indentation + 1) d.source));
( "source",
match d.source with
| Some s -> Some (stringifySource ~indentation:(indentation + 1) s)
| None -> None );
( "items",
Some
(d.items
Expand Down Expand Up @@ -280,14 +293,17 @@ let typeDetail typ ~env ~full =
let makeId modulePath ~identifier =
identifier :: modulePath |> List.rev |> SharedTypes.ident

let getSource ~rootPath ({loc_start} : Location.t) =
let line, col = Pos.ofLexing loc_start in
let filepath =
Files.relpath rootPath loc_start.pos_fname
|> Files.split Filename.dir_sep
|> String.concat "/"
in
{filepath; line = line + 1; col = col + 1}
let getSource ~rootPath ({loc_start; loc_ghost} : Location.t) =
match loc_ghost with
| true -> None
| false ->
let line, col = Pos.ofLexing loc_start in
let filepath =
Files.relpath rootPath loc_start.pos_fname
|> Files.split Filename.dir_sep
|> String.concat "/"
in
Some {filepath; line = line + 1; col = col + 1}

let extractDocs ~entryPointFile ~debug =
let path =
Expand Down Expand Up @@ -337,17 +353,18 @@ let extractDocs ~entryPointFile ~debug =
name = structure.name;
deprecated = structure.deprecated;
source =
{
filepath =
(match rootPath = "." with
| true -> file.uri |> Uri.toPath
| false ->
Files.relpath rootPath (file.uri |> Uri.toPath)
|> Files.split Filename.dir_sep
|> String.concat "/");
line = 1;
col = 1;
};
Some
{
filepath =
(match rootPath = "." with
| true -> file.uri |> Uri.toPath
| false ->
Files.relpath rootPath (file.uri |> Uri.toPath)
|> Files.split Filename.dir_sep
|> String.concat "/");
line = 1;
col = 1;
};
items =
structure.items
|> List.filter_map (fun (item : Module.item) ->
Expand Down Expand Up @@ -390,7 +407,29 @@ let extractDocs ~entryPointFile ~debug =
ProcessCmt.fileForModule ~package:full.package
aliasToModule
with
| None -> ([], [])
| None -> (
let localModuleAliasDocs =
match
Exported.find env.exported Module aliasToModule
with
| None -> None
| Some stamp -> (
match
SharedTypes.Stamps.findModule full.file.stamps
stamp
with
| None -> None
| Some {item} -> (
match item with
| Structure struc ->
Some
(extractDocsForModule ~modulePath:[id]
struc)
| _ -> None))
in
match localModuleAliasDocs with
| Some {items; docstring} -> (items, docstring)
| None -> ([], []))
| Some file ->
let docs =
extractDocsForModule ~modulePath:[id]
Expand Down
9 changes: 9 additions & 0 deletions tools/tests/src/Aliases.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@warning("-60")

module Exn = Js.Exn

module UtilsPromises = {
include Js.Promise2

module OldPromise = Js.Promise
}
159 changes: 159 additions & 0 deletions tools/tests/src/expected/Aliases.res.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@

{
"name": "Aliases",
"docstrings": [],
"source": {
"filepath": "src/Aliases.res",
"line": 1,
"col": 1
},
"items": [
{
"id": "Aliases.Exn",
"kind": "moduleAlias",
"name": "Exn",
"docstrings": [],
"source": {
"filepath": "src/Aliases.res",
"line": 3,
"col": 8
},
"items": []
},
{
"id": "Aliases.UtilsPromises",
"name": "UtilsPromises",
"kind": "module",
"docstrings": [],
"source": {
"filepath": "src/Aliases.res",
"line": 5,
"col": 8
},
"items": [
{
"id": "Aliases.UtilsPromises.t",
"kind": "type",
"name": "t",
"signature": "type t<'a> = promise<'a>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.error",
"kind": "type",
"name": "error",
"signature": "type error = Js_promise2.error",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.then",
"kind": "value",
"name": "then",
"signature": "let then: (promise<'a>, 'a => promise<'b>) => promise<'b>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.catch",
"kind": "value",
"name": "catch",
"signature": "let catch: (promise<'a>, error => promise<'a>) => promise<'a>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.make",
"kind": "value",
"name": "make",
"signature": "let make: (\n (~resolve: 'a => unit, ~reject: exn => unit) => unit,\n) => promise<'a>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.resolve",
"kind": "value",
"name": "resolve",
"signature": "let resolve: 'a => promise<'a>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.reject",
"kind": "value",
"name": "reject",
"signature": "let reject: exn => promise<'a>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.all",
"kind": "value",
"name": "all",
"signature": "let all: array<promise<'a>> => promise<array<'a>>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.all2",
"kind": "value",
"name": "all2",
"signature": "let all2: ((promise<'a0>, promise<'a1>)) => promise<('a0, 'a1)>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.all3",
"kind": "value",
"name": "all3",
"signature": "let all3: (\n (promise<'a0>, promise<'a1>, promise<'a2>),\n) => promise<('a0, 'a1, 'a2)>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.all4",
"kind": "value",
"name": "all4",
"signature": "let all4: (\n (promise<'a0>, promise<'a1>, promise<'a2>, promise<'a3>),\n) => promise<('a0, 'a1, 'a2, 'a3)>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.all5",
"kind": "value",
"name": "all5",
"signature": "let all5: (\n (\n promise<'a0>,\n promise<'a1>,\n promise<'a2>,\n promise<'a3>,\n promise<'a4>,\n ),\n) => promise<('a0, 'a1, 'a2, 'a3, 'a4)>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.all6",
"kind": "value",
"name": "all6",
"signature": "let all6: (\n (\n promise<'a0>,\n promise<'a1>,\n promise<'a2>,\n promise<'a3>,\n promise<'a4>,\n promise<'a5>,\n ),\n) => promise<('a0, 'a1, 'a2, 'a3, 'a4, 'a5)>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.race",
"kind": "value",
"name": "race",
"signature": "let race: array<promise<'a>> => promise<'a>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.unsafe_async",
"kind": "value",
"name": "unsafe_async",
"signature": "let unsafe_async: 'a => promise<'a>",
"docstrings": []
},
{
"id": "Aliases.UtilsPromises.unsafe_await",
"kind": "value",
"name": "unsafe_await",
"signature": "let unsafe_await: promise<'a> => 'a",
"docstrings": []
},
{
"id": "Aliases.OldPromise",
"kind": "moduleAlias",
"name": "OldPromise",
"docstrings": [],
"source": {
"filepath": "src/Aliases.res",
"line": 8,
"col": 10
},
"items": []
}]
}]
}
Loading