Skip to content

Commit 04a287a

Browse files
authored
fix: apply shield() to all exports (#141)
1 parent 6910727 commit 04a287a

File tree

1 file changed

+4
-38
lines changed

1 file changed

+4
-38
lines changed

telefunc/node/server/shield/codegen/generateShield.ts

+4-38
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export { logResult }
44
// For ./generateShield.spec.ts
55
export { testGenerateShield }
66

7-
import { Project, VariableDeclarationKind, SourceFile, getCompilerOptionsFromTsConfig, SyntaxKind } from 'ts-morph'
7+
import { Project, VariableDeclarationKind, SourceFile, getCompilerOptionsFromTsConfig } from 'ts-morph'
88
import {
99
assert,
1010
assertUsage,
@@ -123,16 +123,14 @@ function generate({
123123
telefuncFilePath: string
124124
exportList: ExportList
125125
}): string {
126-
const exportedFunctions = getExportedFunctions(telefuncFileSource, exportList)
127-
128126
shieldGenSource.addImportDeclaration({
129127
moduleSpecifier: getTelefuncFileImportPath(telefuncFilePath),
130-
namedImports: exportedFunctions.map((e) => e.exportName),
128+
namedImports: exportList.map((e) => e.exportName),
131129
})
132130

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

168-
for (const exportedFunction of exportedFunctions) {
166+
for (const exportedFunction of exportList) {
169167
const typeAliasName = getShieldName(exportedFunction.exportName)
170168
const typeAlias = shieldGenSource.getTypeAlias(typeAliasName)
171169
assert(typeAlias, `Failed to get type alias \`${typeAliasName}\`.`)
@@ -442,35 +440,3 @@ function assertTelefuncFilesSource(
442440
assert(false, debugInfo)
443441
}
444442
}
445-
446-
function getExportedFunctions(telefuncFileSource: SourceFile, exportList: ExportList) {
447-
const exportNames: string[] = Array.from(telefuncFileSource.getExportedDeclarations())
448-
.filter(([_, declarations]) =>
449-
declarations.some(
450-
(decl) =>
451-
// Regular function
452-
decl.isKind(SyntaxKind.FunctionDeclaration) ||
453-
// Arrow function
454-
(decl.isKind(SyntaxKind.VariableDeclaration) && decl.getInitializer()?.isKind(SyntaxKind.ArrowFunction)),
455-
),
456-
)
457-
.map(([exportName]) => exportName)
458-
459-
// Double check for regular functions (the following doesn't catch arrow functions)
460-
telefuncFileSource
461-
.getFunctions()
462-
.filter((f) => f.isExported())
463-
.flatMap((telefunction) => {
464-
const name = telefunction.getName()
465-
if (!name) return
466-
assert(exportNames.includes(name))
467-
})
468-
469-
const exportedFunctions = exportNames.map((exportName) => {
470-
const e = exportList.find((e) => e.exportName === exportName)
471-
assert(e)
472-
return e
473-
})
474-
475-
return exportedFunctions
476-
}

0 commit comments

Comments
 (0)