Skip to content

Commit

Permalink
code
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed May 22, 2024
1 parent e804590 commit 403ff12
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,53 @@ export class SchemaGenerator {
}
return;
}
case ts.SyntaxKind.ExportDeclaration: {
if (!ts.isExportDeclaration(node)) {
return;
}

// export { variable } clauses
if (!node.moduleSpecifier) {
return;
}

const symbol = typeChecker.getSymbolAtLocation(node.moduleSpecifier);

// should never hit this (maybe type error in user's code)
if (!symbol || !symbol.declarations) {
return;
}

// module augmentation can result in more than one source file
for (const source of symbol.declarations) {
const sourceSymbol = typeChecker.getSymbolAtLocation(source);

if (!sourceSymbol) {
throw new Error(
`Could not find symbol for SourceFile at ${(source as ts.SourceFile).fileName}`,
);
}

const moduleExports = typeChecker.getExportsOfModule(sourceSymbol);

for (const moduleExport of moduleExports) {
const nodes =
moduleExport.declarations ||
(!!moduleExport.valueDeclaration && [moduleExport.valueDeclaration]);

// should never hit this (maybe type error in user's code)
if (!nodes) {
return;
}

for (const node of nodes) {
this.inspectNode(node, typeChecker, allTypes);
}
}
}

return;
}
default:
ts.forEachChild(node, (subnode) => this.inspectNode(subnode, typeChecker, allTypes));
return;
Expand Down
2 changes: 1 addition & 1 deletion test/sourceless-nodes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("sourceless-nodes", () => {
});
});

// From github.com/arthurfiorette/kita/blob/main/packages/generator/src/util/type-resolver.ts
// From https://github.com/kitajs/kitajs/blob/ebf23297de07887c78becff52120f941e69386ec/packages/parser/src/util/nodes.ts#L64
function getReturnType(node: ts.SignatureDeclaration, typeChecker: ts.TypeChecker) {
const signature = typeChecker.getSignatureFromDeclaration(node);
const implicitType = typeChecker.getReturnTypeOfSignature(signature!);
Expand Down

0 comments on commit 403ff12

Please sign in to comment.