Skip to content

Commit

Permalink
feat: predict function types and params
Browse files Browse the repository at this point in the history
  • Loading branch information
ariansobczak-rst committed Jan 20, 2022
1 parent eac1043 commit c0f9d1d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions fixtures/typescript/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Add
*/
function add(x: number, y: number): number {
return x + y;
}

/**
* My Add
*/
let myAdd = function (x: number, y: number): number {
return x + y;
};
29 changes: 29 additions & 0 deletions typescript/type-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ const getName = (node, src) => {
return name
}

/**
* Check type of node
* @param {node} node
* @return {void} Console log predicted types
*/
function checkType(node) {
console.group(node.name?.escapedText)
Object.keys(ts)
.filter((key) => typeof ts[key] === 'function' && key.startsWith('is'))
.filter((key) => {
try {
return ts[key](node) === true
} catch (error) {
// console.log(error)
}
})
.forEach((key) => console.log(key))
console.groupEnd()
}


/**
* converts function parameters to @params
*
Expand Down Expand Up @@ -149,7 +170,15 @@ module.exports = function typeConverter(src, filename = 'test.ts') {
if (jsDocNode) {
let comment = src.substring(jsDocNode.pos, jsDocNode.end)
const name = getName(statement, src)
checkType(statement)

if (ts.isFunctionLikeDeclaration(statement)) {
const returnType = getTypeName(statement.type, src)
comment = appendComment(comment, `@method ${name}`)
comment = convertParams(comment, statement, src)
comment = appendComment(comment, `@return {${returnType}}`)
return comment;
}
if (ts.isTypeAliasDeclaration(statement)) {
if (ts.isFunctionTypeNode(statement.type)) {
comment = appendComment(comment, `@typedef {function} ${name}`)
Expand Down

0 comments on commit c0f9d1d

Please sign in to comment.