try getting resolved type arguments (WIP)

This commit is contained in:
Jos de Jong 2023-09-11 16:07:52 +02:00
parent 232d5d4a96
commit 24bcf34b60
3 changed files with 83 additions and 5 deletions

View File

@ -2,12 +2,14 @@ lockfileVersion: 5.4
specifiers:
'@types/node': 20.5.7
ts-macros: 2.4.1
ts-node: 10.9.1
ttypescript: 1.5.15
typescript: 4.7.4
dependencies:
'@types/node': 20.5.7
ts-macros: 2.4.1_typescript@4.7.4
devDependencies:
ts-node: 10.9.1_l7whiu4appksmcywzzf5ucsgha
@ -121,6 +123,14 @@ packages:
engines: {node: '>= 0.4'}
dev: true
/ts-macros/2.4.1_typescript@4.7.4:
resolution: {integrity: sha512-DmrftFZ5pgM7dw8ySYcd90wGEfjp7yx9MFw/YyJHNdwKYdUb//lcy/XI/Lvg3LbrGIbjBMEj1rzuc5Kfkzvafg==}
peerDependencies:
typescript: 4.7.x || 4.8.x || 4.9.x || 5.0.x || 5.1.x
dependencies:
typescript: 4.7.4
dev: false
/ts-node/10.9.1_l7whiu4appksmcywzzf5ucsgha:
resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==}
hasBin: true
@ -168,7 +178,6 @@ packages:
resolution: {integrity: sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==}
engines: {node: '>=4.2.0'}
hasBin: true
dev: true
/v8-compile-cache-lib/3.0.1:
resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==}

View File

@ -1,8 +1,19 @@
import { $reflect } from '../core/$reflect.js'
import { Dependencies } from '../interfaces/type.js'
export const square = $reflect(<T>(dep: {
multiply: (a: T, b: T) => T,
unaryMinus: (x: T) => T, // just for the experiment
}): (a: T) => T =>
// unaryMinus dep is just for the experiment
// FIXME: the typescript plugin should resolve Dependencies<'multiply' | 'unaryMinus', T>
export const square = $reflect(<T>(dep: Dependencies<'multiply' | 'unaryMinus', T>): (a: T) => T =>
z => dep.multiply(z, z)
)
// export const square2 = function $reflect<T>(dep: Dependencies<'multiply' | 'unaryMinus', T>) {
// return (z: T) => dep.multiply(z, z)
// }
// export const square = $reflect(<T>(dep: {
// multiply: (a: T, b: T) => T,
// unaryMinus: (x: T) => T, // just for the experiment
// }): (a: T) => T =>
// z => dep.multiply(z, z)
// )

View File

@ -32,11 +32,39 @@ const transformer: ts.TransformerFactory<ts.SourceFile> = context => {
// unaryMinus: (x: number) => number, // just for the experiment
// }
// WIP
// @ts-ignore
const type = checker.getTypeAtLocation(paramNode)
const paramType = checker.typeToString(type, paramNode, ts.TypeFormatFlags.InTypeAlias)
// const paramType = checker.typeToString(type)
// TDOO: get checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration)
console.log('PLUGIN: PARAM TYPE STRING', paramType)
// { multiply: (a: number, b: number) => number; unaryMinus: (x: number) => number; }
// WIP
// For a function definition
// const signature = checker.getResolvedSignature(node);
// if (signature != null) {
// // outputs -- (Ctor: Ctor<Bar>): void
// console.log(signature)
// console.log('TEST 1', checker.signatureToString(signature));
// // @ts-ignore
// // console.log('TEST 2', checker.getResolvedSignatureForStringLiteralCompletions(node));
// const params = signature.getParameters();
// for (const param of params) {
// const type = checker.getTypeOfSymbolAtLocation(param, node);
// // outputs -- Ctor<Bar>
// console.log('TEST 3', checker.typeToString(type));
// }
// }
// WIP
const type0 = resolveTypeArgumentOfCall(checker, node, 0)
// @ts-ignore
const typeStr = checker.typeToString(type0);
console.log('PLUGIN: RESOLVED TYPE ARGUMENT', typeStr)
const depsAndReturnType = `{ deps: ${paramType}; return: ${returnType} }`
// Now we insert a second argument to the $reflect function call: a string with the types
@ -53,4 +81,34 @@ const transformer: ts.TransformerFactory<ts.SourceFile> = context => {
}
}
// WIP
function resolveTypeArgumentOfCall(checker: ts.TypeChecker, macroCall: ts.CallExpression, typeIndex: number) : ts.Type | undefined {
// @ts-ignore
console.log( 'TEST D', macroCall.arguments[0].typeParameters)
if (!macroCall.typeArguments || !macroCall.typeArguments[typeIndex]) return;
const type = checker.getTypeAtLocation(macroCall.typeArguments[typeIndex]);
console.log( 'TEST A', type, macroCall.typeArguments )
return type
// const lastMacroCall = this.getLastMacro();
// if (!lastMacroCall) return type;
// if (type.isTypeParameter()) {
// const resolvedTypeParameterIndex = lastMacroCall.macro.typeParams.findIndex(arg => this.checker.getTypeAtLocation(arg) === type);
// if (resolvedTypeParameterIndex === -1) return;
// if (lastMacroCall.call && ts.isCallExpression(lastMacroCall.call)) {
// const resolvedTypeParam = lastMacroCall.call.typeArguments?.[resolvedTypeParameterIndex];
// if (!resolvedTypeParam) return resolveTypeArguments(this.checker, lastMacroCall.call)[resolvedTypeParameterIndex];
// return this.checker.getTypeAtLocation(resolvedTypeParam);
// } else return;
// } else {
// const allParams = lastMacroCall.macro.typeParams.map(p => this.checker.getTypeAtLocation(p));
// const replacementTypes = resolveTypeArguments(this.checker, lastMacroCall.call as ts.CallExpression);
// return resolveTypeWithTypeParams(type, allParams, replacementTypes);
// }
}
export default transformer