Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply shield() to all exports #141

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 4 additions & 38 deletions telefunc/node/server/shield/codegen/generateShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { logResult }
// For ./generateShield.spec.ts
export { testGenerateShield }

import { Project, VariableDeclarationKind, SourceFile, getCompilerOptionsFromTsConfig, SyntaxKind } from 'ts-morph'
import { Project, VariableDeclarationKind, SourceFile, getCompilerOptionsFromTsConfig } from 'ts-morph'
import {
assert,
assertUsage,
Expand Down Expand Up @@ -123,16 +123,14 @@ function generate({
telefuncFilePath: string
exportList: ExportList
}): string {
const exportedFunctions = getExportedFunctions(telefuncFileSource, exportList)

shieldGenSource.addImportDeclaration({
moduleSpecifier: getTelefuncFileImportPath(telefuncFilePath),
namedImports: exportedFunctions.map((e) => e.exportName),
namedImports: exportList.map((e) => e.exportName),
})

// assign the template literal type to a string
// then diagnostics are used to get the value of the template literal type
for (const exportedFunction of exportedFunctions) {
for (const exportedFunction of exportList) {
shieldGenSource.addTypeAlias({
name: getShieldName(exportedFunction.exportName),
type: `ShieldArrStr<Parameters<typeof ${exportedFunction.exportName}>>`,
Expand Down Expand Up @@ -165,7 +163,7 @@ function generate({
// We need `compilerOptions.strict` to avoid `TS2589: Type instantiation is excessively deep and possibly infinite.`
assert(project.compilerOptions.get().strict === true)

for (const exportedFunction of exportedFunctions) {
for (const exportedFunction of exportList) {
const typeAliasName = getShieldName(exportedFunction.exportName)
const typeAlias = shieldGenSource.getTypeAlias(typeAliasName)
assert(typeAlias, `Failed to get type alias \`${typeAliasName}\`.`)
Expand Down Expand Up @@ -442,35 +440,3 @@ function assertTelefuncFilesSource(
assert(false, debugInfo)
}
}

function getExportedFunctions(telefuncFileSource: SourceFile, exportList: ExportList) {
const exportNames: string[] = Array.from(telefuncFileSource.getExportedDeclarations())
.filter(([_, declarations]) =>
declarations.some(
(decl) =>
// Regular function
decl.isKind(SyntaxKind.FunctionDeclaration) ||
// Arrow function
(decl.isKind(SyntaxKind.VariableDeclaration) && decl.getInitializer()?.isKind(SyntaxKind.ArrowFunction)),
),
)
.map(([exportName]) => exportName)

// Double check for regular functions (the following doesn't catch arrow functions)
telefuncFileSource
.getFunctions()
.filter((f) => f.isExported())
.flatMap((telefunction) => {
const name = telefunction.getName()
if (!name) return
assert(exportNames.includes(name))
})

const exportedFunctions = exportNames.map((exportName) => {
const e = exportList.find((e) => e.exportName === exportName)
assert(e)
return e
})

return exportedFunctions
}
Loading