Skip to content

Commit 97e88d4

Browse files
committed
Handle parameter initializers
1 parent 26ec43f commit 97e88d4

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/parser.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,19 +427,34 @@ export function parseFromProgram(
427427

428428
const checkedSignatures = type.getCallSignatures().map((signature) => {
429429
return {
430-
parameters: signature.parameters.map((param) => {
430+
parameters: signature.parameters.map((parameterSymbol) => {
431+
const parameterDeclaration = parameterSymbol.valueDeclaration as ts.ParameterDeclaration;
431432
const type = checkType(
432-
checker.getTypeOfSymbolAtLocation(param, param.valueDeclaration!),
433+
checker.getTypeOfSymbolAtLocation(parameterSymbol, parameterDeclaration),
433434
typeStack,
434-
param.getName(),
435+
parameterSymbol.getName(),
435436
);
436437

437-
const parameterDescription = parameterDescriptions[param.getName()];
438+
const documentation: t.Documentation = {};
439+
documentation.description = parameterDescriptions[parameterSymbol.getName()];
440+
const initializer = parameterDeclaration.initializer;
441+
if (initializer) {
442+
const initializerType = checker.getTypeAtLocation(initializer);
443+
if (initializerType.flags & ts.TypeFlags.Literal) {
444+
if (initializerType.isStringLiteral()) {
445+
documentation.defaultValue = `"${initializer.getText()}"`;
446+
} else {
447+
documentation.defaultValue = initializer.getText();
448+
}
449+
}
450+
}
451+
452+
const hasDocumentation = documentation.description || documentation.defaultValue;
438453

439454
return t.parameterNode(
440455
type,
441-
param.getName(),
442-
parameterDescription ? { description: parameterDescription } : undefined,
456+
parameterSymbol.getName(),
457+
hasDocumentation ? documentation : undefined,
443458
);
444459
}),
445460

test/hook-arrow-function/output.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"parameters": [
88
{
99
"nodeType": "parameter",
10+
"name": "parameters",
1011
"type": {
1112
"nodeType": "interface",
1213
"members": [
@@ -38,11 +39,11 @@
3839
"parameters": [
3940
{
4041
"nodeType": "parameter",
42+
"name": "value",
4143
"type": {
4244
"nodeType": "intrinsic",
4345
"type": "string"
44-
},
45-
"name": "value"
46+
}
4647
}
4748
],
4849
"returnValueType": {
@@ -54,8 +55,7 @@
5455
"filenames": {}
5556
}
5657
]
57-
},
58-
"name": "parameters"
58+
}
5959
}
6060
],
6161
"returnValueType": {
@@ -69,11 +69,11 @@
6969
"parameters": [
7070
{
7171
"nodeType": "parameter",
72+
"name": "externalProps",
7273
"type": {
7374
"nodeType": "reference",
7475
"typeName": "React.HTMLAttributes"
75-
},
76-
"name": "externalProps"
76+
}
7777
}
7878
],
7979
"returnValueType": {
@@ -90,4 +90,4 @@
9090
"visibility": "internal"
9191
}
9292
]
93-
}
93+
}

test/hook-multiple-parameters/output.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
]
5858
},
5959
"documentation": {
60-
"description": "The severity."
60+
"description": "The severity.",
61+
"defaultValue": "\"'low'\""
6162
}
6263
}
6364
],
@@ -69,4 +70,4 @@
6970
"visibility": "internal"
7071
}
7172
]
72-
}
73+
}

0 commit comments

Comments
 (0)