import { readFileSync } from "fs"; import * as ts from "typescript"; import { inspect } from 'util' export function infer3(sourceFile: ts.SourceFile) { recurse(sourceFile); function getType(kind: number) { switch(kind) { case ts.SyntaxKind.NumberKeyword: return 'number' case ts.SyntaxKind.StringKeyword: return 'string' case ts.SyntaxKind.BooleanKeyword: return 'boolean' case ts.SyntaxKind.JSDoc: return 'jsdoc' default: return String(ts.SyntaxKind[kind]) // TODO: work out all types } } function recurse(node: ts.Node) { if (node.kind === ts.SyntaxKind.Identifier) { console.log('Identifier', node['escapedText'], ts.SyntaxKind[node.kind]) } if (node['jsDoc']) { console.log('Found a JSDoc comment:') // console.log(inspect(node['jsDoc'], { depth: null, colors: true })) const fullComment = sourceFile.text.slice(node.pos, node.end) console.log(fullComment) // TODO: next steps: // - either get the types from the TypeScript AST, // or extract them ourselves with regex or anything from the comment text // - After that, we have to transform the source file and insert the comments // as string or object that is runtime accessible in JavaScript } ts.forEachChild(node, recurse); } } const fileNames = process.argv.slice(2); console.log('infer files', fileNames) fileNames.forEach(fileName => { // Parse a file const sourceFile = ts.createSourceFile( fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES2022, /*setParentNodes */ true ); console.log('FILE') console.log(fileName) console.log() console.log('SOURCE') console.log(sourceFile.text) console.log() console.log('AST') console.log(inspect(sourceFile, { depth: null, colors: true })) console.log() console.log('INFER') infer3(sourceFile); });