aboutsummaryrefslogtreecommitdiff
path: root/src/util.ts
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2023-01-14 17:12:48 +0530
committerAkshay Nair <phenax5@gmail.com>2023-01-14 17:12:48 +0530
commitcd11925707c42843df195d9b2efb3c77b5de793b (patch)
tree0871b0041597096fdbf7c49416f96fa70da00e3c /src/util.ts
parentd362ef26db894ea52c6abd34489aa9efdbc0c89b (diff)
downloadts-types-lang-cd11925707c42843df195d9b2efb3c77b5de793b.tar.gz
ts-types-lang-cd11925707c42843df195d9b2efb3c77b5de793b.zip
feat: adds cleanup of result nodes
Diffstat (limited to 'src/util.ts')
-rw-r--r--src/util.ts63
1 files changed, 62 insertions, 1 deletions
diff --git a/src/util.ts b/src/util.ts
index f7bb20a..2746746 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -1,4 +1,4 @@
-import { Type } from 'ts-morph'
+import { SyntaxKind, Type } from 'ts-morph'
import { Ctx } from './types'
export const match = <K extends string, R>(
@@ -13,3 +13,64 @@ export const evalList = async (ctx: Ctx, effectTyps: Type[]) => {
}
return effectResults
}
+
+export const applyFunc = (ctx: Ctx, fn: Type | undefined, val: string): Type => {
+ const resultType = (() => {
+ const baseTypes = fn
+ ?.getBaseTypes()
+ .flatMap((t) => t.getSymbol()?.getName() ?? [])
+
+ if (!!fn?.getProperty('return') || baseTypes?.includes('Kind1')) {
+ const [key, resultNode] = ctx.createResult(
+ `(${ctx.typeToString(fn)} & { input: ${val} })['return']`
+ )
+ ctx.removeResult(key)
+
+ return resultNode
+ ?.getType()
+ .getProperty('output')
+ ?.getTypeAtLocation(resultNode)
+ } else {
+ const [key, resultNode] = ctx.createResult(
+ `ReturnType<${ctx.typeToString(fn)}>`
+ )
+ ctx.removeResult(key)
+
+ const resValueNode = resultNode
+ ?.asKind(SyntaxKind.PropertySignature)
+ ?.getChildAtIndexIfKind(2, SyntaxKind.TypeLiteral)
+ ?.getProperty('output')
+ ?.getChildAtIndexIfKind(2, SyntaxKind.TypeReference)
+
+ const functionNode = resValueNode
+ ?.getChildAtIndexIfKind(1, SyntaxKind.SyntaxList)
+ ?.getFirstChildIfKind(SyntaxKind.FunctionType)
+
+ if (functionNode) {
+ const typeParameters = functionNode.getTypeParameters() ?? []
+
+ if (typeParameters.length > 0) {
+ const constraint = typeParameters[0]?.getConstraint()
+ if (constraint) {
+ constraint?.replaceWithText(val)
+ } else {
+ typeParameters[0]?.setConstraint(val)
+ }
+ }
+
+ return resValueNode?.getType()
+ }
+ }
+
+ return undefined
+ })()
+
+ // TODO: Cleanup unwanted result node values
+
+ if (!resultType) {
+ throw new Error('Couldnt get result for function application')
+ }
+
+ return resultType
+}
+