Skip to content

Commit 437bb94

Browse files
QjuhJiralite
authored andcommitted
feat: show default values in docs (#10465)
1 parent 77f7c7f commit 437bb94

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

packages/api-extractor/src/generators/ApiModelGenerator.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ interface DocgenEventJson {
114114
}
115115

116116
interface DocgenParamJson {
117-
default?: string;
117+
default?: boolean | number | string;
118118
description: string;
119119
name: string;
120120
nullable?: boolean;
@@ -155,7 +155,7 @@ interface DocgenMethodJson {
155155
interface DocgenPropertyJson {
156156
abstract?: boolean;
157157
access?: DocgenAccess;
158-
default?: string;
158+
default?: boolean | number | string;
159159
deprecated?: DocgenDeprecated;
160160
description: string;
161161
meta: DocgenMetaJson;
@@ -1264,7 +1264,7 @@ export class ApiModelGenerator {
12641264
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);
12651265
const docComment: tsdoc.DocComment | undefined = jsDoc
12661266
? this._tsDocParser.parseString(
1267-
`/**\n * ${this._fixLinkTags(jsDoc.description) ?? ''}${jsDoc.default === undefined ? '' : ` (default: ${this._escapeSpecialChars(jsDoc.default)})`}\n${
1267+
`/**\n * ${this._fixLinkTags(jsDoc.description) ?? ''}${jsDoc.default ? ` (default: ${this._escapeSpecialChars(jsDoc.default)})` : ''}\n${
12681268
'see' in jsDoc ? jsDoc.see.map((see) => ` * @see ${see}\n`).join('') : ''
12691269
}${'readonly' in jsDoc && jsDoc.readonly ? ' * @readonly\n' : ''}${
12701270
'deprecated' in jsDoc && jsDoc.deprecated
@@ -1342,7 +1342,7 @@ export class ApiModelGenerator {
13421342
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);
13431343
const docComment: tsdoc.DocComment | undefined = jsDoc
13441344
? this._tsDocParser.parseString(
1345-
`/**\n * ${this._fixLinkTags(jsDoc.description) ?? ''}${jsDoc.default === undefined ? '' : `\n * @defaultValue ${this._escapeSpecialChars(jsDoc.default)}`}\n${
1345+
`/**\n * ${this._fixLinkTags(jsDoc.description) ?? ''}${jsDoc.default ? ` (default: ${this._escapeSpecialChars(jsDoc.default)})` : ''}\n${
13461346
'see' in jsDoc ? jsDoc.see.map((see) => ` * @see ${see}\n`).join('') : ''
13471347
}${'readonly' in jsDoc && jsDoc.readonly ? ' * @readonly\n' : ''}${
13481348
'deprecated' in jsDoc && jsDoc.deprecated
@@ -1746,6 +1746,14 @@ export class ApiModelGenerator {
17461746
return sourceLocation;
17471747
}
17481748

1749+
private _escapeSpecialChars(input: boolean | number | string) {
1750+
if (typeof input !== 'string') {
1751+
return input;
1752+
}
1753+
1754+
return input.replaceAll(/(?<char>[{}])/g, '\\$<char>');
1755+
}
1756+
17491757
private _fixLinkTags(input?: string): string | undefined {
17501758
return input
17511759
?.replaceAll(linkRegEx, (_match, _p1, _p2, _p3, _p4, _p5, _offset, _string, groups) => {
@@ -1823,7 +1831,7 @@ export class ApiModelGenerator {
18231831
isOptional: Boolean(prop.nullable),
18241832
isReadonly: Boolean(prop.readonly),
18251833
docComment: this._tsDocParser.parseString(
1826-
`/**\n * ${this._fixLinkTags(prop.description) ?? ''}\n${
1834+
`/**\n * ${this._fixLinkTags(prop.description) ?? ''}${prop.default ? ` (default: ${this._escapeSpecialChars(prop.default)})` : ''}\n${
18271835
prop.see?.map((see) => ` * @see ${see}\n`).join('') ?? ''
18281836
}${prop.readonly ? ' * @readonly\n' : ''} */`,
18291837
).docComment,

packages/scripts/src/generateSplitDocumentation.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
} from '@discordjs/api-extractor-model';
4040
import { DocNodeKind, SelectorKind, StandardTags } from '@microsoft/tsdoc';
4141
import type {
42+
DocEscapedText,
4243
DocNode,
4344
DocNodeContainer,
4445
DocDeclarationReference,
@@ -307,6 +308,11 @@ function itemTsDoc(item: DocNode, apiItem: ApiItem) {
307308
kind: DocNodeKind.PlainText,
308309
text: (node as DocPlainText).text,
309310
};
311+
case DocNodeKind.EscapedText:
312+
return {
313+
kind: DocNodeKind.PlainText,
314+
text: (node as DocEscapedText).decodedText,
315+
};
310316
case DocNodeKind.Section:
311317
case DocNodeKind.Paragraph:
312318
return (node as DocNodeContainer).nodes.map((node) => createNode(node));

0 commit comments

Comments
 (0)