Skip to content

Commit

Permalink
Conditionally serialize media->reflection entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Aug 18, 2024
1 parent 73e1dd6 commit aa2b4a9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- The `suppressCommentWarningsInDeclarationFiles` option now correctly ignores warnings in `.d.cts` and `.d.mts` files, #2647.
- Restored re-exports in the page navigation menu, #2671.
- JSON serialized projects will no longer contain reflection IDs for other projects created in the same run. Gerrit0/typedoc-plugin-zod#6.
- 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.

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

Expand Down
5 changes: 2 additions & 3 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import { validateLinks } from "./validation/links";
import { ApplicationEvents } from "./application-events";
import { findTsConfigFile } from "./utils/tsconfig";
import { deriveRootDir, glob, readFile } from "./utils/fs";
import { resetReflectionID } from "./models/reflections/abstract";
import { addInferredDeclarationMapPaths } from "./models/reflections/ReflectionSymbolId";
import {
Internationalization,
Expand Down Expand Up @@ -706,13 +705,13 @@ export class Application extends ChildableComponent<
}

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

resetReflectionID();
continue;
}

this.options = origOptions;
Expand Down
6 changes: 5 additions & 1 deletion src/lib/models/FileRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ export class FileRegistry {
result.entries[key] = normalizePath(relative(ser.projectRoot, val));
}
for (const [key, val] of this.mediaToReflection.entries()) {
result.reflections[key] = val.id;
// A registry may be shared by multiple projects. When serializing,
// only save reflection mapping for reflections in the serialized project.
if (ser.project.getReflectionById(val.id)) {
result.reflections[key] = val.id;
}
}

return result;
Expand Down
20 changes: 18 additions & 2 deletions src/lib/models/reflections/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ReferenceReflection } from "./reference";
import type { DeclarationReflection } from "./declaration";
import type { SignatureReflection } from "./signature";
import type { ParameterReflection } from "./parameter";
import { IntrinsicType } from "../types";
import { IntrinsicType, makeRecursiveVisitor, Type } from "../types";

Check failure on line 7 in src/lib/models/reflections/project.ts

View workflow job for this annotation

GitHub Actions / Node 18 Windows

Imports "Type" are only used as type

Check failure on line 7 in src/lib/models/reflections/project.ts

View workflow job for this annotation

GitHub Actions / Node 18

Imports "Type" are only used as type

Check failure on line 7 in src/lib/models/reflections/project.ts

View workflow job for this annotation

GitHub Actions / Node 20

Imports "Type" are only used as type

Check failure on line 7 in src/lib/models/reflections/project.ts

View workflow job for this annotation

GitHub Actions / Node 22 Release

Imports "Type" are only used as type
import type { TypeParameterReflection } from "./type-parameter";
import { assertNever, removeIf, removeIfPresent } from "../../utils";
import type * as ts from "typescript";
Expand Down Expand Up @@ -49,7 +49,7 @@ export class ProjectReflection extends ContainerReflection {
*
* This may be replaced with a `Map<number, Reflection>` someday.
*/
reflections: { [id: number]: Reflection } = {};
reflections: { [id: number]: Reflection } = { [this.id]: this };

/**
* The name of the package that this reflection documents according to package.json.
Expand Down Expand Up @@ -153,6 +153,22 @@ export class ProjectReflection extends ContainerReflection {
}
}

/**
* Removes references to reflections contained within the provided type.
* Plugins which overwrite types on reflections should pass the type to this
* method before overwriting the property.
* @since 0.26.6
*/
removeTypeReflections(type: Type | undefined) {
type?.visit(
makeRecursiveVisitor({
reflection: (type) => {
this.removeReflection(type.declaration);
},
}),
);
}

/**
* Removes a reflection from the documentation. Can be used by plugins to filter reflections
* out of the generated documentation. Has no effect if the reflection is not present in the
Expand Down
1 change: 0 additions & 1 deletion src/lib/serialization/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ export class Deserializer {
name || projectObj.name,
registry,
);
project.registerReflection(project, undefined, undefined);
this.project = project;
this.projectRoot = projectRoot;
this.oldIdToNewId = { [projectObj.id]: project.id };
Expand Down
9 changes: 9 additions & 0 deletions src/lib/serialization/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export class Serializer extends EventDispatcher<SerializerEvents> {
*/
projectRoot!: string;

/**
* Only set when serializing
*/
project!: ProjectReflection;

addSerializer<T extends object>(serializer: SerializerComponent<T>): void {
insertPrioritySorted(this.serializers, serializer);
}
Expand Down Expand Up @@ -75,6 +80,7 @@ export class Serializer extends EventDispatcher<SerializerEvents> {
projectRoot: string,
): ModelToObject<ProjectReflection> {
this.projectRoot = projectRoot;
this.project = value;

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

this.project = undefined!;
this.projectRoot = undefined!;

return project;
}
}
9 changes: 9 additions & 0 deletions src/test/models/reflections.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ok } from "assert";
import { FileRegistry, ProjectReflection } from "../../lib/models";

describe("ProjectReflection", () => {
it("getReflectionById works with the project ID", () => {
const project = new ProjectReflection("", new FileRegistry());
ok(project === project.getReflectionById(project.id));
});
});

0 comments on commit aa2b4a9

Please sign in to comment.