make defining __infer__
redundant
This commit is contained in:
parent
7fc9d2a2f3
commit
232d5d4a96
@ -1,7 +1,7 @@
|
||||
export function $reflect<T>(types: string, arg: T) : T {
|
||||
export function $reflect<T>(arg: T, types?: string) : T {
|
||||
// TODO: implement typed-function for real
|
||||
if (types === '__infer__') {
|
||||
console.error('__infer__ should be replaced with runtime type information by the TypeScript plugin')
|
||||
if (!types) {
|
||||
console.error('types should be resolved with runtime type information by the TypeScript plugin')
|
||||
}
|
||||
|
||||
console.log(`INFER: Creating function with types ${types}`)
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { $reflect } from '../core/$reflect.js'
|
||||
|
||||
export const square = $reflect('__infer__', <T>(dep: {
|
||||
export const square = $reflect(<T>(dep: {
|
||||
multiply: (a: T, b: T) => T,
|
||||
unaryMinus: (x: T) => T, // just for the experiment
|
||||
}): (a: T) => T =>
|
||||
|
@ -7,19 +7,16 @@ const transformer: ts.TransformerFactory<ts.SourceFile> = context => {
|
||||
|
||||
return sourceFile => {
|
||||
const visitor = (node: ts.Node): ts.Node => {
|
||||
// we're looking for a function call like $reflect(deps => ...)
|
||||
// @ts-ignore
|
||||
if (ts.isStringLiteral(node) && node.text === '__infer__') {
|
||||
console.log('PLUGIN: FOUND AN OCCURRENCE OF __infer__')
|
||||
|
||||
// we're looking for a function call like typed('name', '__infer__', deps => ...)
|
||||
const parentNode = node.parent
|
||||
if (ts.isCallExpression(parentNode) && ts.isIdentifier(parentNode.expression) && parentNode.expression.escapedText === '$reflect') {
|
||||
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.escapedText === '$reflect') {
|
||||
console.log('PLUGIN: FOUND AN OCCURRENCE OF $reflect')
|
||||
// console.log('PARENT')
|
||||
// console.log(parentNode)
|
||||
// console.log(node)
|
||||
|
||||
// TODO: validate that argNode is an ArrowFunction
|
||||
// @ts-ignore
|
||||
const argNode = parentNode.arguments[1]
|
||||
const argNode = node.arguments[0]
|
||||
// @ts-ignore
|
||||
const returnType = argNode.type.getText(sourceFile)
|
||||
console.log('PLUGIN: RETURN TYPE')
|
||||
@ -42,8 +39,11 @@ const transformer: ts.TransformerFactory<ts.SourceFile> = context => {
|
||||
|
||||
const depsAndReturnType = `{ deps: ${paramType}; return: ${returnType} }`
|
||||
|
||||
return ts.factory.createStringLiteral(depsAndReturnType)
|
||||
}
|
||||
// Now we insert a second argument to the $reflect function call: a string with the types
|
||||
// @ts-ignore
|
||||
node.arguments.push(ts.factory.createStringLiteral(depsAndReturnType))
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
return ts.visitEachChild(node, visitor, context)
|
||||
|
Loading…
Reference in New Issue
Block a user