Skip to content

Commit

Permalink
fix: correctly handle certain type alias and generic cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DJankauskas committed Jun 29, 2023
1 parent 2cc1839 commit f01f00c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
31 changes: 19 additions & 12 deletions packages/stl-api-gen/src/index.ts
Expand Up @@ -100,23 +100,30 @@ async function main() {
hasMagicCall = true;

const [typeArgument] = typeRefArguments;
const type = typeArgument.getType();

let hasTypeArguments = false;

if (typeArgument instanceof tm.TypeReferenceNode) {
const name = typeArgument.getTypeName();
if (typeArgument.getTypeArguments().length) hasTypeArguments = true;
const symbol = name.getSymbolOrThrow();
} else {

}


let schemaExpression: ts.Expression;

const typeSymbol = type.getAliasSymbol() || type.getSymbol();
const type = typeArgument.getType();
let schemaExpression: ts.Expression;

ctx.isRoot = true;
ctx.diagnostics = new Map();
if (
typeSymbol &&
(type.getAliasSymbol() ||
type.isInterface() ||
type.isEnum() ||
type.isClass())
typeArgument instanceof tm.TypeReferenceNode && typeArgument.getTypeArguments().length === 0
) {
const symbol = typeArgument.getTypeName().getSymbolOrThrow();
try {
convertSymbol(ctx, typeSymbol, {
convertSymbol(ctx, symbol, {
variant: "node",
node: typeArgument,
});
Expand All @@ -126,9 +133,9 @@ async function main() {
} finally {
addDiagnostics(ctx, file, callExpression, callDiagnostics);
}
const name = typeSymbol.getName();
const name = symbol.getName();
let as;
const declaration = typeSymbol.getDeclarations()[0];
const declaration = symbol.getDeclarations()[0];
// TODO factor out this logic in ts-to-zod and export a function
if (type.isEnum()) {
as = `__enum_${name}`;
Expand All @@ -141,7 +148,7 @@ async function main() {
const declarationFilePath = declaration.getSourceFile().getFilePath();

if (declarationFilePath === file.getFilePath()) {
imports.set(typeSymbol.getName(), {
imports.set(symbol.getName(), {
as,
sourceFile: declarationFilePath,
});
Expand Down
7 changes: 7 additions & 0 deletions packages/ts-to-zod/src/convertType.ts
Expand Up @@ -1304,6 +1304,13 @@ function getDeclaration(symbol: tm.Symbol): tm.Node | undefined {
) {
return declaration;
}

if (declaration instanceof tm.ImportSpecifier) {
const symbol = declaration.getSymbol();
if (symbol && !symbol.getValueDeclaration()) {
return declaration;
}
}
}

return undefined;
Expand Down

0 comments on commit f01f00c

Please sign in to comment.