Skip to content

Commit

Permalink
feat: show default values in docs (#10465)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qjuh authored Sep 15, 2024
1 parent 2adee06 commit 651f2d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/api-extractor/src/generators/ApiModelGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ interface DocgenEventJson {
}

interface DocgenParamJson {
default?: string;
default?: boolean | number | string;
description: string;
name: string;
nullable?: boolean;
Expand Down Expand Up @@ -155,7 +155,7 @@ interface DocgenMethodJson {
interface DocgenPropertyJson {
abstract?: boolean;
access?: DocgenAccess;
default?: string;
default?: boolean | number | string;
deprecated?: DocgenDeprecated;
description: string;
meta: DocgenMetaJson;
Expand Down Expand Up @@ -1264,7 +1264,7 @@ export class ApiModelGenerator {
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);
const docComment: tsdoc.DocComment | undefined = jsDoc
? this._tsDocParser.parseString(
`/**\n * ${this._fixLinkTags(jsDoc.description) ?? ''}\n${
`/**\n * ${this._fixLinkTags(jsDoc.description) ?? ''}${jsDoc.default ? ` (default: ${this._escapeSpecialChars(jsDoc.default)})` : ''}\n${
'see' in jsDoc ? jsDoc.see.map((see) => ` * @see ${see}\n`).join('') : ''
}${'readonly' in jsDoc && jsDoc.readonly ? ' * @readonly\n' : ''}${
'deprecated' in jsDoc && jsDoc.deprecated
Expand Down Expand Up @@ -1342,7 +1342,7 @@ export class ApiModelGenerator {
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);
const docComment: tsdoc.DocComment | undefined = jsDoc
? this._tsDocParser.parseString(
`/**\n * ${this._fixLinkTags(jsDoc.description) ?? ''}\n${
`/**\n * ${this._fixLinkTags(jsDoc.description) ?? ''}${jsDoc.default ? ` (default: ${this._escapeSpecialChars(jsDoc.default)})` : ''}\n${
'see' in jsDoc ? jsDoc.see.map((see) => ` * @see ${see}\n`).join('') : ''
}${'readonly' in jsDoc && jsDoc.readonly ? ' * @readonly\n' : ''}${
'deprecated' in jsDoc && jsDoc.deprecated
Expand Down Expand Up @@ -1746,6 +1746,14 @@ export class ApiModelGenerator {
return sourceLocation;
}

private _escapeSpecialChars(input: boolean | number | string) {
if (typeof input !== 'string') {
return input;
}

return input.replaceAll(/(?<char>[{}])/g, '\\$<char>');
}

private _fixLinkTags(input?: string): string | undefined {
return input
?.replaceAll(linkRegEx, (_match, _p1, _p2, _p3, _p4, _p5, _offset, _string, groups) => {
Expand Down Expand Up @@ -1823,7 +1831,7 @@ export class ApiModelGenerator {
isOptional: Boolean(prop.nullable),
isReadonly: Boolean(prop.readonly),
docComment: this._tsDocParser.parseString(
`/**\n * ${this._fixLinkTags(prop.description) ?? ''}\n${
`/**\n * ${this._fixLinkTags(prop.description) ?? ''}${prop.default ? ` (default: ${this._escapeSpecialChars(prop.default)})` : ''}\n${
prop.see?.map((see) => ` * @see ${see}\n`).join('') ?? ''
}${prop.readonly ? ' * @readonly\n' : ''} */`,
).docComment,
Expand Down
6 changes: 6 additions & 0 deletions packages/scripts/src/generateSplitDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
} from '@discordjs/api-extractor-model';
import { DocNodeKind, SelectorKind, StandardTags } from '@microsoft/tsdoc';
import type {
DocEscapedText,
DocNode,
DocNodeContainer,
DocDeclarationReference,
Expand Down Expand Up @@ -307,6 +308,11 @@ function itemTsDoc(item: DocNode, apiItem: ApiItem) {
kind: DocNodeKind.PlainText,
text: (node as DocPlainText).text,
};
case DocNodeKind.EscapedText:
return {
kind: DocNodeKind.PlainText,
text: (node as DocEscapedText).decodedText,
};
case DocNodeKind.Section:
case DocNodeKind.Paragraph:
return (node as DocNodeContainer).nodes.map((node) => createNode(node));
Expand Down

0 comments on commit 651f2d0

Please sign in to comment.