forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse.ts
222 lines (190 loc) · 5.6 KB
/
parse.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import {
type ApiModel,
type ApiPackage,
type ApiItem,
ApiItemKind,
ApiDocumentedItem,
type Excerpt,
ExcerptTokenKind,
ApiNameMixin,
type ApiPropertyItem,
type ExcerptToken,
type Parameter,
type ApiFunction,
} from '@microsoft/api-extractor-model';
import type { DocNode, DocParagraph, DocPlainText } from '@microsoft/tsdoc';
import { type Meaning, ModuleSource } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference';
import type { DocBlockJSON } from './tsdoc/CommentBlock.js';
import { createCommentNode } from './tsdoc/index.js';
export function findPackage(model: ApiModel, name: string): ApiPackage | undefined {
return (model.findMembersByName(name)[0] ?? model.findMembersByName(`@discordjs/${name}`)[0]) as
| ApiPackage
| undefined;
}
export function generatePath(items: readonly ApiItem[], version: string) {
let path = '/docs/packages';
for (const item of items) {
switch (item.kind) {
case ApiItemKind.Model:
case ApiItemKind.EntryPoint:
case ApiItemKind.EnumMember:
break;
case ApiItemKind.Package:
path += `/${item.displayName}`;
break;
case ApiItemKind.Function:
// eslint-disable-next-line no-case-declarations
const functionItem = item as ApiFunction;
path += `/${functionItem.displayName}${
functionItem.overloadIndex && functionItem.overloadIndex > 1 ? `:${functionItem.overloadIndex}` : ''
}:${item.kind}`;
break;
case ApiItemKind.Property:
case ApiItemKind.Method:
case ApiItemKind.MethodSignature:
case ApiItemKind.PropertySignature:
// TODO: Take overloads into account
path += `#${item.displayName}:${item.kind}`;
break;
default:
path += `/${item.displayName}:${item.kind}`;
}
}
// eslint-disable-next-line prefer-named-capture-group, unicorn/no-unsafe-regex
return path.replace(/@discordjs\/(.*)\/(.*)?/, `$1/${version}/$2`);
}
export function resolveDocComment(item: ApiDocumentedItem) {
if (!(item instanceof ApiDocumentedItem)) {
return null;
}
const { tsdocComment } = item;
if (!tsdocComment) {
return null;
}
const { summarySection } = tsdocComment;
function recurseNodes(node: DocNode | undefined): string | null {
if (!node) {
return null;
}
switch (node.kind) {
case 'Paragraph':
return recurseNodes(node as DocParagraph);
case 'PlainText':
return (node as DocPlainText).text;
default:
return null;
}
}
return recurseNodes(summarySection);
}
export function findReferences(model: ApiModel, excerpt: Excerpt) {
const retVal: Set<ApiItem> = new Set();
for (const token of excerpt.spannedTokens) {
switch (token.kind) {
case ExcerptTokenKind.Reference: {
const item = model.resolveDeclarationReference(token.canonicalReference!, undefined).resolvedApiItem;
if (!item) {
break;
}
retVal.add(item);
break;
}
default:
break;
}
}
return retVal;
}
export function resolveName(item: ApiItem) {
if (ApiNameMixin.isBaseClassOf(item)) {
return item.name;
}
return item.displayName;
}
export function getProperties(item: ApiItem) {
const properties: ApiPropertyItem[] = [];
for (const member of item.members) {
switch (member.kind) {
case ApiItemKind.Property:
case ApiItemKind.PropertySignature:
case ApiItemKind.Method:
case ApiItemKind.MethodSignature:
properties.push(member as ApiPropertyItem);
break;
default:
break;
}
}
return properties;
}
export interface TokenDocumentation {
kind: string;
path: string | null;
text: string;
}
export interface ParameterDocumentation {
isOptional: boolean;
name: string;
paramCommentBlock: DocBlockJSON | null;
tokens: TokenDocumentation[];
}
function createDapiTypesURL(meaning: Meaning, name: string) {
const base = 'https://discord-api-types.dev/api/discord-api-types-v10';
switch (meaning) {
case 'type':
return `${base}#${name}`;
default:
return `${base}/${meaning}/${name}`;
}
}
export function genReference(item: ApiItem, version: string) {
return {
name: resolveName(item),
path: generatePath(item.getHierarchy(), version),
};
}
export function genToken(model: ApiModel, token: ExcerptToken, version: string) {
if (token.canonicalReference) {
// @ts-expect-error Symbol is not publicly accessible
token.canonicalReference._navigation = '.';
}
if (
token.canonicalReference?.source instanceof ModuleSource &&
token.canonicalReference.symbol &&
token.canonicalReference.source.packageName === 'discord-api-types' &&
token.canonicalReference.symbol.meaning
) {
return {
kind: token.kind,
text: token.text,
path: createDapiTypesURL(token.canonicalReference.symbol.meaning, token.text),
};
}
const item = token.canonicalReference
? model.resolveDeclarationReference(token.canonicalReference, undefined).resolvedApiItem ?? null
: null;
return {
kind: token.kind,
text: token.text,
path: item ? generatePath(item.getHierarchy(), version) : null,
};
}
export function genParameter(model: ApiModel, param: Parameter, version: string): ParameterDocumentation {
return {
name: param.name,
isOptional: param.isOptional,
tokens: param.parameterTypeExcerpt.spannedTokens.map((token) => genToken(model, token, version)),
paramCommentBlock: param.tsdocParamBlock
? (createCommentNode(param.tsdocParamBlock, model, version) as DocBlockJSON)
: null,
};
}
export function getMembers(pkg: ApiPackage, version: string) {
return pkg.members[0]!.members.map((member) => ({
name: member.displayName,
kind: member.kind as string,
path: generatePath(member.getHierarchy(), version),
containerKey: member.containerKey,
overloadIndex: member.kind === 'Function' ? (member as ApiFunction).overloadIndex : null,
}));
}