-
Notifications
You must be signed in to change notification settings - Fork 186
Basic JSDoc types - Plot.plot and all Marks #1055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 22 commits
1b251df
1a1383e
60dae75
f363cb3
f5b23f0
aeed20a
1fec1ef
193fc61
34af125
7a18bf4
9d9cdb3
78ff9de
6c106c8
69db71e
c60fb8e
6762489
f7447f3
331caa3
032cc1c
d4b0680
2ce04f4
0df438f
4ef9406
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import {readFileSync, writeFileSync} from "fs"; | ||
import type {ExportedDeclarations, FunctionDeclaration} from "ts-morph"; | ||
import type {ExportedDeclarations, FunctionDeclaration, JSDoc} from "ts-morph"; | ||
import {Project} from "ts-morph"; | ||
|
||
/** | ||
|
@@ -53,7 +53,7 @@ function injectJsDoc(readme: string) { | |
if (!declaration) throw new Error(`${name} is not exported by src/index`); | ||
parts.push(getJsDocs(name, declaration, prefix)); | ||
parts.push(""); | ||
replacement = parts.join("\n"); | ||
replacement = pad(parts.join("\n")); | ||
} | ||
if (!insideReplacement || isReplacementDelimiter) output.push(line); | ||
if (replacement) output.push(replacement); | ||
|
@@ -66,14 +66,24 @@ function getJsDocs(name: string, declaration: ExportedDeclarations, prefix = "## | |
return getJsDocsForFunction(name, declaration, prefix); | ||
} | ||
if ("getJsDocs" in declaration) { | ||
return `${prefix} Plot.${name}\n${declaration | ||
.getJsDocs() | ||
.map((doc) => makeRelativeUrls(doc.getDescription())) | ||
.join("\n\n")}`; | ||
return `${prefix} Plot.${name}\n${transformDocs(declaration.getJsDocs())}`; | ||
} | ||
return `JSDoc extraction for ${declaration.getKindName()} not yet implemented.`; | ||
} | ||
|
||
function transformDocs(docs: JSDoc[]): string { | ||
return makeRelativeUrls(pad(docs.map((doc) => doc.getDescription()).join("\n\n"))); | ||
} | ||
|
||
function makeRelativeUrls(description: string) { | ||
return description.replace(new RegExp("https://github.com/observablehq/plot/blob/main/README.md#", "g"), "#"); | ||
} | ||
|
||
// Standardize on one leading and trailing new line for each replacement. | ||
function pad(s: string) { | ||
return `\n${s.trim()}\n`; | ||
} | ||
|
||
function getJsDocsForFunction(name: string, declaration: FunctionDeclaration, prefix = "####") { | ||
const parameters = declaration.getParameters(); | ||
const title = `${prefix} Plot.${name}(${parameters | ||
|
@@ -82,7 +92,7 @@ function getJsDocsForFunction(name: string, declaration: FunctionDeclaration, pr | |
const parts = [title]; | ||
const docs = declaration.getJsDocs(); | ||
if (docs.length) { | ||
parts.push(docs.map((doc) => makeRelativeUrls(doc.getDescription())).join("\n\n")); | ||
parts.push(transformDocs(docs)); | ||
return parts.join("\n"); | ||
} | ||
// If we didn't find docs on the implementation, it's probably on one of the | ||
|
@@ -91,17 +101,13 @@ function getJsDocsForFunction(name: string, declaration: FunctionDeclaration, pr | |
for (const overload of overloads) { | ||
const docs = overload.getJsDocs(); | ||
if (!docs.length) continue; | ||
parts.push(docs.map((doc) => makeRelativeUrls(doc.getDescription())).join("\n\n")); | ||
parts.push(transformDocs(docs)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of the changes to jsdoc-to-readme are about trimming the JSDoc so extra leading and trailing new lines don't end up in the README file. For some reason, when you have a JSDoc in a file that is just importing types, TypeScript returns empty lines for that JSDoc. This is benign but will result in a slightly cleaner README file. |
||
return parts.join("\n"); | ||
} | ||
|
||
return "No JSDocs found."; | ||
} | ||
|
||
function makeRelativeUrls(description: string) { | ||
return description.replace(new RegExp("https://github.com/observablehq/plot/blob/main/README.md#", "g"), "#"); | ||
} | ||
|
||
const check = process.argv[process.argv.length - 1] === "--check"; | ||
const original = readFileSync(readmePath, {encoding: "utf-8"}); | ||
const output = injectJsDoc(original); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #1055 (comment)