Skip to content

Commit fd44b38

Browse files
committed
docs: extract defaults from the comments if absent otherwise
1 parent 6e29767 commit fd44b38

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

scripts/apidoc/signature.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ function analyzeParameter(parameter: ParameterReflection): {
162162
const name = parameter.name;
163163
const declarationName = name + (isOptional(parameter) ? '?' : '');
164164
const type = parameter.type;
165-
const defaultValue = parameter.defaultValue;
165+
const commentDefault = extractDefaultFromComment(parameter.comment);
166+
const defaultValue = parameter.defaultValue ?? commentDefault;
166167

167168
let signatureText = '';
168169
if (defaultValue) {
@@ -200,6 +201,7 @@ function analyzeParameterOptions(
200201
return properties.map((property) => ({
201202
name: `${name}.${property.name}${isOptional(property) ? '?' : ''}`,
202203
type: declarationTypeToText(property),
204+
default: extractDefaultFromComment(property.comment),
203205
description: mdToHtml(
204206
toBlock(property.comment ?? property.signatures?.[0].comment)
205207
),
@@ -284,3 +286,25 @@ function signatureTypeToText(signature: SignatureReflection): string {
284286
.map((p) => `${p.name}: ${typeToText(p.type)}`)
285287
.join(', ')}) => ${typeToText(signature.type)}`;
286288
}
289+
290+
/**
291+
* Extracts and removed the parameter default from the comments.
292+
*
293+
* @param comment The comment to extract the default from.
294+
* @returns The extracted default value.
295+
*/
296+
function extractDefaultFromComment(comment?: Comment): string {
297+
if (!comment) {
298+
return;
299+
}
300+
const text = comment.shortText;
301+
if (!text || text.trim() === '') {
302+
return;
303+
}
304+
const result = /(.*)[ \n]Defaults to `([^`]+)`./.exec(text);
305+
if (!result) {
306+
return;
307+
}
308+
comment.shortText = result[1];
309+
return result[2];
310+
}

0 commit comments

Comments
 (0)