Skip to content

Commit 403ff12

Browse files
code
1 parent e804590 commit 403ff12

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/SchemaGenerator.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,53 @@ export class SchemaGenerator {
187187
}
188188
return;
189189
}
190+
case ts.SyntaxKind.ExportDeclaration: {
191+
if (!ts.isExportDeclaration(node)) {
192+
return;
193+
}
194+
195+
// export { variable } clauses
196+
if (!node.moduleSpecifier) {
197+
return;
198+
}
199+
200+
const symbol = typeChecker.getSymbolAtLocation(node.moduleSpecifier);
201+
202+
// should never hit this (maybe type error in user's code)
203+
if (!symbol || !symbol.declarations) {
204+
return;
205+
}
206+
207+
// module augmentation can result in more than one source file
208+
for (const source of symbol.declarations) {
209+
const sourceSymbol = typeChecker.getSymbolAtLocation(source);
210+
211+
if (!sourceSymbol) {
212+
throw new Error(
213+
`Could not find symbol for SourceFile at ${(source as ts.SourceFile).fileName}`,
214+
);
215+
}
216+
217+
const moduleExports = typeChecker.getExportsOfModule(sourceSymbol);
218+
219+
for (const moduleExport of moduleExports) {
220+
const nodes =
221+
moduleExport.declarations ||
222+
(!!moduleExport.valueDeclaration && [moduleExport.valueDeclaration]);
223+
224+
// should never hit this (maybe type error in user's code)
225+
if (!nodes) {
226+
return;
227+
}
228+
229+
for (const node of nodes) {
230+
this.inspectNode(node, typeChecker, allTypes);
231+
}
232+
}
233+
}
234+
235+
return;
236+
}
190237
default:
191238
ts.forEachChild(node, (subnode) => this.inspectNode(subnode, typeChecker, allTypes));
192239
return;

test/sourceless-nodes/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("sourceless-nodes", () => {
4141
});
4242
});
4343

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

0 commit comments

Comments
 (0)