2024-04-22 07:07:34 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
/**
|
2024-05-21 05:34:08 +00:00
|
|
|
* This script checks that all public symbols documentation aligns with the
|
|
|
|
* {@link ./CONTRIBUTING.md#documentation | documentation guidelines}.
|
2024-04-22 07:07:34 +00:00
|
|
|
*
|
2024-05-21 14:09:42 +00:00
|
|
|
* TODO(lucacasonato): Add support for variables, interfaces, namespaces, and type aliases.
|
2024-04-22 07:07:34 +00:00
|
|
|
*/
|
2024-05-14 19:36:08 +00:00
|
|
|
import {
|
2024-05-21 14:09:42 +00:00
|
|
|
type ClassConstructorDef,
|
|
|
|
type ClassMethodDef,
|
|
|
|
type ClassPropertyDef,
|
2024-05-14 19:36:08 +00:00
|
|
|
doc,
|
2024-05-21 14:09:42 +00:00
|
|
|
type DocNode,
|
2024-05-14 19:36:08 +00:00
|
|
|
type DocNodeBase,
|
2024-05-21 14:09:42 +00:00
|
|
|
type DocNodeClass,
|
2024-05-14 19:36:08 +00:00
|
|
|
type DocNodeFunction,
|
2024-05-21 05:34:08 +00:00
|
|
|
type JsDoc,
|
2024-05-14 19:36:08 +00:00
|
|
|
type JsDocTagDocRequired,
|
2024-05-21 14:09:42 +00:00
|
|
|
type Location,
|
|
|
|
type TsTypeDef,
|
2024-05-14 19:36:08 +00:00
|
|
|
} from "@deno/doc";
|
2024-04-22 07:07:34 +00:00
|
|
|
|
2024-05-21 05:34:08 +00:00
|
|
|
type DocNodeWithJsDoc<T = DocNodeBase> = T & {
|
|
|
|
jsDoc: JsDoc;
|
|
|
|
};
|
|
|
|
|
2024-04-22 07:07:34 +00:00
|
|
|
const ENTRY_POINTS = [
|
2024-05-22 00:40:43 +00:00
|
|
|
"../async/mod.ts",
|
2024-05-22 13:42:59 +00:00
|
|
|
"../bytes/mod.ts",
|
2024-05-06 07:51:20 +00:00
|
|
|
"../collections/mod.ts",
|
2024-05-22 13:42:59 +00:00
|
|
|
"../datetime/mod.ts",
|
2024-05-15 07:57:20 +00:00
|
|
|
"../internal/mod.ts",
|
2024-05-22 13:42:59 +00:00
|
|
|
"../jsonc/mod.ts",
|
2024-05-15 06:33:05 +00:00
|
|
|
"../media_types/mod.ts",
|
2024-05-22 13:42:59 +00:00
|
|
|
"../ulid/mod.ts",
|
2024-05-21 17:33:10 +00:00
|
|
|
"../webgpu/mod.ts",
|
2024-05-22 19:09:08 +00:00
|
|
|
"../http/mod.ts",
|
2024-04-22 07:07:34 +00:00
|
|
|
] as const;
|
|
|
|
|
2024-05-21 10:12:55 +00:00
|
|
|
const TS_SNIPPET = /```ts[\s\S]*?```/g;
|
|
|
|
const NEWLINE = "\n";
|
2024-05-22 15:58:40 +00:00
|
|
|
const diagnostics: DocumentError[] = [];
|
|
|
|
const snippetPromises: Promise<void>[] = [];
|
2024-05-06 03:28:15 +00:00
|
|
|
|
2024-05-08 06:18:26 +00:00
|
|
|
class DocumentError extends Error {
|
2024-05-21 14:09:42 +00:00
|
|
|
constructor(
|
|
|
|
message: string,
|
|
|
|
document: { location: Location },
|
|
|
|
) {
|
2024-04-22 07:07:34 +00:00
|
|
|
super(message, {
|
|
|
|
cause: `${document.location.filename}:${document.location.line}`,
|
|
|
|
});
|
|
|
|
this.name = this.constructor.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function assert(
|
|
|
|
condition: boolean,
|
|
|
|
message: string,
|
2024-05-21 14:09:42 +00:00
|
|
|
document: { location: Location },
|
2024-05-22 15:58:40 +00:00
|
|
|
) {
|
2024-04-22 07:07:34 +00:00
|
|
|
if (!condition) {
|
2024-05-22 15:58:40 +00:00
|
|
|
diagnostics.push(new DocumentError(message, document));
|
2024-04-22 07:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isExported(document: DocNodeBase) {
|
|
|
|
return document.declarationKind === "export";
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:09:42 +00:00
|
|
|
function isVoidOrPromiseVoid(returnType: TsTypeDef) {
|
|
|
|
return isVoid(returnType) ||
|
|
|
|
(returnType.kind === "typeRef" &&
|
|
|
|
returnType.typeRef.typeName === "Promise" &&
|
|
|
|
returnType.typeRef.typeParams?.length === 1 &&
|
|
|
|
isVoid(returnType.typeRef.typeParams[0]!));
|
2024-05-21 05:34:08 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 14:09:42 +00:00
|
|
|
function isVoid(returnType: TsTypeDef) {
|
|
|
|
return returnType.kind === "keyword" && returnType.keyword === "void";
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertHasReturnTag(document: { jsDoc: JsDoc; location: Location }) {
|
2024-05-21 05:34:08 +00:00
|
|
|
const tag = document.jsDoc.tags?.find((tag) => tag.kind === "return");
|
2024-05-22 15:58:40 +00:00
|
|
|
if (tag === undefined) {
|
|
|
|
diagnostics.push(
|
|
|
|
new DocumentError("Symbol must have a @return tag", document),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
assert(
|
|
|
|
// @ts-ignore doc is defined
|
|
|
|
tag.doc !== undefined,
|
|
|
|
"@return tag must have a description",
|
|
|
|
document,
|
|
|
|
);
|
|
|
|
}
|
2024-04-22 07:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function assertHasParamTag(
|
2024-05-21 14:09:42 +00:00
|
|
|
document: { jsDoc: JsDoc; location: Location },
|
2024-04-22 07:07:34 +00:00
|
|
|
param: string,
|
|
|
|
) {
|
2024-05-21 05:34:08 +00:00
|
|
|
const tag = document.jsDoc.tags?.find((tag) =>
|
|
|
|
tag.kind === "param" && tag.name === param
|
|
|
|
);
|
2024-05-22 15:58:40 +00:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
}
|
2024-04-22 07:07:34 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 15:58:40 +00:00
|
|
|
function assertHasExampleTag(
|
|
|
|
document: { jsDoc: JsDoc; location: Location },
|
|
|
|
) {
|
2024-05-21 05:34:08 +00:00
|
|
|
const tags = document.jsDoc.tags?.filter((tag) => tag.kind === "example");
|
|
|
|
if (tags === undefined || tags.length === 0) {
|
2024-05-22 15:58:40 +00:00
|
|
|
diagnostics.push(
|
|
|
|
new DocumentError("Symbol must have an @example tag", document),
|
|
|
|
);
|
|
|
|
return;
|
2024-05-06 03:28:15 +00:00
|
|
|
}
|
|
|
|
for (const tag of (tags as JsDocTagDocRequired[])) {
|
|
|
|
assert(
|
|
|
|
tag.doc !== undefined,
|
2024-05-22 05:08:36 +00:00
|
|
|
"@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",
|
2024-05-06 03:28:15 +00:00
|
|
|
document,
|
|
|
|
);
|
2024-05-21 10:12:55 +00:00
|
|
|
const snippets = tag.doc.match(TS_SNIPPET);
|
2024-05-06 03:28:15 +00:00
|
|
|
if (snippets === null) {
|
2024-05-22 15:58:40 +00:00
|
|
|
diagnostics.push(
|
|
|
|
new DocumentError(
|
|
|
|
"@example tag must have a TypeScript code snippet",
|
|
|
|
document,
|
|
|
|
),
|
2024-05-06 03:28:15 +00:00
|
|
|
);
|
2024-05-22 15:58:40 +00:00
|
|
|
continue;
|
2024-05-06 03:28:15 +00:00
|
|
|
}
|
2024-05-21 10:12:55 +00:00
|
|
|
for (let snippet of snippets) {
|
|
|
|
if (snippet.split(NEWLINE)[0]?.includes("no-eval")) continue;
|
|
|
|
// Trim the code block delimiters
|
|
|
|
snippet = snippet.split(NEWLINE).slice(1, -1).join(NEWLINE);
|
2024-05-06 03:28:15 +00:00
|
|
|
const command = new Deno.Command(Deno.execPath(), {
|
|
|
|
args: [
|
|
|
|
"eval",
|
2024-05-08 06:18:26 +00:00
|
|
|
"--ext=ts",
|
2024-05-21 17:33:10 +00:00
|
|
|
"--unstable-webgpu",
|
2024-05-06 03:28:15 +00:00
|
|
|
snippet,
|
|
|
|
],
|
2024-05-08 06:18:26 +00:00
|
|
|
stderr: "piped",
|
2024-05-06 03:28:15 +00:00
|
|
|
});
|
2024-05-22 15:58:40 +00:00
|
|
|
snippetPromises.push((async () => {
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
console.warn("Snippet has been running for more than 10 seconds...");
|
|
|
|
console.warn(snippet);
|
|
|
|
}, 10_000);
|
|
|
|
try {
|
|
|
|
const { success, stderr } = await command.output();
|
|
|
|
assert(
|
|
|
|
success,
|
|
|
|
`Example code snippet failed to execute: \n${snippet}\n${
|
|
|
|
new TextDecoder().decode(stderr)
|
|
|
|
}`,
|
|
|
|
document,
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
}
|
|
|
|
})());
|
2024-05-06 03:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 07:34:47 +00:00
|
|
|
function assertHasTypeParamTags(
|
2024-05-21 14:09:42 +00:00
|
|
|
document: { jsDoc: JsDoc; location: Location },
|
2024-05-20 07:34:47 +00:00
|
|
|
typeParamName: string,
|
2024-05-02 07:47:00 +00:00
|
|
|
) {
|
2024-05-21 05:34:08 +00:00
|
|
|
const tag = document.jsDoc.tags?.find((tag) =>
|
2024-05-20 07:34:47 +00:00
|
|
|
tag.kind === "template" && tag.name === typeParamName
|
2024-05-02 07:47:00 +00:00
|
|
|
);
|
2024-05-22 15:58:40 +00:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
}
|
2024-05-02 07:47:00 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 05:34:08 +00:00
|
|
|
/**
|
|
|
|
* 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 {@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.
|
|
|
|
*/
|
2024-05-21 14:09:42 +00:00
|
|
|
function assertFunctionDocs(
|
|
|
|
document: DocNodeWithJsDoc<DocNodeFunction | ClassMethodDef>,
|
|
|
|
) {
|
2024-04-22 07:07:34 +00:00
|
|
|
for (const param of document.functionDef.params) {
|
|
|
|
if (param.kind === "identifier") {
|
2024-05-21 05:34:08 +00:00
|
|
|
assertHasParamTag(document, param.name);
|
2024-04-22 07:07:34 +00:00
|
|
|
}
|
2024-05-21 14:09:42 +00:00
|
|
|
if (param.kind === "assign" && param.left.kind === "identifier") {
|
2024-05-21 05:34:08 +00:00
|
|
|
assertHasParamTag(document, param.left.name);
|
2024-04-22 07:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-02 07:47:00 +00:00
|
|
|
for (const typeParam of document.functionDef.typeParams) {
|
2024-05-21 05:34:08 +00:00
|
|
|
assertHasTypeParamTags(document, typeParam.name);
|
2024-05-02 07:47:00 +00:00
|
|
|
}
|
2024-05-21 14:09:42 +00:00
|
|
|
if (
|
|
|
|
document.functionDef.returnType !== undefined &&
|
|
|
|
!isVoidOrPromiseVoid(document.functionDef.returnType)
|
|
|
|
) {
|
|
|
|
assertHasReturnTag(document);
|
|
|
|
}
|
2024-05-21 05:34:08 +00:00
|
|
|
assertHasExampleTag(document);
|
2024-04-22 07:07:34 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 14:09:42 +00:00
|
|
|
/**
|
|
|
|
* 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>) {
|
|
|
|
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) {
|
2024-05-22 15:58:40 +00:00
|
|
|
diagnostics.push(
|
|
|
|
new DocumentError(
|
|
|
|
"Do not use `public`, `protected`, or `private` fields in classes",
|
|
|
|
property,
|
|
|
|
),
|
2024-05-21 14:09:42 +00:00
|
|
|
);
|
2024-05-22 15:58:40 +00:00
|
|
|
continue;
|
2024-05-21 14:09:42 +00:00
|
|
|
}
|
2024-05-22 15:58:40 +00:00
|
|
|
assertClassPropertyDocs(
|
|
|
|
property as DocNodeWithJsDoc<ClassPropertyDef>,
|
|
|
|
);
|
2024-05-21 14:09:42 +00:00
|
|
|
}
|
|
|
|
for (const method of document.classDef.methods) {
|
|
|
|
if (method.jsDoc === undefined) continue; // this is caught by `deno doc --lint`
|
|
|
|
if (method.accessibility !== undefined) {
|
2024-05-22 15:58:40 +00:00
|
|
|
diagnostics.push(
|
|
|
|
new DocumentError(
|
|
|
|
"Do not use `public`, `protected`, or `private` methods in classes",
|
|
|
|
method,
|
|
|
|
),
|
2024-05-21 14:09:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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) {
|
2024-05-22 15:58:40 +00:00
|
|
|
diagnostics.push(
|
|
|
|
new DocumentError(
|
|
|
|
"Do not use `public`, `protected`, or `private` constructors in classes",
|
|
|
|
constructor,
|
|
|
|
),
|
2024-05-21 14:09:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
2024-05-22 15:58:40 +00:00
|
|
|
function assertClassPropertyDocs(
|
|
|
|
property: DocNodeWithJsDoc<ClassPropertyDef>,
|
|
|
|
) {
|
2024-05-21 14:09:42 +00:00
|
|
|
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) {
|
2024-05-22 15:58:40 +00:00
|
|
|
assert(
|
|
|
|
param.accessibility === undefined,
|
|
|
|
"Do not use `public`, `protected`, or `private` parameters in constructors",
|
|
|
|
constructor,
|
|
|
|
);
|
2024-05-21 14:09:42 +00:00
|
|
|
if (param.kind === "identifier") {
|
|
|
|
assertHasParamTag(constructor, param.name);
|
|
|
|
}
|
|
|
|
if (param.kind === "assign" && param.left.kind === "identifier") {
|
|
|
|
assertHasParamTag(constructor, param.left.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertHasExampleTag(constructor);
|
|
|
|
}
|
|
|
|
|
2024-04-22 07:07:34 +00:00
|
|
|
async function checkDocs(specifier: string) {
|
|
|
|
const docs = await doc(specifier);
|
2024-05-21 14:09:42 +00:00
|
|
|
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 "function": {
|
|
|
|
assertFunctionDocs(document);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "class": {
|
|
|
|
assertClassDocs(document);
|
|
|
|
break;
|
|
|
|
}
|
2024-05-06 03:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-22 07:07:34 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 13:42:59 +00:00
|
|
|
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) {
|
|
|
|
console.error(
|
|
|
|
`%c[error] %c'deno doc --lint' failed`,
|
|
|
|
"color: red",
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
Deno.exit(1);
|
|
|
|
}
|
|
|
|
|
2024-04-22 07:07:34 +00:00
|
|
|
const promises = [];
|
2024-05-22 13:42:59 +00:00
|
|
|
for (const url of ENTRY_POINT_URLS) {
|
|
|
|
promises.push(checkDocs(url));
|
2024-04-22 07:07:34 +00:00
|
|
|
}
|
2024-05-21 14:09:42 +00:00
|
|
|
|
2024-05-22 15:58:40 +00:00
|
|
|
await Promise.all(promises);
|
|
|
|
await Promise.all(snippetPromises);
|
|
|
|
if (diagnostics.length > 0) {
|
|
|
|
for (const error of diagnostics) {
|
2024-05-21 14:09:42 +00:00
|
|
|
console.error(
|
|
|
|
`%c[error] %c${error.message} %cat ${error.cause}`,
|
|
|
|
"color: red",
|
|
|
|
"",
|
|
|
|
"color: gray",
|
|
|
|
);
|
|
|
|
}
|
2024-05-22 15:58:40 +00:00
|
|
|
Deno.exit(1);
|
2024-05-21 14:09:42 +00:00
|
|
|
}
|