Skip to content

Commit c3b9504

Browse files
fix: prune unreachable definitions when --type "*" is used with multiple exports (#2284)
Co-authored-by: Arthur Fiorette <47537704+arthurfiorette@users.noreply.github.com>
1 parent 93428f4 commit c3b9504

File tree

6 files changed

+73
-4
lines changed

6 files changed

+73
-4
lines changed

src/SchemaGenerator.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class SchemaGenerator {
3131
rootType: this.nodeParser.createType(rootNode, new Context()),
3232
}));
3333

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

3838
for (const root of roots) {
@@ -47,7 +47,10 @@ export class SchemaGenerator {
4747
}
4848
}
4949

50-
const reachableDefinitions = removeUnreachable(rootTypeDefinition, definitions);
50+
const reachableDefinitions = rootTypeDefinitions.reduce<StringMap<Definition>>(
51+
(acc, def) => Object.assign(acc, removeUnreachable(def, definitions)),
52+
{},
53+
);
5154

5255
return {
5356
...(this.config?.schemaId ? { $id: this.config.schemaId } : {}),
@@ -229,11 +232,18 @@ export class SchemaGenerator {
229232
return;
230233
}
231234

232-
// export { variable } clauses
235+
if (node.exportClause) {
236+
// export { Foo } from './lib' or export { Foo };
237+
// export * as Foo from './lib' should not import all exports
238+
ts.forEachChild(node.exportClause, (subnode) => this.inspectNode(subnode, typeChecker, allTypes));
239+
return;
240+
}
241+
233242
if (!node.moduleSpecifier) {
234243
return;
235244
}
236245

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

239249
// should never hit this (maybe type error in user's code)

test/valid-data-type.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,8 @@ describe("valid-data-type", () => {
151151
it("promise-extensions", assertValidSchema("promise-extensions", "*"));
152152

153153
it("export-star", assertValidSchema("export-star", "*", undefined, { mainTsOnly: true }));
154+
it(
155+
"export-star-prune-unreachable",
156+
assertValidSchema("export-star-prune-unreachable", "*", undefined, { mainTsOnly: true }),
157+
);
154158
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface SomeInterface {
2+
foo?: string;
3+
}
4+
5+
export type DepType = string;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type DepType2 = string;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { SomeInterface } from "./dep";
2+
export { DepType } from "./dep";
3+
export * from "./dep2";
4+
5+
export type MyType = string;
6+
7+
export interface MyObject extends SomeInterface {
8+
bar?: number;
9+
baz?: Internal;
10+
}
11+
12+
interface Internal {
13+
nested?: boolean;
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"definitions": {
4+
"DepType": {
5+
"type": "string"
6+
},
7+
"DepType2": {
8+
"type": "string"
9+
},
10+
"MyObject": {
11+
"additionalProperties": false,
12+
"properties": {
13+
"bar": {
14+
"type": "number"
15+
},
16+
"baz": {
17+
"additionalProperties": false,
18+
"properties": {
19+
"nested": {
20+
"type": "boolean"
21+
}
22+
},
23+
"type": "object"
24+
},
25+
"foo": {
26+
"type": "string"
27+
}
28+
},
29+
"type": "object"
30+
},
31+
"MyType": {
32+
"type": "string"
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)