Skip to content

Commit

Permalink
Fix warnings due to removed object types
Browse files Browse the repository at this point in the history
Resolves #6
  • Loading branch information
Gerrit0 committed Aug 18, 2024
1 parent 602d396 commit 175e22f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 18 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach",
"port": 9229,
"request": "attach",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**"],
"type": "node",
"sourceMaps": true
}
]
}
55 changes: 38 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@types/node": "^20.14.8",
"outdent": "^0.8.0",
"prettier": "^3.3.2",
"typedoc": "^0.26.0",
"typedoc": "^0.26.6",
"typescript": "^5.5.2",
"vitest": "^1.6.0",
"zod": "^3.23.8"
Expand Down
20 changes: 20 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DeclarationReflection,
ReferenceType,
ReflectionKind,
makeRecursiveVisitor,
} from "typedoc";

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

app.converter.on(Converter.EVENT_CREATE_DECLARATION, onCreateDeclaration);
app.converter.on(Converter.EVENT_END, (context: Context) => {
const typeCleanup = makeRecursiveVisitor({
reflection: (type) => {
context.project.removeReflection(type.declaration);
},
});

for (const [inferredType, refOrig] of schemaTypes) {
if (
refOrig.reflection instanceof DeclarationReflection &&
refOrig.reflection.type instanceof ReferenceType
) {
refOrig.reflection.type.typeArguments?.forEach((t) =>
t.visit(typeCleanup),
);
refOrig.reflection.type.typeArguments = [
ReferenceType.createResolvedReference(
inferredType.name,
Expand All @@ -29,6 +39,9 @@ export function load(app: Application) {
}
}

console.log(context.project.getReflectionById(35)?.getFullName());
console.log(context.project.getReflectionById(70)?.getFullName());

schemaTypes.clear();
});

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

const type = context.getTypeAtLocation(declaration);
refl.type.visit(
makeRecursiveVisitor({
reflection: (type) => {
context.project.removeReflection(type.declaration);
},
}),
);
refl.type = context.converter.convertType(context, type);

if (originalRef) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/plugin.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,21 @@ test("Schemas which have multiple declarations, #2", () => {
Variable Foo: ZodObject<Foo>
`);
});

test("Serialized/deserialized projects do not create warnings, #6", () => {
const project = convert("gh6.ts");
const ser = app.serializer.projectToObject(project, process.cwd());
app.deserializer.reviveProject(ser, "gh6", process.cwd(), project.files);

expect(project.toStringHierarchy()).toBe(outdent`
Project typedoc-plugin-zod
TypeAlias Foo: Object
TypeLiteral __type
Property a: string
Property b: number
Property c: unknown
Variable Foo: ZodObject<Foo>
`);

expect(app.logger.hasWarnings()).toBe(false);
});
9 changes: 9 additions & 0 deletions src/testdata/gh6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { number, object, string, TypeOf, unknown } from "zod";

export const Foo = object({
a: string(),
b: number(),
c: unknown(),
});

export type Foo = TypeOf<typeof Foo>;

0 comments on commit 175e22f

Please sign in to comment.