Skip to content

Commit

Permalink
fix: Generate schema for unspecified re-exported types (#1893)
Browse files Browse the repository at this point in the history
* fix: Generate schema for unspecified re-exported types

* Clarify comments
  • Loading branch information
Twixes committed Mar 26, 2024
1 parent 031f3ea commit 5928a3d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/SchemaGenerator.ts
Expand Up @@ -167,6 +167,25 @@ export class SchemaGenerator {
case ts.SyntaxKind.ArrowFunction:
allTypes.set(`NamedParameters<typeof ${this.getFullName(node, typeChecker)}>`, node);
return;
case ts.SyntaxKind.ExportSpecifier: {
const exportSpecifierNode = node as ts.ExportSpecifier;
const symbol = typeChecker.getExportSpecifierLocalTargetSymbol(exportSpecifierNode);
if (symbol?.declarations?.length === 1) {
const declaration = symbol.declarations[0];
if (declaration.kind === ts.SyntaxKind.ImportSpecifier) {
// Handling the `Foo` in `import { Foo } from "./lib"; export { Foo };`
const importSpecifierNode = declaration as ts.ImportSpecifier;
const type = typeChecker.getTypeAtLocation(importSpecifierNode);
if (type.symbol?.declarations?.length === 1) {
this.inspectNode(type.symbol.declarations[0], typeChecker, allTypes);
}
} else {
// Handling the `Bar` in `export { Bar } from './lib';`
this.inspectNode(declaration, typeChecker, allTypes);
}
}
return;
}
default:
ts.forEachChild(node, (subnode) => this.inspectNode(subnode, typeChecker, allTypes));
return;
Expand Down
3 changes: 2 additions & 1 deletion test/utils.ts
Expand Up @@ -47,12 +47,13 @@ export function assertValidSchema(
* @default {strict:false}
*/
ajvOptions?: AjvOptions;
mainTsOnly?: boolean;
}
) {
return (): void => {
const config: Config = {
...DEFAULT_CONFIG,
path: `${basePath}/${relativePath}/*.ts`,
path: `${basePath}/${relativePath}/${options?.mainTsOnly ? "main" : "*"}.ts`,
skipTypeCheck: !!process.env.FAST_TEST,
type,
...config_,
Expand Down
1 change: 1 addition & 0 deletions test/valid-data-other.test.ts
Expand Up @@ -102,4 +102,5 @@ describe("valid-data-other", () => {
ajvOptions: { $data: true },
})
);
it("re-export-with-asterisk", assertValidSchema("re-export-with-asterisk", "*", undefined, { mainTsOnly: true }));
});
8 changes: 8 additions & 0 deletions test/valid-data/re-export-with-asterisk/lib.ts
@@ -0,0 +1,8 @@
export enum MyEnum {
Foo = "foo",
Bar = "bar",
}

export interface MyObject {
id: string;
}
3 changes: 3 additions & 0 deletions test/valid-data/re-export-with-asterisk/main.ts
@@ -0,0 +1,3 @@
export { MyObject } from "./lib";
import { MyEnum } from "./lib";
export { MyEnum };
24 changes: 24 additions & 0 deletions test/valid-data/re-export-with-asterisk/schema.json
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyEnum": {
"enum": [
"foo",
"bar"
],
"type": "string"
},
"MyObject": {
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
}
},
"required": [
"id"
],
"type": "object"
}
}
}

0 comments on commit 5928a3d

Please sign in to comment.