Skip to content

Commit 18c4661

Browse files
committed
Consider overload position when discovering comments
Resolves #2755
1 parent ab8f4eb commit 18c4661

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fix support for ESM config files with Node 23, #2752.
66
- Fix type errors when using `"module": "ESNext"` and importing TypeDoc, #2747.
7+
- Inherited comments on overloaded methods now consider the overload position when inheriting a comment, #2755.
78

89
## v0.26.10 (2024-10-16)
910

src/lib/converter/comments/discovery.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ReflectionKind } from "../../models";
33
import { assertNever, type Logger } from "../../utils";
44
import { CommentStyle } from "../../utils/options/declaration";
55
import { nicePath } from "../../utils/paths";
6-
import { ok } from "assert";
6+
import assert, { ok } from "assert";
77
import { filter, firstDefined } from "../../utils/array";
88

99
const variablePropertyKinds = [
@@ -499,13 +499,33 @@ function declarationToCommentNodes(
499499
},
500500
];
501501

502+
let overloadIndex: number | undefined = undefined;
503+
if (ts.isMethodDeclaration(node)) {
504+
const symbol = checker.getSymbolAtLocation(node.name || node);
505+
if (symbol) {
506+
overloadIndex = symbol.declarations
507+
?.filter((d) => d.kind === node.kind)
508+
.indexOf(node);
509+
assert(overloadIndex !== -1, "Should always find declaration");
510+
}
511+
}
512+
502513
const seenSymbols = new Set<ts.Symbol>();
503514
const bases = findBaseOfDeclaration(checker, node, (symbol) => {
504515
if (!seenSymbols.has(symbol)) {
505516
seenSymbols.add(symbol);
506-
return symbol.declarations?.map(
507-
(node) => declarationToCommentNodeIgnoringParents(node) || node,
508-
);
517+
if (overloadIndex === undefined) {
518+
return symbol.declarations?.map(
519+
(node) =>
520+
declarationToCommentNodeIgnoringParents(node) || node,
521+
);
522+
} else if (symbol.declarations?.[overloadIndex]) {
523+
const parentSigNode = symbol.declarations[overloadIndex];
524+
return [
525+
declarationToCommentNodeIgnoringParents(parentSigNode) ||
526+
parentSigNode,
527+
];
528+
}
509529
}
510530
});
511531

src/test/converter2/issues/gh2755.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export interface Test {
2+
/** A */
3+
method(a: string): boolean;
4+
5+
/** B */
6+
method(a: number): number;
7+
}
8+
9+
export class Class implements Test {
10+
method(a: string): boolean;
11+
method(a: number): number;
12+
method(a: string | number): number | boolean {
13+
if (typeof a === "string") {
14+
return false;
15+
}
16+
return 1;
17+
}
18+
}
19+
20+
export interface MultiCallSignature {
21+
/** A */
22+
(): string;
23+
/** B */
24+
(x: string): string;
25+
}
26+
27+
export const Callable: MultiCallSignature = () => "";

src/test/issues.c2.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1798,4 +1798,16 @@ describe("Issue Tests", () => {
17981798
{ display: "Node", target: "https://typescriptlang.org" },
17991799
]);
18001800
});
1801+
1802+
it("#2755 handles multiple signature when discovering inherited comments", () => {
1803+
const project = convert();
1804+
equal(getSigComment(project, "Test.method", 0), "A");
1805+
equal(getSigComment(project, "Test.method", 1), "B");
1806+
1807+
equal(getSigComment(project, "Class.method", 0), "A");
1808+
equal(getSigComment(project, "Class.method", 1), "B");
1809+
1810+
equal(getSigComment(project, "Callable", 0), "A");
1811+
equal(getSigComment(project, "Callable", 1), "B");
1812+
});
18011813
});

0 commit comments

Comments
 (0)