@@ -61,18 +61,22 @@ export const requiredTypeofs = {};
61
61
* This function handles various TypeScript AST node types and converts them into a string
62
62
* or an object representing the type.
63
63
*
64
- * @param {ts.TypeNode|ts.Identifier|ts.QualifiedName } node - The TypeScript AST node to convert.
64
+ * @param {ts.TypeNode|ts.Identifier|ts.QualifiedName|undefined } node - The TypeScript AST node to convert.
65
65
* @returns {string | number | boolean | {type: string, [key: string]: any} | undefined } The source string/number,
66
66
* or an object with type information based on the node, or `undefined` if the node kind is not handled.
67
67
*/
68
68
function toSourceTS ( node ) {
69
+ if ( ! node ) {
70
+ return 'undefined' ;
71
+ }
69
72
const { typeArguments, typeName} = node ;
70
73
const kind_ = ts . SyntaxKind [ node . kind ] ;
71
74
const {
72
75
AnyKeyword, // parseType('any' ).kind === ts.SyntaxKind.AnyKeyword && toSourceTS(parseType('any')) === 'any'
73
76
ArrayType, // parseType('number[]' ).kind === ts.SyntaxKind.ArrayType // todo toSourceTS(parseType('number[]')) === {type: 'array etc.
74
77
BooleanKeyword, // parseType("boolean" ).kind === ts.SyntaxKind.BooleanKeyword
75
78
FunctionType, // parseType("() => void" ).kind === ts.SyntaxKind.FunctionType
79
+ JSDocFunctionType, // parseType("function (number, number): number").kind === ts.SyntaxKind.JSDocFunctionType
76
80
Identifier, // parseType("{a: 1, b: 2}" ).members[0].name.kind === ts.SyntaxKind.Identifier
77
81
IntersectionType, // parseType("1 & 2" ).kind === ts.SyntaxKind.IntersectionType
78
82
JSDocAllType, // parseType("*" ).kind === ts.SyntaxKind.JSDocAllType
@@ -144,12 +148,20 @@ function toSourceTS(node) {
144
148
const ret = toSourceTS ( node . type ) ;
145
149
return { type : 'new' , parameters, ret} ;
146
150
}
147
- case FunctionType :
151
+ case FunctionType : {
148
152
if ( ! ts . isFunctionTypeNode ( node ) ) {
149
153
throw Error ( "Impossible" ) ;
150
154
}
151
155
const parameters = node . parameters . map ( toSourceTS ) ;
152
156
return { type : 'function' , parameters} ;
157
+ }
158
+ case JSDocFunctionType : {
159
+ if ( ! ts . isJSDocFunctionType ( node ) ) {
160
+ throw Error ( "Impossible" ) ;
161
+ }
162
+ const parameters = node . parameters . map ( toSourceTS ) ;
163
+ return { type : 'function' , parameters} ;
164
+ }
153
165
case IndexedAccessType :
154
166
if ( ! ts . isIndexedAccessTypeNode ( node ) ) {
155
167
throw Error ( "Impossible" ) ;
0 commit comments