Skip to content

Commit 5fd357a

Browse files
feat: improved types predict in class methods
1 parent da7d9ca commit 5fd357a

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

typescript/type-converter.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,27 @@ const getName = (node, src) => {
5050
}
5151

5252
/**
53-
* Check type of node
53+
* Check type of node (dev only)
5454
* @param {node} node
5555
* @return {void} Console log predicted types
5656
*/
5757
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);
6666
}
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();
7074
}
7175

7276

@@ -85,10 +89,10 @@ const fillMethodComment = (comment, member, src) => {
8589
if (!comment.includes('@param')) {
8690
comment = convertParams(comment, member, src)
8791
}
88-
if (ts.isArrayTypeNode(member.type)) {
92+
if (member.type && ts.isArrayTypeNode(member.type)) {
8993
comment = convertMembers(comment, member.type, src)
9094
}
91-
if (!comment.includes('@return')) {
95+
if (member.type && !comment.includes('@return')) {
9296
const returnType = getTypeName(member.type, src)
9397
comment = appendComment(comment, `@return {${returnType}}`)
9498
}
@@ -195,14 +199,8 @@ module.exports = function typeConverter(src, filename = 'test.ts') {
195199
if (jsDocNode) {
196200
let comment = src.substring(jsDocNode.pos, jsDocNode.end)
197201
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);
206204
}
207205
if (ts.isTypeAliasDeclaration(statement)) {
208206
if (ts.isFunctionTypeNode(statement.type)) {
@@ -266,7 +264,7 @@ module.exports = function typeConverter(src, filename = 'test.ts') {
266264
const type = getTypeName(member.type, src)
267265
memberComment = appendComment(memberComment, `@type {${type}}`)
268266
}
269-
if (member.type && ts.isFunctionLike(member)) {
267+
if (ts.isFunctionLike(member)) {
270268
memberComment = fillMethodComment(memberComment, member, src)
271269
}
272270
if (modifiers.find((m => m === 'static'))) {

0 commit comments

Comments
 (0)