diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-01-06 20:40:29 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-01-06 20:43:49 +0530 |
| commit | cda0419722b022ed0a8f3c4fd3122230b19a0c64 (patch) | |
| tree | 6d63849e950cc4c0257a2368560eb1bafefbd9d4 /src/runtime.ts | |
| parent | a56f093bf08780ab7ed7ef196f031c193a522922 (diff) | |
| download | ts-types-lang-cda0419722b022ed0a8f3c4fd3122230b19a0c64.tar.gz ts-types-lang-cda0419722b022ed0a8f3c4fd3122230b19a0c64.zip | |
feat: adds examples for everything
Diffstat (limited to '')
| -rw-r--r-- | src/runtime.ts | 31 |
1 files changed, 11 insertions, 20 deletions
diff --git a/src/runtime.ts b/src/runtime.ts index bac9415..f2fc516 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -2,8 +2,8 @@ import { Project, ScriptTarget, Type, Node, StringLiteral, TypeFormatFlags, Synt import path from 'path' import { promises as fs } from 'fs' import readline from 'readline'; -import { stdout } from 'process'; import { v4 as uuid } from 'uuid'; +import { match } from './util'; const rl = readline.createInterface({ input: process.stdin, @@ -23,20 +23,15 @@ const project = new Project({ const typeChecker = project.getTypeChecker() -const sourceFile = project.addSourceFileAtPath(path.resolve("./src/index.ts")) +const [filePath] = process.argv.slice(2) + +const sourceFile = project.addSourceFileAtPath(path.resolve(filePath)) const entryPoint = sourceFile.getExportedDeclarations().get('main')?.[0] const typeToString = (ty: Type | undefined): string => ty ? typeChecker.compilerObject.typeToString(ty.compilerType) : '' -const getPropertyType = (n: Node, prop: string): Type | undefined => { - const tt = typeChecker.getTypeAtLocation(n) - const propSym = tt.getProperty(prop) - const ty = propSym && typeChecker.getTypeOfSymbolAtLocation(propSym, n) - return ty -} - const typeRefNode = entryPoint?.getLastChild() const RESULT_TYPE_NAME = '__$result' @@ -55,13 +50,11 @@ const addResult = (name: string, ty: string): Node | undefined => { } } -const match = <K extends string, R>(k: K | undefined, pattern: { [key in K | '_']: () => R }) => - k && pattern[k] ? pattern[k]() : pattern._() - const customEffects: Record<string, (...args: Type[]) => void> = {} -const accumulateResults = async (effTyp: Type, node: Node): Promise<string[]> => { +const evaluateType = async (effTyp: Type, node: Node): Promise<string[]> => { const name = effTyp.getSymbol()?.getName() + // console.log(name) return match(name, { DefineEffect: async () => { @@ -83,7 +76,7 @@ const accumulateResults = async (effTyp: Type, node: Node): Promise<string[]> => const [strinTyp] = effTyp.getTypeArguments() const typString = typeToString(strinTyp) const string = JSON.parse(!typString.startsWith('"') ? `"${typString}"` : typString) - stdout.write(string); + process.stdout.write(string); return [] }, @@ -115,16 +108,14 @@ const accumulateResults = async (effTyp: Type, node: Node): Promise<string[]> => Bind: async () => { const [inputTyp, chainToKind] = effTyp.getTypeArguments() - const [resultKey] = inputTyp ? await accumulateResults(inputTyp, node) : [] + const [resultKey] = inputTyp ? await evaluateType(inputTyp, node) : [] const hash = uuid() const compNode = addResult(hash, `(${typeToString(chainToKind)} & { input: ${RESULT_TYPE_NAME}[${JSON.stringify(resultKey)}]['output'] })['return']`) const compTyp = compNode?.getType().getProperty('output')?.getTypeAtLocation(node) - if (compTyp) - return accumulateResults(compTyp, node) - return [] + return compTyp ? await evaluateType(compTyp, node) : [] }, GetEnv: async () => { @@ -161,7 +152,7 @@ const accumulateResults = async (effTyp: Type, node: Node): Promise<string[]> => const [effectTyps] = effTyp.getTypeArguments() const effectResults: string[] = [] for (const item of effectTyps?.getTupleElements() ?? []) { - effectResults.push(...(await accumulateResults(item, node))) + effectResults.push(...(await evaluateType(item, node))) } const hash = uuid() @@ -189,7 +180,7 @@ const main = async () => { if (resultType) { const effects = resultType.isTuple() ? resultType.getTupleElements() : [resultType] for (const typ of effects) { - await accumulateResults(typ, typeRefNode) + await evaluateType(typ, typeRefNode) } } } |
