Skip to content

Commit

Permalink
Handle @module on declare module "foo"
Browse files Browse the repository at this point in the history
Resolves #2401
  • Loading branch information
Gerrit0 committed Oct 6, 2023
1 parent b12258d commit 8e0aaf7
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Invalid link validation is now correctly suppressed before all projects have been converted in packages mode, #2403.
- Fixed tsconfig handling for projects using a solution-style tsconfig, #2406.
- Fixed broken settings icons caused by icon caching introduced in 0.25.1, #2408.
- Corrected module comment handling on declaration files containing a single `declare module "foo"`, #2401.

### Thanks!

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"test": "mocha --config .config/mocha.fast.json",
"test:cov": "c8 mocha --config .config/mocha.fast.json",
"doc:c": "node bin/typedoc --tsconfig src/test/converter/tsconfig.json",
"doc:c2": "node bin/typedoc --tsconfig src/test/converter2/tsconfig.json",
"doc:c2": "node --inspect-brk bin/typedoc --tsconfig src/test/converter2/tsconfig.json",
"test:full": "c8 mocha --config .config/mocha.full.json",
"test:visual": "ts-node ./src/test/capture-screenshots.ts && ./scripts/compare_screenshots.sh",
"test:visual:accept": "node scripts/accept_visual_regression.js",
Expand Down
10 changes: 8 additions & 2 deletions src/lib/converter/comments/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ const variablePropertyKinds = [
// the JSDoc converter because we only want part of the comment when
// getting them.
const wantedKinds: Record<ReflectionKind, ts.SyntaxKind[]> = {
[ReflectionKind.Project]: [ts.SyntaxKind.SourceFile],
[ReflectionKind.Module]: [ts.SyntaxKind.SourceFile],
[ReflectionKind.Project]: [
ts.SyntaxKind.SourceFile,
ts.SyntaxKind.ModuleDeclaration,
],
[ReflectionKind.Module]: [
ts.SyntaxKind.SourceFile,
ts.SyntaxKind.ModuleDeclaration,
],
[ReflectionKind.Namespace]: [
ts.SyntaxKind.ModuleDeclaration,
ts.SyntaxKind.SourceFile,
Expand Down
10 changes: 9 additions & 1 deletion src/lib/converter/comments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,19 @@ export function getComment(
);
}

const isModule = declarations.some((decl) => {
if (ts.isSourceFile(decl)) return true;
if (ts.isModuleDeclaration(decl) && ts.isStringLiteral(decl.name)) {
return true;
}
return false;
});

const comment = getCommentImpl(
discoverComment(symbol, kind, logger, commentStyle),
config,
logger,
declarations.some(ts.isSourceFile),
isModule,
checker,
);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/plugins/CommentPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export class CommentPlugin extends ConverterComponent {
const comment = reflection.comment;
if (!comment) return;

if (reflection.kindOf(ReflectionKind.Module)) {
if (reflection.kindOf(ReflectionKind.SomeModule)) {
const tag = comment.getTag("@module");
if (tag) {
// If no name is specified, this is a flag to mark a comment as a module comment
Expand Down
7 changes: 7 additions & 0 deletions src/test/converter2/issues/gh2401.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Comment for module
* @module foo:bar
*/
declare module "foo:bar" {
export function doSomething(a: any): void;
}
12 changes: 12 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1162,4 +1162,16 @@ describe("Issue Tests", () => {
"desc",
);
});

// This is rather unfortunate, we need to do this so that files which include only
// a single declare module can still have a comment on them, but it looks really
// weird and wrong if there are multiple declare module statements in a file...
// there's probably some nicer way of doing this that I'm not seeing right now.
it("Uses module comment discovery on 'declare module \"foo\"' #2401", () => {
const project = convert();
equal(
Comment.combineDisplayParts(project.comment?.summary),
"Comment for module",
);
});
});

0 comments on commit 8e0aaf7

Please sign in to comment.