Skip to content

Commit 175e22f

Browse files
committed
Fix warnings due to removed object types
Resolves #6
1 parent 602d396 commit 175e22f

File tree

6 files changed

+103
-18
lines changed

6 files changed

+103
-18
lines changed

.vscode/launch.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible Node.js debug attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Attach",
9+
"port": 9229,
10+
"request": "attach",
11+
"internalConsoleOptions": "openOnSessionStart",
12+
"skipFiles": ["<node_internals>/**"],
13+
"type": "node",
14+
"sourceMaps": true
15+
}
16+
]
17+
}

package-lock.json

+38-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@types/node": "^20.14.8",
1919
"outdent": "^0.8.0",
2020
"prettier": "^3.3.2",
21-
"typedoc": "^0.26.0",
21+
"typedoc": "^0.26.6",
2222
"typescript": "^5.5.2",
2323
"vitest": "^1.6.0",
2424
"zod": "^3.23.8"

src/plugin.ts

+20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
DeclarationReflection,
77
ReferenceType,
88
ReflectionKind,
9+
makeRecursiveVisitor,
910
} from "typedoc";
1011

1112
export function load(app: Application) {
@@ -14,11 +15,20 @@ export function load(app: Application) {
1415

1516
app.converter.on(Converter.EVENT_CREATE_DECLARATION, onCreateDeclaration);
1617
app.converter.on(Converter.EVENT_END, (context: Context) => {
18+
const typeCleanup = makeRecursiveVisitor({
19+
reflection: (type) => {
20+
context.project.removeReflection(type.declaration);
21+
},
22+
});
23+
1724
for (const [inferredType, refOrig] of schemaTypes) {
1825
if (
1926
refOrig.reflection instanceof DeclarationReflection &&
2027
refOrig.reflection.type instanceof ReferenceType
2128
) {
29+
refOrig.reflection.type.typeArguments?.forEach((t) =>
30+
t.visit(typeCleanup),
31+
);
2232
refOrig.reflection.type.typeArguments = [
2333
ReferenceType.createResolvedReference(
2434
inferredType.name,
@@ -29,6 +39,9 @@ export function load(app: Application) {
2939
}
3040
}
3141

42+
console.log(context.project.getReflectionById(35)?.getFullName());
43+
console.log(context.project.getReflectionById(70)?.getFullName());
44+
3245
schemaTypes.clear();
3346
});
3447

@@ -57,6 +70,13 @@ export function load(app: Application) {
5770
if (!declaration) return;
5871

5972
const type = context.getTypeAtLocation(declaration);
73+
refl.type.visit(
74+
makeRecursiveVisitor({
75+
reflection: (type) => {
76+
context.project.removeReflection(type.declaration);
77+
},
78+
}),
79+
);
6080
refl.type = context.converter.convertType(context, type);
6181

6282
if (originalRef) {

src/test/plugin.test.mts

+18
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,21 @@ test("Schemas which have multiple declarations, #2", () => {
113113
Variable Foo: ZodObject<Foo>
114114
`);
115115
});
116+
117+
test("Serialized/deserialized projects do not create warnings, #6", () => {
118+
const project = convert("gh6.ts");
119+
const ser = app.serializer.projectToObject(project, process.cwd());
120+
app.deserializer.reviveProject(ser, "gh6", process.cwd(), project.files);
121+
122+
expect(project.toStringHierarchy()).toBe(outdent`
123+
Project typedoc-plugin-zod
124+
TypeAlias Foo: Object
125+
TypeLiteral __type
126+
Property a: string
127+
Property b: number
128+
Property c: unknown
129+
Variable Foo: ZodObject<Foo>
130+
`);
131+
132+
expect(app.logger.hasWarnings()).toBe(false);
133+
});

src/testdata/gh6.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { number, object, string, TypeOf, unknown } from "zod";
2+
3+
export const Foo = object({
4+
a: string(),
5+
b: number(),
6+
c: unknown(),
7+
});
8+
9+
export type Foo = TypeOf<typeof Foo>;

0 commit comments

Comments
 (0)