Skip to content

Commit 26ec43f

Browse files
committed
Document hook parameters
1 parent 3096c5c commit 26ec43f

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

src/parser.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ export function parseFromProgram(
423423
const type = checker.getTypeOfSymbolAtLocation(symbol, node);
424424
const typeStack = new Set<number>([(type as any).id]);
425425

426+
const parameterDescriptions = getParameterDescriptionFromNode(node);
427+
426428
const checkedSignatures = type.getCallSignatures().map((signature) => {
427429
return {
428430
parameters: signature.parameters.map((param) => {
@@ -431,7 +433,14 @@ export function parseFromProgram(
431433
typeStack,
432434
param.getName(),
433435
);
434-
return t.parameterNode(type, param.getName(), getDocumentationFromSymbol(param));
436+
437+
const parameterDescription = parameterDescriptions[param.getName()];
438+
439+
return t.parameterNode(
440+
type,
441+
param.getName(),
442+
parameterDescription ? { description: parameterDescription } : undefined,
443+
);
435444
}),
436445

437446
returnValue: checkType(signature.getReturnType(), typeStack, hookName),
@@ -762,3 +771,22 @@ function getVisibilityFromJSDoc(doc: ts.JSDoc): t.Documentation['visibility'] |
762771

763772
return undefined;
764773
}
774+
775+
function getParameterDescriptionFromNode(node: ts.Node) {
776+
const comments = ts.getJSDocCommentsAndTags(node);
777+
if (comments && comments.length >= 1) {
778+
const commentNode = comments[0];
779+
if (ts.isJSDoc(commentNode)) {
780+
const paramComments: Record<string, string> = {};
781+
commentNode.tags?.forEach((tag) => {
782+
if (ts.isJSDocParameterTag(tag) && typeof tag.comment === 'string') {
783+
paramComments[tag.name.getText()] = tag.comment.replace(/^[\s-*:]+/g, '');
784+
}
785+
});
786+
787+
return paramComments;
788+
}
789+
}
790+
791+
return {};
792+
}

test/hook-multiple-parameters/input.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
/**
22
* A hook defined as a function.
33
*
4+
* @param value - The value.
5+
* @param onChange - The change handler.
6+
* @param severity - The severity.
7+
*
48
* @internal
59
*/
610
export function useHook(
711
value: string,
8-
severity: Severity,
912
onChange: (newValue: string) => void,
13+
severity: Severity = 'low',
1014
): number {
1115
return 42;
1216
}

test/hook-multiple-parameters/output.json

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,9 @@
1111
"type": {
1212
"nodeType": "intrinsic",
1313
"type": "string"
14-
}
15-
},
16-
{
17-
"nodeType": "parameter",
18-
"name": "severity",
19-
"type": {
20-
"nodeType": "union",
21-
"types": [
22-
{
23-
"nodeType": "literal",
24-
"value": "\"low\""
25-
},
26-
{
27-
"nodeType": "literal",
28-
"value": "\"high\""
29-
}
30-
]
14+
},
15+
"documentation": {
16+
"description": "The value."
3117
}
3218
},
3319
{
@@ -49,6 +35,29 @@
4935
"nodeType": "intrinsic",
5036
"type": "void"
5137
}
38+
},
39+
"documentation": {
40+
"description": "The change handler."
41+
}
42+
},
43+
{
44+
"nodeType": "parameter",
45+
"name": "severity",
46+
"type": {
47+
"nodeType": "union",
48+
"types": [
49+
{
50+
"nodeType": "literal",
51+
"value": "\"low\""
52+
},
53+
{
54+
"nodeType": "literal",
55+
"value": "\"high\""
56+
}
57+
]
58+
},
59+
"documentation": {
60+
"description": "The severity."
5261
}
5362
}
5463
],

0 commit comments

Comments
 (0)