Skip to content

Commit

Permalink
Improve name infering (see #72)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipatalas committed Sep 17, 2022
1 parent 6353b7f commit 67d0911
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/utils/infer-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ export const inferVarTemplateName = (node: ts.Node): string[] => {
return [lowerFirst(inferNewExpressionVar(node))]
} else if (ts.isCallExpression(node)) {
const methodName = getMethodName(node)
const name = MethodCallRegex.exec(methodName)?.groups?.name
if (!name) {
if (!methodName) {
return
}

return getUniqueVariants(name).map(lowerFirst)
return getUniqueVariants(methodName).map(lowerFirst)
}
}

Expand All @@ -27,7 +26,7 @@ export const inferForVarTemplate = (node: ts.Node): string[] => {
return
}

const clean = subjectName?.replace(/^(?:all)?(.+?)(?:List)?$/, "$1")
const clean = subjectName.replace(/^(?:all)?(.+?)(?:List)?$/, "$1")

return getUniqueVariants(clean)
.map(pluralize.singular)
Expand All @@ -46,14 +45,20 @@ function getForExpressionName(node: ts.Node) {
return node.text
} else if (ts.isPropertyAccessExpression(node)) {
return node.name.text
} else if (ts.isCallExpression(node)) {
return getMethodName(node)
}
}

function getMethodName(node: ts.CallExpression) {
if (ts.isIdentifier(node.expression)) {
return node.expression.text
} else if (ts.isPropertyAccessExpression(node.expression)) {
return node.expression.name.text
return MethodCallRegex.exec(getText())?.groups?.name

function getText() {
if (ts.isIdentifier(node.expression)) {
return node.expression.text
} else if (ts.isPropertyAccessExpression(node.expression)) {
return node.expression.name.text
}
}
}

Expand Down

0 comments on commit 67d0911

Please sign in to comment.