75 lines
2.5 KiB
TypeScript
75 lines
2.5 KiB
TypeScript
import * as ts from "typescript"
|
|
|
|
// based on: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#using-the-type-checker
|
|
|
|
infer2(process.argv.slice(2), {
|
|
target: ts.ScriptTarget.ES5,
|
|
module: ts.ModuleKind.CommonJS
|
|
})
|
|
|
|
function infer2(
|
|
fileNames: string[],
|
|
options: ts.CompilerOptions
|
|
): void {
|
|
const program = ts.createProgram(fileNames, options)
|
|
const typeChecker = program.getTypeChecker()
|
|
|
|
for (const sourceFile of program.getSourceFiles()) {
|
|
if (!sourceFile.isDeclarationFile) {
|
|
ts.forEachChild(sourceFile, visit)
|
|
}
|
|
}
|
|
|
|
return
|
|
|
|
function visit(node: ts.Node) {
|
|
// // Only consider exported nodes
|
|
// if (!isNodeExported(node)) {
|
|
// return;
|
|
// }
|
|
|
|
// console.log('Node', node.kind, node?.['name']?.escapedText)
|
|
|
|
if (ts.isModuleDeclaration(node)) {
|
|
// This is a namespace, visit its children
|
|
console.log('check')
|
|
ts.forEachChild(node, visit);
|
|
} else if (ts.isTypeAliasDeclaration(node)) {
|
|
console.log('isTypeAliasDeclaration', node.name.escapedText)
|
|
|
|
let symbol = typeChecker.getSymbolAtLocation(node.name);
|
|
if (symbol) {
|
|
const symbolType = typeChecker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration)
|
|
const symbolSignature = typeChecker.getSignaturesOfType(symbolType, ts.SignatureKind.Call)
|
|
|
|
// checker.getResolvedSignature(symbol)
|
|
console.log('symbol', symbol.getName(), symbolSignature)
|
|
|
|
// getTypeOfSymbolAtLocation
|
|
// getResolvedSignature
|
|
}
|
|
} else if (ts.isCallExpression(node)) {
|
|
console.log('isCallExpression', node.expression)
|
|
} else if (ts.isFunctionDeclaration(node)) {
|
|
console.log('isFunctionDeclaration', node.name.escapedText, { typeParameter0: node.typeParameters[0] })
|
|
|
|
if (node.name.escapedText === 'infer') {
|
|
const param0 = node.typeParameters[0]
|
|
if (ts.isPropertyDeclaration(param0)) {
|
|
const symbol = typeChecker.getSymbolAtLocation(param0)
|
|
|
|
// TODO: get resolving
|
|
|
|
// console.log('getResolvedSignature', typeChecker.getResolvedSignature(node) )
|
|
|
|
// const symbolType = typeChecker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration)
|
|
// const symbolSignature = typeChecker.getSignaturesOfType(symbolType, ts.SignatureKind.Call)
|
|
// console.log('symbol', symbol.getName(), symbolSignature)
|
|
|
|
// console.log('getSignaturesOfType', typeChecker.getSignaturesOfType(param0)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|