chore: inject the reflected type information inside the .ship() occurrences

This commit is contained in:
Jos de Jong 2024-10-11 13:37:07 +02:00 committed by Glen Whitney
parent ad5fd99f53
commit b9675c6d59
2 changed files with 25 additions and 28 deletions

View File

@ -102,6 +102,10 @@ type Implementations<
Specs extends Specifications<Signatures, NeedKeys, NeedList>
> = {[K in NeedKeys]: ImpType<Signatures, K, Specs[K]>}
export interface ReflectedTypeInfo {
reflectedType5: string
}
// The builder interface that lets us assemble narrowly-typed Implementations:
interface ImplementationBuilder<
Signatures extends GenSigs,
@ -135,7 +139,7 @@ interface ImplementationBuilder<
Specs & {[K in NewKeys]: DepCheck<RD, Signatures>}
>
ship(): Implementations<Signatures, NeedKeys, NeedList, Specs> & { reflectedType5: string }
ship(info?: ReflectedTypeInfo): Implementations<Signatures, NeedKeys, NeedList, Specs> & ReflectedTypeInfo
}
// And a function that actually provides the builder interface:
@ -187,9 +191,8 @@ function impBuilder<
Specs & {[K in NewKeys]: DepCheck<RD, Signatures, DepKeys>}
>
},
ship() {
return (sofar as
Implementations<Signatures, NeedKeys, NeedList, Specs> & { reflectedType5: string })
ship(info?: ReflectedTypeInfo) {
return { ...sofar, ...info }
}
}
}

View File

@ -11,44 +11,38 @@ const files = (await readdirSync(buildDir, { recursive: true }))
.map(file => join(buildDir, file))
for (const file of files) {
reflectType(file, { debug: true })
reflectType5(file, { debug: true })
}
function reflectType(srcFile, options = { debug: false }) {
function reflectType5(srcFile, options = { debug: false }) {
log(`Reflecting file "${relative(__dirname, srcFile)}"`)
const defFile = srcFile.replace(/.js$/, '.d.ts')
const src = String(readFileSync(srcFile))
const defs = String(readFileSync(defFile))
if (src.includes('.reflectedType5')) {
log(' Source file already contains reflected types and will be ignored')
const typeDefMatches = defs.matchAll(/: ({(?:(?!\n}).)+\n}) & (?:(?!ReflectedTypeInfo).)+ReflectedTypeInfo/gs)
if (!typeDefMatches) {
log('No ReflectedTypeInfo found.')
return
}
const matches = Array.from(src.matchAll(/[^\n](export +)?const +([\w$]+) += +implementations\b/g))
const typeDefs = Array.from(typeDefMatches).map(def => def[1])
log(` ${typeDefs.length} ReflectedTypeInfo found`)
if (matches.length === 0) {
log(` No implementations found`)
return
let index = 0
const 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 match of matches) {
const name = match[2]
log(` Found an implementations definition: "${name}"`)
const def = new RegExp(`export declare const ${name}: {(?:(?!\n}).)+\n}`, 's').exec(defs)
if (def) {
log(` Injected type definitions for "${name}"`)
const srcReflected = `${src}\n${name}.reflectedType5 = \`${def}\`\n`
writeFileSync(srcFile, srcReflected)
} else {
log(` No type definitions found for "${name}"`)
}
}
writeFileSync(srcFile, srcReflected)
function log(...args) {
if (options.debug) {