Skip to content

Commit

Permalink
Fix call signature handling on classes/interfaces
Browse files Browse the repository at this point in the history
Resolves #2290
  • Loading branch information
Gerrit0 committed May 29, 2023
1 parent 5b9ead6 commit 2624c28
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
- Optimized icon caching to reduce file size in generated HTML documentation, #2287.
- Added `MarkdownEvent.INCLUDE` for plugins, #2284.

### Bug Fixes

- Comments are no longer removed from classes/interfaces containing call signatures, #2290.

### Thanks!

- @krisztianb
Expand Down
8 changes: 6 additions & 2 deletions src/lib/converter/plugins/CommentPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,16 @@ export class CommentPlugin extends ConverterComponent {
return;
}

const comment = reflection.comment;
const comment = reflection.kindOf(ReflectionKind.ClassOrInterface)
? undefined
: reflection.comment;

// Since this reflection has signatures, remove the comment from the parent
// reflection. This is important so that in type aliases we don't end up with
// a comment rendered twice.
delete reflection.comment;
if (!reflection.kindOf(ReflectionKind.ClassOrInterface)) {
delete reflection.comment;
}

for (const signature of signatures) {
const childComment = (signature.comment ||= comment?.clone());
Expand Down
8 changes: 8 additions & 0 deletions src/test/converter/interface/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,14 @@
"variant": "declaration",
"kind": 256,
"flags": {},
"comment": {
"summary": [
{
"kind": "text",
"text": "Function signature of an event listener callback"
}
]
},
"sources": [
{
"fileName": "interface-implementation.ts",
Expand Down
14 changes: 14 additions & 0 deletions src/test/converter2/issues/gh2290.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** Int comment */
export interface CallSignature {
/** Sig comment */
(x: string): void;
}

/** Int comment */
export interface CallSignature2 {
/** Sig comment */
(x: string): void;

/** Prop comment */
prop: 123;
}
24 changes: 23 additions & 1 deletion src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { TestLogger } from "./TestLogger";
import { clearCommentCache } from "../lib/converter/comments";
import { join } from "path";
import { existsSync } from "fs";
import { getLinks, query } from "./utils";
import { getComment, getLinks, query } from "./utils";

const base = getConverter2Base();
const app = getConverter2App();
Expand Down Expand Up @@ -1111,4 +1111,26 @@ describe("Issue Tests", () => {
},
]);
});

it("Handles comments on interfaces with call signatures #2290", () => {
const project = convert();

equal(getComment(project, "CallSignature"), "Int comment");
equal(getComment(project, "CallSignature2"), "Int comment");
equal(getComment(project, "CallSignature2.prop"), "Prop comment");

equal(
Comment.combineDisplayParts(
query(project, "CallSignature").signatures![0].comment?.summary
),
"Sig comment"
);

equal(
Comment.combineDisplayParts(
query(project, "CallSignature2").signatures![0].comment?.summary
),
"Sig comment"
);
});
});
5 changes: 5 additions & 0 deletions src/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ok } from "assert";
import {
Comment,
DeclarationReflection,
ProjectReflection,
Reflection,
Expand All @@ -13,6 +14,10 @@ export function query(project: ProjectReflection, name: string) {
return reflection;
}

export function getComment(project: ProjectReflection, name: string) {
return Comment.combineDisplayParts(query(project, name).comment?.summary);
}

export function getLinks(refl: Reflection): Array<{
display: string;
target: undefined | string | [ReflectionKind, string];
Expand Down

0 comments on commit 2624c28

Please sign in to comment.