Skip to content

Commit aa2b4a9

Browse files
committed
Conditionally serialize media->reflection entries
Ref: Gerrit0/typedoc-plugin-zod#6
1 parent 73e1dd6 commit aa2b4a9

File tree

7 files changed

+45
-7
lines changed

7 files changed

+45
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
- The `suppressCommentWarningsInDeclarationFiles` option now correctly ignores warnings in `.d.cts` and `.d.mts` files, #2647.
1212
- Restored re-exports in the page navigation menu, #2671.
13+
- JSON serialized projects will no longer contain reflection IDs for other projects created in the same run. Gerrit0/typedoc-plugin-zod#6.
14+
- In packages mode the reflection ID counter will no longer be reset when converting projects. This previously could result in links to files not working as expected.
1315

1416
## v0.26.5 (2024-07-21)
1517

src/lib/application.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import { validateLinks } from "./validation/links";
4040
import { ApplicationEvents } from "./application-events";
4141
import { findTsConfigFile } from "./utils/tsconfig";
4242
import { deriveRootDir, glob, readFile } from "./utils/fs";
43-
import { resetReflectionID } from "./models/reflections/abstract";
4443
import { addInferredDeclarationMapPaths } from "./models/reflections/ReflectionSymbolId";
4544
import {
4645
Internationalization,
@@ -706,13 +705,13 @@ export class Application extends ChildableComponent<
706705
}
707706

708707
// When debugging memory issues, it's useful to set these
709-
// here so that a breakpoint on resetReflectionID below
708+
// here so that a breakpoint on the continue statement below
710709
// gets the memory as it ought to be with all TS objects released.
711710
project = undefined;
712711
this.files = undefined!;
713712
// global.gc!();
714713

715-
resetReflectionID();
714+
continue;
716715
}
717716

718717
this.options = origOptions;

src/lib/models/FileRegistry.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ export class FileRegistry {
101101
result.entries[key] = normalizePath(relative(ser.projectRoot, val));
102102
}
103103
for (const [key, val] of this.mediaToReflection.entries()) {
104-
result.reflections[key] = val.id;
104+
// A registry may be shared by multiple projects. When serializing,
105+
// only save reflection mapping for reflections in the serialized project.
106+
if (ser.project.getReflectionById(val.id)) {
107+
result.reflections[key] = val.id;
108+
}
105109
}
106110

107111
return result;

src/lib/models/reflections/project.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ReferenceReflection } from "./reference";
44
import type { DeclarationReflection } from "./declaration";
55
import type { SignatureReflection } from "./signature";
66
import type { ParameterReflection } from "./parameter";
7-
import { IntrinsicType } from "../types";
7+
import { IntrinsicType, makeRecursiveVisitor, Type } from "../types";
88
import type { TypeParameterReflection } from "./type-parameter";
99
import { assertNever, removeIf, removeIfPresent } from "../../utils";
1010
import type * as ts from "typescript";
@@ -49,7 +49,7 @@ export class ProjectReflection extends ContainerReflection {
4949
*
5050
* This may be replaced with a `Map<number, Reflection>` someday.
5151
*/
52-
reflections: { [id: number]: Reflection } = {};
52+
reflections: { [id: number]: Reflection } = { [this.id]: this };
5353

5454
/**
5555
* The name of the package that this reflection documents according to package.json.
@@ -153,6 +153,22 @@ export class ProjectReflection extends ContainerReflection {
153153
}
154154
}
155155

156+
/**
157+
* Removes references to reflections contained within the provided type.
158+
* Plugins which overwrite types on reflections should pass the type to this
159+
* method before overwriting the property.
160+
* @since 0.26.6
161+
*/
162+
removeTypeReflections(type: Type | undefined) {
163+
type?.visit(
164+
makeRecursiveVisitor({
165+
reflection: (type) => {
166+
this.removeReflection(type.declaration);
167+
},
168+
}),
169+
);
170+
}
171+
156172
/**
157173
* Removes a reflection from the documentation. Can be used by plugins to filter reflections
158174
* out of the generated documentation. Has no effect if the reflection is not present in the

src/lib/serialization/deserializer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ export class Deserializer {
240240
name || projectObj.name,
241241
registry,
242242
);
243-
project.registerReflection(project, undefined, undefined);
244243
this.project = project;
245244
this.projectRoot = projectRoot;
246245
this.oldIdToNewId = { [projectObj.id]: project.id };

src/lib/serialization/serializer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export class Serializer extends EventDispatcher<SerializerEvents> {
3131
*/
3232
projectRoot!: string;
3333

34+
/**
35+
* Only set when serializing
36+
*/
37+
project!: ProjectReflection;
38+
3439
addSerializer<T extends object>(serializer: SerializerComponent<T>): void {
3540
insertPrioritySorted(this.serializers, serializer);
3641
}
@@ -75,6 +80,7 @@ export class Serializer extends EventDispatcher<SerializerEvents> {
7580
projectRoot: string,
7681
): ModelToObject<ProjectReflection> {
7782
this.projectRoot = projectRoot;
83+
this.project = value;
7884

7985
const eventBegin = new SerializeEvent(value);
8086
this.trigger(Serializer.EVENT_BEGIN, eventBegin);
@@ -84,6 +90,9 @@ export class Serializer extends EventDispatcher<SerializerEvents> {
8490
const eventEnd = new SerializeEvent(value, project);
8591
this.trigger(Serializer.EVENT_END, eventEnd);
8692

93+
this.project = undefined!;
94+
this.projectRoot = undefined!;
95+
8796
return project;
8897
}
8998
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ok } from "assert";
2+
import { FileRegistry, ProjectReflection } from "../../lib/models";
3+
4+
describe("ProjectReflection", () => {
5+
it("getReflectionById works with the project ID", () => {
6+
const project = new ProjectReflection("", new FileRegistry());
7+
ok(project === project.getReflectionById(project.id));
8+
});
9+
});

0 commit comments

Comments
 (0)