Skip to content
Merged
18 changes: 14 additions & 4 deletions src/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class SchemaGenerator {
rootType: this.nodeParser.createType(rootNode, new Context()),
}));

const rootTypeDefinition =
roots.length === 1 ? this.getRootTypeDefinition(roots[0].rootType, roots[0].rootNode) : undefined;
const rootTypeDefinitions = roots.map((root) => this.getRootTypeDefinition(root.rootType, root.rootNode));
const rootTypeDefinition = rootTypeDefinitions.length === 1 ? rootTypeDefinitions[0] : undefined;
const definitions: StringMap<Definition> = {};

for (const root of roots) {
Expand All @@ -47,7 +47,10 @@ export class SchemaGenerator {
}
}

const reachableDefinitions = removeUnreachable(rootTypeDefinition, definitions);
const reachableDefinitions = rootTypeDefinitions.reduce<StringMap<Definition>>(
(acc, def) => Object.assign(acc, removeUnreachable(def, definitions)),
{},
);

return {
...(this.config?.schemaId ? { $id: this.config.schemaId } : {}),
Expand Down Expand Up @@ -229,11 +232,18 @@ export class SchemaGenerator {
return;
}

// export { variable } clauses
if (node.exportClause) {
// export { Foo } from './lib' or export { Foo };
// export * as Foo from './lib' should not import all exports
ts.forEachChild(node.exportClause, (subnode) => this.inspectNode(subnode, typeChecker, allTypes));
return;
}

if (!node.moduleSpecifier) {
return;
}

// export * from './lib'
const symbol = typeChecker.getSymbolAtLocation(node.moduleSpecifier);

// should never hit this (maybe type error in user's code)
Expand Down
4 changes: 4 additions & 0 deletions test/valid-data-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,8 @@ describe("valid-data-type", () => {
it("promise-extensions", assertValidSchema("promise-extensions", "*"));

it("export-star", assertValidSchema("export-star", "*", undefined, { mainTsOnly: true }));
it(
"export-star-prune-unreachable",
assertValidSchema("export-star-prune-unreachable", "*", undefined, { mainTsOnly: true }),
);
});
5 changes: 5 additions & 0 deletions test/valid-data/export-star-prune-unreachable/dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface SomeInterface {
foo?: string;
}

export type DepType = string;
1 change: 1 addition & 0 deletions test/valid-data/export-star-prune-unreachable/dep2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type DepType2 = string;
14 changes: 14 additions & 0 deletions test/valid-data/export-star-prune-unreachable/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { SomeInterface } from "./dep";
export { DepType } from "./dep";
export * from "./dep2";

export type MyType = string;

export interface MyObject extends SomeInterface {
Comment thread
arthurfiorette marked this conversation as resolved.
bar?: number;
baz?: Internal;
}

interface Internal {
Comment thread
arthurfiorette marked this conversation as resolved.
nested?: boolean;
}
35 changes: 35 additions & 0 deletions test/valid-data/export-star-prune-unreachable/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"DepType": {
"type": "string"
},
"DepType2": {
"type": "string"
},
"MyObject": {
"additionalProperties": false,
"properties": {
"bar": {
"type": "number"
},
"baz": {
"additionalProperties": false,
"properties": {
"nested": {
"type": "boolean"
}
},
"type": "object"
},
"foo": {
"type": "string"
}
},
"type": "object"
},
"MyType": {
"type": "string"
}
}
}
Loading