Skip to content

Commit

Permalink
Make declare module "foo" create a module
Browse files Browse the repository at this point in the history
Resolves #2778
  • Loading branch information
Gerrit0 committed Nov 24, 2024
1 parent 9ba6e2d commit 876c690
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ title: Changelog
- TypeDoc can now link to type alias properties, #2524.
- TypeDoc will now document the merged symbol type when considering globals
declared inside `declare global`, #2774
- TypeDoc now converts `declare module "foo"` as a module rather than a namespace, #2778.
- Fixed an issue where properties were not properly marked optional in some
cases. This primarily affected destructured parameters.
- Added `yaml` to the highlight languages supported by default.
Expand Down
16 changes: 15 additions & 1 deletion src/lib/converter/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,24 @@ function convertNamespace(
if (existingReflection?.kind === ReflectionKind.Namespace) {
reflection = existingReflection as DeclarationReflection;
} else {
let kind = ReflectionKind.Namespace;
let nameOverride: string | undefined;

// #2778 - always treat declare module "foo" as a module, not a namespace
const declareModule = symbol.declarations?.find(
(mod): mod is ts.ModuleDeclaration =>
ts.isModuleDeclaration(mod) && ts.isStringLiteral(mod.name),
);
if (declareModule) {
kind = ReflectionKind.Module;
nameOverride = declareModule.name.text;
}

reflection = context.createDeclarationReflection(
ReflectionKind.Namespace,
kind,
symbol,
exportSymbol,
nameOverride,
);
context.finalizeDeclarationReflection(reflection);
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/converter2/issues/gh2778.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare module "common" {
export class Base {}
}
declare module "foo/bar1" {
import { Base } from "common";
export class Bar1 extends Base {}
}
declare module "foo/bar2" {
import { Base } from "common";
export class Bar2 extends Base {}
}
16 changes: 16 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1908,4 +1908,20 @@ describe("Issue Tests", () => {
["Extensions"],
);
});

it("#2778 creates modules for declare module blocks", () => {
const project = convert();
equal(
project.children?.map((c) => c.name),
["common", "foo/bar1", "foo/bar2"],
);
equal(
project.children.map((c) => c.kind),
[
ReflectionKind.Module,
ReflectionKind.Module,
ReflectionKind.Module,
],
);
});
});

0 comments on commit 876c690

Please # to comment.