-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathapidocs-util.ts
75 lines (70 loc) · 1.85 KB
/
apidocs-util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* (c) 2021, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import {
ApiDocsContent,
ApiDocsEntry,
ApiDocsResponse,
} from "../../language-server/apidocs";
export const pullModulesToTop = (input: ApiDocsContent) => {
const recurse = (docs: ApiDocsEntry[], topLevel: boolean) => {
let removedSoFar = 0;
[...docs].forEach((d, index) => {
if (d.kind === "module" && !topLevel) {
input.content[d.fullName] = d;
docs.splice(index - removedSoFar, 1);
removedSoFar++;
}
if (d.children) {
recurse(d.children, false);
}
});
};
recurse(Object.values(input.content), true);
};
export const resolveModule = (
docs: ApiDocsResponse,
name: string
): ApiDocsEntry | undefined => {
return Object.values(docs)
.filter(
(module) =>
name === module.fullName || name.startsWith(module.fullName + ".")
)
.reduce(
(acc: ApiDocsEntry | undefined, curr) =>
// Longest match wins.
!acc || acc.fullName.length < curr.fullName.length ? curr : acc,
undefined
);
};
export const moduleAndApiFromId = (id: string) => {
const idSegments = id.split(".");
const pythonModuleName = idSegments[0];
const apiId = idSegments.slice(1).join(".");
return {
pythonModuleName,
apiId,
};
};
export const filterOutUndocumentedBuiltins = (input: ApiDocsContent) => {
const recursiveFilter = (
entries: ApiDocsEntry[] | undefined
): ApiDocsEntry[] | undefined => {
if (!entries) {
return;
}
return entries.filter((entry) => {
const hasDocstring = !!entry.docString;
if (hasDocstring) {
entry.children = recursiveFilter(entry.children);
}
return hasDocstring;
});
};
input.content.builtins.children = recursiveFilter(
input.content.builtins.children
);
};