Skip to content

Commit c0f9d1d

Browse files
feat: predict function types and params
1 parent eac1043 commit c0f9d1d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

fixtures/typescript/functions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Add
3+
*/
4+
function add(x: number, y: number): number {
5+
return x + y;
6+
}
7+
8+
/**
9+
* My Add
10+
*/
11+
let myAdd = function (x: number, y: number): number {
12+
return x + y;
13+
};

typescript/type-converter.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ const getName = (node, src) => {
4949
return name
5050
}
5151

52+
/**
53+
* Check type of node
54+
* @param {node} node
55+
* @return {void} Console log predicted types
56+
*/
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)
66+
}
67+
})
68+
.forEach((key) => console.log(key))
69+
console.groupEnd()
70+
}
71+
72+
5273
/**
5374
* converts function parameters to @params
5475
*
@@ -149,7 +170,15 @@ module.exports = function typeConverter(src, filename = 'test.ts') {
149170
if (jsDocNode) {
150171
let comment = src.substring(jsDocNode.pos, jsDocNode.end)
151172
const name = getName(statement, src)
173+
checkType(statement)
152174

175+
if (ts.isFunctionLikeDeclaration(statement)) {
176+
const returnType = getTypeName(statement.type, src)
177+
comment = appendComment(comment, `@method ${name}`)
178+
comment = convertParams(comment, statement, src)
179+
comment = appendComment(comment, `@return {${returnType}}`)
180+
return comment;
181+
}
153182
if (ts.isTypeAliasDeclaration(statement)) {
154183
if (ts.isFunctionTypeNode(statement.type)) {
155184
comment = appendComment(comment, `@typedef {function} ${name}`)

0 commit comments

Comments
 (0)