11import ts , { FunctionDeclaration } from 'typescript' ;
22import { type ParserContext } from '../parser' ;
3- import { getParameterDescriptionFromNode } from './documentationParser' ;
43import { resolveType } from './typeResolver' ;
54import { FunctionNode , CallSignature , Documentation , Parameter } from '../models' ;
65
@@ -34,8 +33,6 @@ function parseFunctionSignature(
3433 context : ParserContext ,
3534 skipResolvingComplexTypes : boolean = false ,
3635) : CallSignature {
37- const { checker } = context ;
38-
3936 // Node that possibly has JSDocs attached to it
4037 let documentationNodeCandidate : ts . Node | undefined = undefined ;
4138
@@ -66,40 +63,9 @@ function parseFunctionSignature(
6663 }
6764 }
6865
69- const parameterDescriptions = documentationNodeCandidate
70- ? getParameterDescriptionFromNode ( documentationNodeCandidate )
71- : { } ;
72-
73- const parameters = signature . parameters . map ( ( parameterSymbol ) => {
74- const parameterDeclaration = parameterSymbol . valueDeclaration as ts . ParameterDeclaration ;
75- const parameterType = resolveType (
76- checker . getTypeOfSymbolAtLocation ( parameterSymbol , parameterSymbol . valueDeclaration ! ) ,
77- parameterSymbol . getName ( ) ,
78- context ,
79- skipResolvingComplexTypes ,
80- ) ;
81-
82- const documentation = new Documentation ( parameterDescriptions [ parameterSymbol . getName ( ) ] ) ;
83- const initializer = parameterDeclaration . initializer ;
84- if ( initializer ) {
85- const initializerType = checker . getTypeAtLocation ( initializer ) ;
86- if ( initializerType . flags & ts . TypeFlags . Literal ) {
87- if ( initializerType . isStringLiteral ( ) ) {
88- documentation . defaultValue = `"${ initializer . getText ( ) } "` ;
89- } else {
90- documentation . defaultValue = initializer . getText ( ) ;
91- }
92- }
93- }
94-
95- const hasDocumentation = documentation . description || documentation . defaultValue ;
96-
97- return new Parameter (
98- parameterType ,
99- parameterSymbol . getName ( ) ,
100- hasDocumentation ? documentation : undefined ,
101- ) ;
102- } ) ;
66+ const parameters = signature . parameters . map ( ( parameterSymbol ) =>
67+ parseParameter ( parameterSymbol , context , skipResolvingComplexTypes ) ,
68+ ) ;
10369
10470 const returnValueType = resolveType (
10571 signature . getReturnType ( ) ,
@@ -109,3 +75,50 @@ function parseFunctionSignature(
10975
11076 return new CallSignature ( parameters , returnValueType ) ;
11177}
78+
79+ function parseParameter (
80+ parameterSymbol : ts . Symbol ,
81+ context : ParserContext ,
82+ skipResolvingComplexTypes : boolean ,
83+ ) : Parameter {
84+ const { checker } = context ;
85+ const parameterDeclaration = parameterSymbol . valueDeclaration as ts . ParameterDeclaration ;
86+ const parameterType = resolveType (
87+ checker . getTypeOfSymbolAtLocation ( parameterSymbol , parameterSymbol . valueDeclaration ! ) ,
88+ parameterSymbol . getName ( ) ,
89+ context ,
90+ skipResolvingComplexTypes ,
91+ ) ;
92+
93+ const summary = parameterSymbol
94+ . getDocumentationComment ( checker )
95+ . map ( ( comment ) => comment . text )
96+ . join ( '\n' )
97+ . replace ( / ^ [ \s - * : ] * / , '' ) ;
98+
99+ const documentation = summary ?. length ? new Documentation ( summary ) : undefined ;
100+
101+ let defaultValue : string | undefined ;
102+ const initializer = parameterDeclaration . initializer ;
103+ if ( initializer ) {
104+ const initializerType = checker . getTypeAtLocation ( initializer ) ;
105+ if ( initializerType . flags & ts . TypeFlags . Literal ) {
106+ if ( initializerType . isStringLiteral ( ) ) {
107+ defaultValue = `"${ initializerType . value } "` ;
108+ } else if ( initializerType . isLiteral ( ) ) {
109+ defaultValue = initializerType . value . toString ( ) ;
110+ } else {
111+ defaultValue = initializer . getText ( ) ;
112+ }
113+ }
114+ }
115+
116+ return new Parameter (
117+ parameterType ,
118+ parameterSymbol . getName ( ) ,
119+ documentation ,
120+ parameterDeclaration . questionToken !== undefined ||
121+ parameterDeclaration . initializer !== undefined ,
122+ defaultValue ,
123+ ) ;
124+ }
0 commit comments