Reflect types into properties on top-level symbols as well. #4

Merged
glen merged 4 commits from feat/parse-reflection into feat/reflect-types 2024-10-21 16:58:00 +00:00
2 changed files with 18 additions and 26 deletions
Showing only changes of commit 25be6a2898 - Show all commits

View File

@ -23,7 +23,6 @@ function reflectType5(srcFile, options = { debug: false }) {
const src = String(readFileSync(srcFile))
const defs = String(readFileSync(defFile))
const parsedDefs = ts2json(defFile)
console.log('Defs from', defFile, 'are', parsedDefs)
const typeDefMatches = defs.matchAll(/: ({(?:(?!\n}).)+\n}) & (?:(?!ReflectedTypeInfo).)+ReflectedTypeInfo/gs)
if (!typeDefMatches) {
@ -48,8 +47,10 @@ function reflectType5(srcFile, options = { debug: false }) {
for (const id in parsedDefs) {
if (id.includes('interface')) continue
if (parsedDefs[id] === undefined) continue
log(` Tagging ${id} with type data`, parsedDefs[id])
srcReflected +=
`\n${id}._reflectedType6 = ${JSON.stringify(parsedDefs[id])}\n`
`\n${id}._reflectedType5 = ${JSON.stringify(parsedDefs[id])}\n`
}
writeFileSync(srcFile, srcReflected)

View File

@ -101,7 +101,6 @@ function typeStructure(typeNode, checker) {
_index: typeStructure(typeNode.indexType, checker)
}
case ts.SyntaxKind.TypeOperator:
console.log('structuring operator', typeNode)
const key = typeOperatorKeywords.get(
typeNode.operator,
'_unidentified_operator'
@ -139,27 +138,31 @@ function typeStructure(typeNode, checker) {
const visit = (parent, checker) => node => {
switch (node.kind) {
// Currently, we are ignoring the following sorts of statements
// that may appear in .d.ts files. We may need to revisit these,
// especially the InterfaceDeclaration and TypeAliasDeclaration,
// if we want to generate runtime information on pure type
// declarations. I think this may be necessary for example to compute
// the "RealType" of a type at runtime.
case ts.SyntaxKind.EndOfFileToken:
case ts.SyntaxKind.ExportDeclaration:
case ts.SyntaxKind.ImportDeclaration:
console.log('Skipping import from', node.moduleSpecifier.text)
case ts.SyntaxKind.InterfaceDeclaration:
case ts.SyntaxKind.TypeAliasDeclaration:
break
case ts.SyntaxKind.VariableStatement:
console.log('Processing variable',
ts.SyntaxKind[node.declarationList.kind])
node.declarationList.declarations.forEach(visit(parent, checker))
console.log('Done with variable')
break
case ts.SyntaxKind.VariableDeclaration: {
console.log('Processing var dec', node.name.text)
const typeStruc = typeStructure(node.type, checker)
parent.addChild(node.name.text, typeStruc)
break
}
case ts.SyntaxKind.FunctionDeclaration: {
console.log('Processing func dec', node.name.text)
const typeStruc = {
_parameters: node.parameters.map(
p => parameterTypeStructure(p, checker)),
_returns: typeStructure(node.type, checker)
const typeStruc = {
_parameters: node.parameters.map(
p => parameterTypeStructure(p, checker)),
_returns: typeStructure(node.type, checker)
}
if (node.typeParameters) {
typeStruc._typeParameters = node.typeParameters.map(
@ -170,20 +173,11 @@ const visit = (parent, checker) => node => {
}
case ts.SyntaxKind.ModuleDeclaration:
let moduleName = node.name.text
console.log('Declaring', moduleName)
ts.forEachChild(node, visit(parent.addChild(moduleName), checker))
visit(parent.addChild(moduleName), checker)(node.body)
break
case ts.SyntaxKind.ModuleBlock:
console.log('Block')
ts.forEachChild(node, visit(parent, checker));
break
case ts.SyntaxKind.InterfaceDeclaration:
let interfaceName = node.name.text
console.log('Skipping Interface', interfaceName);
break
parent[interfaceName] = {}
ts.forEachChild(node, visit(parent.addChild(interfaceName), checker))
break
case ts.SyntaxKind.PropertySignature:
let propertyName = node.name
let propertyType = node.type
@ -217,9 +211,6 @@ const visit = (parent, checker) => node => {
}
}
break
case ts.SyntaxKind.EndOfFileToken:
console.log('File ended.')
break
default:
console.warn(
'Unhandled node kind',