@@ -50,23 +50,27 @@ const getName = (node, src) => {
50
50
}
51
51
52
52
/**
53
- * Check type of node
53
+ * Check type of node (dev only)
54
54
* @param {node } node
55
55
* @return {void } Console log predicted types
56
56
*/
57
57
function checkType ( node ) {
58
- console . group ( node . name ?. escapedText )
59
- Object . keys ( ts )
60
- . filter ( ( key ) => typeof ts [ key ] === ' function' && key . startsWith ( 'is' ) )
61
- . filter ( ( key ) => {
62
- try {
63
- return ts [ key ] ( node ) === true
64
- } catch ( error ) {
65
- // console.log(error)
58
+ console . group ( node . name ?. escapedText ) ;
59
+ const predictedTypes = Object . keys ( ts ) . reduce ( ( acc , key ) => {
60
+ if ( typeof ts [ key ] !== " function" && ! key . startsWith ( "is" ) ) {
61
+ return acc ;
62
+ }
63
+ try {
64
+ if ( ts [ key ] ( node ) === true ) {
65
+ acc . push ( key ) ;
66
66
}
67
- } )
68
- . forEach ( ( key ) => console . log ( key ) )
69
- console . groupEnd ( )
67
+ } catch ( error ) {
68
+ return acc ;
69
+ }
70
+ return acc ;
71
+ } , [ ] ) ;
72
+ console . log ( predictedTypes ) ;
73
+ console . groupEnd ( ) ;
70
74
}
71
75
72
76
@@ -85,10 +89,10 @@ const fillMethodComment = (comment, member, src) => {
85
89
if ( ! comment . includes ( '@param' ) ) {
86
90
comment = convertParams ( comment , member , src )
87
91
}
88
- if ( ts . isArrayTypeNode ( member . type ) ) {
92
+ if ( member . type && ts . isArrayTypeNode ( member . type ) ) {
89
93
comment = convertMembers ( comment , member . type , src )
90
94
}
91
- if ( ! comment . includes ( '@return' ) ) {
95
+ if ( member . type && ! comment . includes ( '@return' ) ) {
92
96
const returnType = getTypeName ( member . type , src )
93
97
comment = appendComment ( comment , `@return {${ returnType } }` )
94
98
}
@@ -195,14 +199,8 @@ module.exports = function typeConverter(src, filename = 'test.ts') {
195
199
if ( jsDocNode ) {
196
200
let comment = src . substring ( jsDocNode . pos , jsDocNode . end )
197
201
const name = getName ( statement , src )
198
- checkType ( statement )
199
-
200
- if ( ts . isFunctionLikeDeclaration ( statement ) ) {
201
- const returnType = getTypeName ( statement . type , src )
202
- comment = appendComment ( comment , `@method ${ name } ` )
203
- comment = convertParams ( comment , statement , src )
204
- comment = appendComment ( comment , `@return {${ returnType } }` )
205
- return comment ;
202
+ if ( ts . isFunctionDeclaration ( statement ) ) {
203
+ return fillMethodComment ( comment , statement , src ) ;
206
204
}
207
205
if ( ts . isTypeAliasDeclaration ( statement ) ) {
208
206
if ( ts . isFunctionTypeNode ( statement . type ) ) {
@@ -266,7 +264,7 @@ module.exports = function typeConverter(src, filename = 'test.ts') {
266
264
const type = getTypeName ( member . type , src )
267
265
memberComment = appendComment ( memberComment , `@type {${ type } }` )
268
266
}
269
- if ( member . type && ts . isFunctionLike ( member ) ) {
267
+ if ( ts . isFunctionLike ( member ) ) {
270
268
memberComment = fillMethodComment ( memberComment , member , src )
271
269
}
272
270
if ( modifiers . find ( ( m => m === 'static' ) ) ) {
0 commit comments