Compare commits

...

2 commits

2 changed files with 5 additions and 26 deletions

View file

@ -20,39 +20,17 @@ function reflectType5(srcFile, options = { debug: false }) {
log(`=========Reflecting file "${relative(__dirname, srcFile)}"`)
const defFile = srcFile.replace(/.js$/, '.d.ts')
const src = String(readFileSync(srcFile))
let src = String(readFileSync(srcFile))
const defs = String(readFileSync(defFile))
const parsedDefs = ts2json(defFile)
const typeDefMatches = defs.matchAll(/: ({(?:(?!\n}).)+\n}) & (?:(?!ReflectedTypeInfo).)+ReflectedTypeInfo/gs)
if (!typeDefMatches) {
log('No ReflectedTypeInfo found.')
return
}
const typeDefs = Array.from(typeDefMatches).map(def => def[1])
log(` ${typeDefs.length} ReflectedTypeInfo found`)
let index = 0
let srcReflected = src.replaceAll(/(\s*)\.ship\(\)/g, () => {
const def = typeDefs[index]
index++
return `.ship({ reflectedType5: \`${def}\` })`
})
log(` ReflectedTypeInfo injected in ${index} occurrences of .ship()`)
if (index !== typeDefs.length) {
log(' WARNING: not all ReflectedTypeInfo occurrences could be injected')
}
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}._reflectedType5 = ${JSON.stringify(parsedDefs[id])}\n`
src += `\n${id}._reflectedType5 = ${JSON.stringify(parsedDefs[id])}\n`
}
writeFileSync(srcFile, srcReflected)
writeFileSync(srcFile, src)
function log(...args) {
if (options.debug) {

View file

@ -225,7 +225,8 @@ export default function(filename, options = {}) {
let program = ts.createProgram([filename], options)
let checker = program.getTypeChecker()
const sourceFiles = program.getSourceFiles()
let sourceFile = sourceFiles.find(file => file.fileName === filename)
const filenameUnix = filename.replaceAll('\\', '/')
let sourceFile = sourceFiles.find(file => file.fileName === filenameUnix)
ts.forEachChild(sourceFile, visit(node, checker))