Skip to content

Commit e6f7ece

Browse files
committed
Format code signatures in jsdocs via prettier
1 parent 6455fc4 commit e6f7ece

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

scripts/docs.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import fs from "node:fs";
22
import path from "node:path";
33
import util from "node:util";
4-
import fg from "fast-glob";
54

5+
import fg from "fast-glob";
66
import dox from "dox";
7+
import prettier from "prettier";
78
import { ReflectionKind, type JSONOutput } from "typedoc";
89
import ts from "typescript";
910

@@ -61,7 +62,7 @@ type SimplifiedComment = {
6162
codeLink: string;
6263
modes: Mode[];
6364
summary: string;
64-
reference: string;
65+
reference?: string;
6566
example?: string;
6667
signature?: string;
6768
params: {
@@ -150,11 +151,15 @@ if (filePaths.length === 0) {
150151
process.exit(1);
151152
}
152153

154+
run();
155+
153156
// Generate markdown documentation for all matching files
154-
filePaths.forEach((filePath) => {
155-
console.log(`\nProcessing file: ${filePath}`);
156-
generateMarkdownDocs(filePath, apiFilter, outputDir, args.write);
157-
});
157+
async function run() {
158+
for (let filePath of filePaths) {
159+
console.log(`\nProcessing file: ${filePath}`);
160+
await generateMarkdownDocs(filePath, apiFilter, outputDir, args.write);
161+
}
162+
}
158163

159164
function buildRepoDocsLinks(outputDir: string): Map<string, string> {
160165
const lookup = new Map<string, string>();
@@ -318,13 +323,13 @@ function getDeclarationDescription(child: JSONOutput.DeclarationReflection) {
318323
.join("");
319324
}
320325

321-
function generateMarkdownDocs(
326+
async function generateMarkdownDocs(
322327
filepath: string,
323328
apiFilter: string[] | null,
324329
outputDir?: string,
325330
writeFiles?: boolean,
326331
) {
327-
let simplifiedComments = parseDocComments(filepath, apiFilter);
332+
let simplifiedComments = await parseDocComments(filepath, apiFilter);
328333
simplifiedComments.forEach((comment) => {
329334
// Generate markdown content for each public function
330335
let markdownContent = generateMarkdownForComment(comment);
@@ -495,17 +500,16 @@ function generateMarkdownForComment(comment: SimplifiedComment): string {
495500
return markdown;
496501
}
497502

498-
function parseDocComments(filepath: string, apiFilter: string[] | null) {
503+
async function parseDocComments(filepath: string, apiFilter: string[] | null) {
499504
let code = fs.readFileSync(filepath).toString();
500505
let comments = dox.parseComments(code, { raw: true }) as ParsedComment[];
506+
let filteredComments = comments.filter(
507+
(c) =>
508+
c.tags.some((t) => t.type === "public") &&
509+
(!apiFilter || apiFilter.includes(getApiName(c))),
510+
);
501511

502-
return comments
503-
.filter(
504-
(c) =>
505-
c.tags.some((t) => t.type === "public") &&
506-
(!apiFilter || apiFilter.includes(getApiName(c))),
507-
)
508-
.map((c) => simplifyComment(c, filepath));
512+
return Promise.all(filteredComments.map((c) => simplifyComment(c, filepath)));
509513
}
510514

511515
function getApiName(comment: ParsedComment): string {
@@ -529,10 +533,10 @@ function getApiName(comment: ParsedComment): string {
529533
throw new Error(`Could not determine API name:\n${comment.code}\n`);
530534
}
531535

532-
function simplifyComment(
536+
async function simplifyComment(
533537
comment: ParsedComment,
534538
filepath: string,
535-
): SimplifiedComment {
539+
): Promise<SimplifiedComment> {
536540
let name = getApiName(comment);
537541
let unstable = name.startsWith("unstable_");
538542

@@ -570,7 +574,7 @@ function simplifyComment(
570574
);
571575
}
572576

573-
let signature = getSignature(comment.code);
577+
let signature = await getSignature(comment.code);
574578

575579
let params: SimplifiedComment["params"] = [];
576580
comment.tags.forEach((tag) => {
@@ -642,7 +646,7 @@ function isParamTag(tag: Tag): tag is ParamTag {
642646

643647
// Parse the TypeScript code into an AST so we can remove the function body
644648
// and just grab the signature
645-
function getSignature(code: string) {
649+
async function getSignature(code: string) {
646650
const ast = ts.createSourceFile("example.ts", code, ts.ScriptTarget.Latest);
647651
if (ast.statements.length === 0) {
648652
throw new Error(`Expected one or more statements: ${code}`);
@@ -663,7 +667,9 @@ function getSignature(code: string) {
663667
.createPrinter({ newLine: ts.NewLineKind.LineFeed })
664668
.printNode(ts.EmitHint.Unspecified, modifiedFunction, ast);
665669

666-
return newCode.replace("{ }", "").trim();
670+
let formatted = await prettier.format(newCode, { parser: "typescript" });
671+
672+
return formatted.replace("{}", "").trim();
667673
}
668674

669675
// TODO: Handle variable statements for forwardRef components

0 commit comments

Comments
 (0)