Skip to content

Commit

Permalink
Fix possibility of absolute paths, resolves #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Nov 15, 2024
1 parent 3d3b69d commit 9348530
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 3.0.1 (2024-10-15)

- Fixed an issue where modules could be created by this plugin with names
including an absolute path, #12.

### 3.0.0 (2024-06-22)

- Support TypeDoc 0.26
Expand Down
14 changes: 14 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { relative } from "path";
import {
Application,
Context,
Expand Down Expand Up @@ -127,6 +128,19 @@ export function load(app: Application) {
if (refl.kindOf(ModuleLike)) {
knownPrograms.set(refl, context.program);
}

// #12 - This plugin might cause TypeDoc to convert some module without
// an export symbol to give it a name other than the full absolute
// path to the symbol. Detect this and rename it to a relative path
// based on base path if specified or CWD.
const symbol = context.project.getSymbolFromReflection(refl);
const file = symbol?.declarations?.find(ts.isSourceFile);
if (file && /^".*"$/.test(refl.name)) {
refl.name = relative(
app.options.getValue("basePath") || process.cwd(),
file.fileName,
);
}
},
);

Expand Down
25 changes: 25 additions & 0 deletions test/packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ beforeAll(async () => {
afterEach(() => {
app.options.reset("internalModule");
app.options.reset("placeInternalsInOwningModule");
app.options.reset("basePath");

expect(logger.messages).toEqual([]);
logger.messages = [];
Expand Down Expand Up @@ -159,6 +160,30 @@ test("Missing declaration", () => {
expect(toStringHierarchy(project)).toBe(hierarchy);
});

// https://github.com/Gerrit0/typedoc-plugin-missing-exports/issues/12
test("Issue #12", () => {
const project = convert("gh12/index.ts");

const hierarchy = outdent`
Module <internal>
Namespace test/packages/gh12/mod.ts
Variable ReferencesModule
`;

expect(toStringHierarchy(project)).toBe(hierarchy);

Check failure on line 173 in test/packages.test.ts

View workflow job for this annotation

GitHub Actions / Node LTS Windows

test/packages.test.ts > Issue #12

AssertionError: expected 'Module <internal>\n\tNamespace test\p…' to be 'Module <internal>\n\tNamespace test/p…' // Object.is equality - Expected + Received Module <internal> - Namespace test/packages/gh12/mod.ts + Namespace test\packages\gh12\mod.ts Variable ReferencesModule ❯ test/packages.test.ts:173:37

app.options.setValue("basePath", "test/packages");
const project2 = convert("gh12/index.ts");

const hierarchy2 = outdent`
Module <internal>
Namespace gh12/mod.ts
Variable ReferencesModule
`;

expect(toStringHierarchy(project2)).toBe(hierarchy2);
});

// https://github.com/Gerrit0/typedoc-plugin-missing-exports/issues/15
test("Issue #15", () => {
const project = convert("gh15/index.ts");
Expand Down
3 changes: 3 additions & 0 deletions test/packages/gh12/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export declare const ReferencesModule: {
mod: typeof import("./mod.js");
};
1 change: 1 addition & 0 deletions test/packages/gh12/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const abc = 123;

0 comments on commit 9348530

Please sign in to comment.