@@ -6,37 +6,11 @@ import {
6
6
ts ,
7
7
} from 'ts-morph'
8
8
9
- export type TsInfo =
10
- | {
11
- // example: type A = { k: v }
12
- type : 'object-literal'
13
- name : string
14
- description : string
15
- properties : TsPropertyOrMethodInfo [ ]
16
- }
17
- | {
18
- // example: interface MyInterface { k: v }
19
- type : 'interface'
20
- name : string
21
- description : string
22
- properties : TsPropertyOrMethodInfo [ ]
23
- }
24
- | {
25
- // complex type literal
26
- // example: type A = 'asd' | 123
27
- type : 'other'
28
- name : string
29
- description : string
30
- text : string
31
- }
32
-
33
- export interface TsPropertyOrMethodInfo {
34
- name : string
35
- type : string
36
- description : string
37
- defaultValue : string | undefined
38
- optional : boolean
39
- }
9
+ import type {
10
+ TsInfo ,
11
+ TsPropertyOrMethodInfo ,
12
+ CallSignatureInfo ,
13
+ } from '../../../../clientTypes'
40
14
41
15
const defaultTsConfig : ts . CompilerOptions = {
42
16
target : ts . ScriptTarget . ESNext ,
@@ -92,12 +66,15 @@ export function collectInterfaceInfo(
92
66
93
67
if ( Node . isTypeLiteral ( typeNode ) ) {
94
68
// example: type A = { k: v }
95
- const members = handleTypeElementMembered ( typeNode , typeChecker )
69
+ const { members, callSignatures, constructSignatures } =
70
+ handleTypeElementMembered ( typeNode , typeChecker )
96
71
return {
97
72
type : 'object-literal' ,
98
73
name,
99
74
description,
100
75
properties : members ,
76
+ callSignatures,
77
+ constructSignatures,
101
78
}
102
79
} else {
103
80
// example: type A = 'asd' | 123
@@ -122,8 +99,16 @@ export function collectInterfaceInfo(
122
99
return jsDoc . getDescription ( ) . trim ( )
123
100
} )
124
101
. join ( '\n\n' )
125
- const members = handleTypeElementMembered ( node , typeChecker )
126
- return { type : 'interface' , name, description, properties : members }
102
+ const { members, callSignatures, constructSignatures } =
103
+ handleTypeElementMembered ( node , typeChecker )
104
+ return {
105
+ type : 'interface' ,
106
+ name,
107
+ description,
108
+ properties : members ,
109
+ callSignatures,
110
+ constructSignatures,
111
+ }
127
112
}
128
113
129
114
throw new Error ( 'unexpected node type: ' + node . getKindName ( ) )
@@ -136,12 +121,16 @@ export function collectInterfaceInfo(
136
121
function handleTypeElementMembered (
137
122
node : TypeElementMemberedNode & Node ,
138
123
typeChecker : TypeChecker
139
- ) : TsPropertyOrMethodInfo [ ] {
140
- const result : TsPropertyOrMethodInfo [ ] = [ ]
124
+ ) : {
125
+ members : TsPropertyOrMethodInfo [ ]
126
+ callSignatures : CallSignatureInfo [ ]
127
+ constructSignatures : CallSignatureInfo [ ]
128
+ } {
129
+ const members : TsPropertyOrMethodInfo [ ] = [ ]
141
130
// or use node.getSymbol()?.getMembers() ?
142
131
const nodeType = node . getType ( )
143
- const properties = nodeType . getProperties ( )
144
- for ( const prop of properties ) {
132
+ // https://stackoverflow.com/a/68623960
133
+ for ( const prop of nodeType . getProperties ( ) ) {
145
134
const name = prop . getName ( )
146
135
const description = ts . displayPartsToString (
147
136
prop . compilerSymbol . getDocumentationComment ( typeChecker . compilerObject )
@@ -165,15 +154,40 @@ function handleTypeElementMembered(
165
154
return res
166
155
} ) ( )
167
156
const optional = prop . isOptional ( )
168
- result . push ( {
157
+ members . push ( {
169
158
name,
170
159
description,
171
160
type,
172
161
defaultValue,
173
162
optional,
174
163
} )
175
164
}
176
- return result
165
+
166
+ const callSignatures : CallSignatureInfo [ ] = [ ]
167
+ for ( const sig of nodeType . getCallSignatures ( ) ) {
168
+ const description = ts . displayPartsToString (
169
+ sig . compilerSignature . getDocumentationComment ( typeChecker . compilerObject )
170
+ )
171
+ const type = sig . getDeclaration ( ) . getText ( )
172
+ callSignatures . push ( {
173
+ description,
174
+ type,
175
+ } )
176
+ }
177
+
178
+ const constructSignatures : CallSignatureInfo [ ] = [ ]
179
+ for ( const sig of nodeType . getConstructSignatures ( ) ) {
180
+ const description = ts . displayPartsToString (
181
+ sig . compilerSignature . getDocumentationComment ( typeChecker . compilerObject )
182
+ )
183
+ const type = sig . getDeclaration ( ) . getText ( )
184
+ constructSignatures . push ( {
185
+ description,
186
+ type,
187
+ } )
188
+ }
189
+
190
+ return { members, callSignatures, constructSignatures }
177
191
}
178
192
179
193
// an alternative way to implement handleTypeElementMembered
0 commit comments