Compare commits

..

No commits in common. "232d5d4a96b3d9face1ba428224d631d28a44330" and "26631febf905cca9a257131c0a8f5589143ad641" have entirely different histories.

4 changed files with 43 additions and 43 deletions

View file

@ -1,11 +0,0 @@
export function $reflect<T>(arg: T, types?: string) : T {
// TODO: implement typed-function for real
if (!types) {
console.error('types should be resolved with runtime type information by the TypeScript plugin')
}
console.log(`INFER: Creating function with types ${types}`)
// TODO: here we can now turn the inputs into a real typed-function with a signature
return arg
}

11
src/core/typed.ts Normal file
View file

@ -0,0 +1,11 @@
export function typed<T>(name: string, types: string, arg: T) : T {
// TODO: implement typed-function for real
if (types === '__infer__') {
console.error('__infer__ should be replaced with runtime type information by the TypeScript plugin')
}
console.log(`TYPED-FUNCTION: Creating function "${name}" with types ${types}`)
// TODO: here we can now turn the inputs into a real typed-function with a signature
return arg
}

View file

@ -1,6 +1,6 @@
import { $reflect } from '../core/$reflect.js' import { typed } from '../core/typed.js'
export const square = $reflect(<T>(dep: { export const square = typed('square', '__infer__', <T>(dep: {
multiply: (a: T, b: T) => T, multiply: (a: T, b: T) => T,
unaryMinus: (x: T) => T, // just for the experiment unaryMinus: (x: T) => T, // just for the experiment
}): (a: T) => T => }): (a: T) => T =>

View file

@ -7,43 +7,43 @@ const transformer: ts.TransformerFactory<ts.SourceFile> = context => {
return sourceFile => { return sourceFile => {
const visitor = (node: ts.Node): ts.Node => { const visitor = (node: ts.Node): ts.Node => {
// we're looking for a function call like $reflect(deps => ...)
// @ts-ignore // @ts-ignore
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.escapedText === '$reflect') { if (ts.isStringLiteral(node) && node.text === '__infer__') {
console.log('PLUGIN: FOUND AN OCCURRENCE OF $reflect') console.log('PLUGIN: FOUND AN OCCURRENCE OF __infer__')
// console.log('PARENT')
// console.log(node)
// TODO: validate that argNode is an ArrowFunction // we're looking for a function call like typed('name', '__infer__', deps => ...)
// @ts-ignore const parentNode = node.parent
const argNode = node.arguments[0] if (ts.isCallExpression(parentNode) && ts.isIdentifier(parentNode.expression) && parentNode.expression.escapedText === 'typed') {
// @ts-ignore // console.log('PARENT')
const returnType = argNode.type.getText(sourceFile) // console.log(parentNode)
console.log('PLUGIN: RETURN TYPE')
console.log(returnType)
// (a: number) => number
// @ts-ignore // TODO: validate that argNode is an ArrowFunction
const paramNode = argNode.parameters[0] // @ts-ignore
const paramTypeSrc = paramNode.type.getText(sourceFile) const argNode = parentNode.arguments[2]
console.log('PLUGIN: PARAM TYPE SRC', paramTypeSrc) // @ts-ignore
// { const returnType = argNode.type.getText(sourceFile)
// multiply: (a: number, b: number) => number, console.log('PLUGIN: RETURN TYPE')
// unaryMinus: (x: number) => number, // just for the experiment console.log(returnType)
// } // (a: number) => number
const type = checker.getTypeAtLocation(paramNode) // @ts-ignore
const paramType = checker.typeToString(type, paramNode, ts.TypeFormatFlags.InTypeAlias) const paramNode = argNode.parameters[0]
console.log('PLUGIN: PARAM TYPE STRING', paramType) const paramTypeSrc = paramNode.type.getText(sourceFile)
// { multiply: (a: number, b: number) => number; unaryMinus: (x: number) => number; } console.log('PLUGIN: PARAM TYPE SRC', paramTypeSrc)
// {
// multiply: (a: number, b: number) => number,
// unaryMinus: (x: number) => number, // just for the experiment
// }
const depsAndReturnType = `{ deps: ${paramType}; return: ${returnType} }` const type = checker.getTypeAtLocation(paramNode)
const paramType = checker.typeToString(type, paramNode, ts.TypeFormatFlags.InTypeAlias)
console.log('PLUGIN: PARAM TYPE STRING', paramType)
// { multiply: (a: number, b: number) => number; unaryMinus: (x: number) => number; }
// Now we insert a second argument to the $reflect function call: a string with the types const depsAndReturnType = `{ deps: ${paramType}; return: ${returnType} }`
// @ts-ignore
node.arguments.push(ts.factory.createStringLiteral(depsAndReturnType))
return node return ts.factory.createStringLiteral(depsAndReturnType)
}
} }
return ts.visitEachChild(node, visitor, context) return ts.visitEachChild(node, visitor, context)