-
Notifications
You must be signed in to change notification settings - Fork 621
/
check_docs.ts
586 lines (552 loc) · 16.3 KB
/
check_docs.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/**
* This script checks that all public symbols documentation aligns with the
* {@link ./CONTRIBUTING.md#documentation | documentation guidelines}.
*
* TODO(lucacasonato): Add support for variables, interfaces, namespaces, and type aliases.
*/
import {
type ClassConstructorDef,
type ClassMethodDef,
type ClassPropertyDef,
doc,
type DocNode,
type DocNodeBase,
type DocNodeClass,
type DocNodeFunction,
type DocNodeInterface,
type DocNodeModuleDoc,
type JsDoc,
type JsDocTagDocRequired,
type JsDocTagParam,
type Location,
type TsTypeDef,
} from "@deno/doc";
import { pooledMap } from "@std/async/pool";
type DocNodeWithJsDoc<T = DocNodeBase> = T & {
jsDoc: JsDoc;
};
const ENTRY_POINTS = [
"../archive/mod.ts",
"../assert/mod.ts",
"../assert/unstable_never.ts",
"../async/mod.ts",
"../bytes/mod.ts",
"../cache/mod.ts",
"../cbor/mod.ts",
"../cli/mod.ts",
"../cli/unstable_spinner.ts",
"../crypto/mod.ts",
"../collections/mod.ts",
"../csv/mod.ts",
"../csv/unstable_stringify.ts",
"../data_structures/mod.ts",
"../data_structures/unstable_bidirectional_map.ts",
"../datetime/mod.ts",
"../dotenv/mod.ts",
"../encoding/mod.ts",
"../encoding/unstable_base64_stream.ts",
"../encoding/unstable_base32hex_stream.ts",
"../encoding/unstable_base32_stream.ts",
"../encoding/unstable_base64url_stream.ts",
"../encoding/unstable_base32hex_stream.ts",
"../encoding/unstable_hex_stream.ts",
"../expect/mod.ts",
"../fmt/bytes.ts",
"../fmt/colors.ts",
"../fmt/duration.ts",
"../fmt/printf.ts",
"../front_matter/mod.ts",
"../front_matter/unstable_yaml.ts",
"../fs/mod.ts",
"../html/mod.ts",
"../html/unstable_is_valid_custom_element_name.ts",
"../http/mod.ts",
"../http/unstable_header.ts",
"../http/unstable_method.ts",
"../http/unstable_signed_cookie.ts",
"../ini/mod.ts",
"../internal/mod.ts",
"../io/mod.ts",
"../json/mod.ts",
"../jsonc/mod.ts",
"../log/base_handler.ts",
"../log/file_handler.ts",
"../log/warn.ts",
"../log/critical.ts",
"../log/debug.ts",
"../log/error.ts",
"../log/info.ts",
"../log/console_handler.ts",
"../log/formatters.ts",
"../log/get_logger.ts",
"../log/logger.ts",
"../media_types/mod.ts",
"../msgpack/mod.ts",
"../net/mod.ts",
"../net/unstable_get_network_address.ts",
"../path/mod.ts",
"../path/unstable_basename.ts",
"../path/unstable_dirname.ts",
"../path/unstable_extname.ts",
"../path/unstable_join.ts",
"../path/unstable_normalize.ts",
"../path/posix/mod.ts",
"../path/windows/mod.ts",
"../random/mod.ts",
"../regexp/mod.ts",
"../semver/mod.ts",
"../streams/mod.ts",
"../streams/unstable_fixed_chunk_stream.ts",
"../streams/unstable_to_lines.ts",
"../streams/unstable_to_bytes.ts",
"../tar/mod.ts",
"../text/mod.ts",
"../text/unstable_slugify.ts",
"../text/unstable_to_constant_case.ts",
"../testing/bdd.ts",
"../testing/mock.ts",
"../testing/snapshot.ts",
"../testing/time.ts",
"../testing/types.ts",
"../toml/mod.ts",
"../ulid/mod.ts",
"../uuid/mod.ts",
"../uuid/unstable_v7.ts",
"../webgpu/mod.ts",
"../yaml/mod.ts",
] as const;
const TS_SNIPPET = /```ts[\s\S]*?```/g;
const ASSERTION_IMPORT =
/from "@std\/(assert(\/[a-z-]+)?|testing\/(mock|snapshot|types))"/g;
const NEWLINE = "\n";
const diagnostics: DocumentError[] = [];
const snippetPromises: (() => Promise<void>)[] = [];
class DocumentError extends Error {
constructor(
message: string,
document: { location: Location },
) {
super(message, {
cause: `${document.location.filename}:${document.location.line}`,
});
this.name = this.constructor.name;
}
}
function assert(
condition: boolean,
message: string,
document: { location: Location },
) {
if (!condition) {
diagnostics.push(new DocumentError(message, document));
}
}
function isExported(document: DocNodeBase) {
return document.declarationKind === "export";
}
function isVoidOrPromiseVoid(returnType: TsTypeDef) {
return isVoid(returnType) ||
(returnType.kind === "typeRef" &&
returnType.typeRef.typeName === "Promise" &&
returnType.typeRef.typeParams?.length === 1 &&
isVoid(returnType.typeRef.typeParams[0]!));
}
function isTypeAsserts(returnType: TsTypeDef) {
return returnType.kind === "typePredicate" &&
returnType.typePredicate.asserts;
}
function isVoid(returnType: TsTypeDef) {
return returnType.kind === "keyword" && returnType.keyword === "void";
}
function assertHasReturnTag(document: { jsDoc: JsDoc; location: Location }) {
const tag = document.jsDoc.tags?.find((tag) => tag.kind === "return");
if (tag === undefined) {
diagnostics.push(
new DocumentError("Symbol must have a @return or @returns tag", document),
);
} else {
assert(
// @ts-ignore doc is defined
tag.doc !== undefined,
"@return tag must have a description",
document,
);
}
}
/**
* Asserts that a @param tag has a corresponding function definition.
*/
function assertHasParamDefinition(
document: DocNodeWithJsDoc<DocNodeFunction | ClassMethodDef>,
param: JsDocTagParam,
) {
const paramDoc = document.functionDef.params.find((paramDoc) => {
if (paramDoc.kind === "identifier") {
return paramDoc.name === param.name;
} else if (paramDoc.kind === "rest" && paramDoc.arg.kind === "identifier") {
return paramDoc.arg.name === param.name;
} else if (
paramDoc.kind === "assign" && paramDoc.left.kind === "identifier"
) {
return paramDoc.left.name === param.name;
}
return false;
});
if (!paramDoc) {
diagnostics.push(
new DocumentError(
`@param ${param.name} must have a corresponding named function parameter definition.`,
document,
),
);
}
}
function assertHasParamTag(
document: { jsDoc: JsDoc; location: Location },
param: string,
) {
const tag = document.jsDoc.tags?.find((tag) =>
tag.kind === "param" && tag.name === param
);
if (!tag) {
diagnostics.push(
new DocumentError(`Symbol must have a @param tag for ${param}`, document),
);
} else {
assert(
// @ts-ignore doc is defined
tag.doc !== undefined,
`@param tag for ${param} must have a description`,
document,
);
}
}
function assertSnippetsWork(
doc: string,
document: { jsDoc: JsDoc; location: Location },
required = true,
) {
const snippets = doc.match(TS_SNIPPET);
if (snippets === null) {
if (required) {
diagnostics.push(
new DocumentError(
"@example tag must have a TypeScript code snippet",
document,
),
);
return;
} else {
return;
}
}
for (let snippet of snippets) {
const delim = snippet.split(NEWLINE)[0];
// Trim the code block delimiters
snippet = snippet.split(NEWLINE).slice(1, -1).join(NEWLINE);
if (!(delim?.includes("no-assert") || delim?.includes("ignore"))) {
assert(
snippet.match(ASSERTION_IMPORT) !== null,
"Snippet must contain assertion from '@std/assert'",
document,
);
}
}
}
function assertHasExampleTag(
document: { jsDoc: JsDoc; location: Location },
) {
const tags = document.jsDoc.tags?.filter((tag) =>
tag.kind === "example"
) as JsDocTagDocRequired[];
if (tags === undefined || tags.length === 0) {
diagnostics.push(
new DocumentError("Symbol must have an @example tag", document),
);
return;
}
for (const tag of tags) {
assert(
tag.doc !== undefined,
"@example tag must have a title and TypeScript code snippet",
document,
);
/**
* Otherwise, if the example title is undefined, it is given the title
* "Example #" by default.
*/
assert(
!tag.doc.startsWith("```ts"),
"@example tag must have a title",
document,
);
assertSnippetsWork(tag.doc, document);
}
}
function assertHasTypeParamTags(
document: { jsDoc: JsDoc; location: Location },
typeParamName: string,
) {
const tag = document.jsDoc.tags?.find((tag) =>
tag.kind === "template" && tag.name === typeParamName
);
if (tag === undefined) {
diagnostics.push(
new DocumentError(
`Symbol must have a @typeParam tag for ${typeParamName}`,
document,
),
);
} else {
assert(
// @ts-ignore doc is defined
tag.doc !== undefined,
`@typeParam tag for ${typeParamName} must have a description`,
document,
);
}
}
/**
* Asserts that a function document has:
* - A `@typeParam` tag for each type parameter.
* - A {@linkcode https://jsdoc.app/tags-param | @param} tag for each parameter.
* - A parameter definition inside the function for each @param tag.
* - A {@linkcode https://jsdoc.app/tags-returns | @returns} tag.
* - At least one {@linkcode https://jsdoc.app/tags-example | @example} tag with
* a code snippet that executes successfully.
*/
function assertFunctionDocs(
document: DocNodeWithJsDoc<DocNodeFunction | ClassMethodDef>,
) {
assertSnippetsWork(document.jsDoc.doc!, document, false);
for (const param of document.functionDef.params) {
if (param.kind === "identifier") {
assertHasParamTag(document, param.name);
}
if (param.kind === "rest" && param.arg.kind === "identifier") {
assertHasParamTag(document, param.arg.name);
}
if (param.kind === "assign" && param.left.kind === "identifier") {
assertHasParamTag(document, param.left.name);
}
}
const documentedParams = document.jsDoc.tags?.filter((
tag,
): tag is JsDocTagParam =>
// Filter nested definitions like options.root as it is still documenting options parameter
tag.kind === "param" && !tag.name.includes(".")
) ?? [];
for (const param of documentedParams) {
assertHasParamDefinition(document, param);
}
for (const typeParam of document.functionDef.typeParams) {
assertHasTypeParamTags(document, typeParam.name);
}
if (
document.functionDef.returnType !== undefined &&
!isVoidOrPromiseVoid(document.functionDef.returnType) &&
!isTypeAsserts(document.functionDef.returnType)
) {
assertHasReturnTag(document);
}
assertHasExampleTag(document);
}
/**
* Asserts that a class document has:
* - A `@typeParam` tag for each type parameter.
* - At least one {@linkcode https://jsdoc.app/tags-example | @example} tag with
* a code snippet that executes successfully.
* - Documentation on all properties, methods, and constructors.
*/
function assertClassDocs(document: DocNodeWithJsDoc<DocNodeClass>) {
assertSnippetsWork(document.jsDoc.doc!, document, false);
for (const typeParam of document.classDef.typeParams) {
assertHasTypeParamTags(document, typeParam.name);
}
assertHasExampleTag(document);
for (const property of document.classDef.properties) {
if (property.jsDoc === undefined) continue; // this is caught by `deno doc --lint`
if (property.accessibility !== undefined) {
diagnostics.push(
new DocumentError(
"Do not use `public`, `protected`, or `private` fields in classes",
property,
),
);
continue;
}
assertClassPropertyDocs(
property as DocNodeWithJsDoc<ClassPropertyDef>,
);
}
for (const method of document.classDef.methods) {
if (method.jsDoc === undefined) continue; // this is caught by `deno doc --lint`
if (method.accessibility !== undefined) {
diagnostics.push(
new DocumentError(
"Do not use `public`, `protected`, or `private` methods in classes",
method,
),
);
}
assertFunctionDocs(method as DocNodeWithJsDoc<ClassMethodDef>);
}
for (const constructor of document.classDef.constructors) {
if (constructor.jsDoc === undefined) continue; // this is caught by `deno doc --lint`
if (constructor.accessibility !== undefined) {
diagnostics.push(
new DocumentError(
"Do not use `public`, `protected`, or `private` constructors in classes",
constructor,
),
);
}
assertConstructorDocs(
constructor as DocNodeWithJsDoc<ClassConstructorDef>,
);
}
}
/**
* Asserts that a class property document has:
* - At least one {@linkcode https://jsdoc.app/tags-example | @example} tag with
* a code snippet that executes successfully.
*/
function assertClassPropertyDocs(
property: DocNodeWithJsDoc<ClassPropertyDef>,
) {
assertHasExampleTag(property);
}
/**
* Checks a constructor document for:
* - No TypeScript parameters marked with `public`, `protected`, or `private`.
* - A {@linkcode https://jsdoc.app/tags-param | @param} tag for each parameter.
* - At least one {@linkcode https://jsdoc.app/tags-example | @example} tag with
* a code snippet that executes successfully.
*/
function assertConstructorDocs(
constructor: DocNodeWithJsDoc<ClassConstructorDef>,
) {
for (const param of constructor.params) {
assert(
param.accessibility === undefined,
"Do not use `public`, `protected`, or `private` parameters in constructors",
constructor,
);
if (param.kind === "identifier") {
assertHasParamTag(constructor, param.name);
}
if (param.kind === "assign" && param.left.kind === "identifier") {
assertHasParamTag(constructor, param.left.name);
}
}
}
/**
* Checks a module document for:
* - Code snippets that execute successfully.
*/
function assertModuleDoc(document: DocNodeWithJsDoc<DocNodeModuleDoc>) {
assertSnippetsWork(document.jsDoc.doc!, document);
}
/**
* Ensures an interface document:
* - Has `@default` tags for all optional properties.
*/
// deno-lint-ignore no-unused-vars
function assertHasDefaultTags(document: DocNodeWithJsDoc<DocNodeInterface>) {
for (const prop of document.interfaceDef.properties) {
if (!prop.optional) continue;
if (!prop.jsDoc?.tags?.find((tag) => tag.kind === "default")) {
diagnostics.push(
new DocumentError(
"Optional interface properties should have default values",
document,
),
);
}
}
}
// deno-lint-ignore no-unused-vars
function assertInterfaceDocs(document: DocNodeWithJsDoc<DocNodeInterface>) {
// TODO(iuioiua): This is currently disabled deliberately, as it throws errors
// for interface properties that don't have a `@default` tag. Re-enable this
// when checking for `@default` tags again, or when a solution is found for
// ignoring some properties (those that don't require a `@default` tag).
// assertHasDefaultTags(document);
}
function resolve(specifier: string, referrer: string): string {
if (specifier.startsWith("@std/")) {
specifier = specifier.replace("@std/", "../").replaceAll("-", "_");
const parts = specifier.split("/");
if (parts.length === 2) {
specifier += "/mod.ts";
} else if (parts.length > 2) {
specifier += ".ts";
}
}
return new URL(specifier, referrer).href;
}
async function checkDocs(specifier: string) {
const docs = await doc(specifier, { resolve });
for (const d of docs.filter(isExported)) {
if (d.jsDoc === undefined) continue; // this is caught by other checks
const document = d as DocNodeWithJsDoc<DocNode>;
switch (document.kind) {
case "moduleDoc": {
assertModuleDoc(document);
break;
}
case "function": {
assertFunctionDocs(document);
break;
}
case "class": {
assertClassDocs(document);
break;
}
case "interface":
assertInterfaceDocs(document);
}
}
}
const ENTRY_POINT_URLS = ENTRY_POINTS.map((entry) =>
new URL(entry, import.meta.url).href
);
const lintStatus = await new Deno.Command(Deno.execPath(), {
args: ["doc", "--lint", ...ENTRY_POINT_URLS],
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
}).output();
if (!lintStatus.success) {
// deno-lint-ignore no-console
console.error(
`%c[error] %c'deno doc --lint' failed`,
"color: red",
"",
);
Deno.exit(1);
}
await Promise.all(ENTRY_POINT_URLS.map(checkDocs));
const iter = pooledMap(
navigator.hardwareConcurrency,
snippetPromises,
(fn) => fn(),
);
for await (const _ of iter) {
// noop
}
if (diagnostics.length > 0) {
for (const error of diagnostics) {
// deno-lint-ignore no-console
console.error(
`%c[error] %c${error.message} %cat ${error.cause}`,
"color: red",
"",
"color: gray",
);
}
// deno-lint-ignore no-console
console.log(`%c${diagnostics.length} errors found`, "color: red");
Deno.exit(1);
}