Skip to content

Commit

Permalink
Consider overload position when discovering comments
Browse files Browse the repository at this point in the history
Resolves #2755
  • Loading branch information
Gerrit0 committed Oct 27, 2024
1 parent ab8f4eb commit 18c4661
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

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

Expand Down
28 changes: 24 additions & 4 deletions src/lib/converter/comments/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ReflectionKind } from "../../models";
import { assertNever, type Logger } from "../../utils";
import { CommentStyle } from "../../utils/options/declaration";
import { nicePath } from "../../utils/paths";
import { ok } from "assert";
import assert, { ok } from "assert";
import { filter, firstDefined } from "../../utils/array";

const variablePropertyKinds = [
Expand Down Expand Up @@ -499,13 +499,33 @@ function declarationToCommentNodes(
},
];

let overloadIndex: number | undefined = undefined;
if (ts.isMethodDeclaration(node)) {
const symbol = checker.getSymbolAtLocation(node.name || node);
if (symbol) {
overloadIndex = symbol.declarations
?.filter((d) => d.kind === node.kind)
.indexOf(node);
assert(overloadIndex !== -1, "Should always find declaration");
}
}

const seenSymbols = new Set<ts.Symbol>();
const bases = findBaseOfDeclaration(checker, node, (symbol) => {
if (!seenSymbols.has(symbol)) {
seenSymbols.add(symbol);
return symbol.declarations?.map(
(node) => declarationToCommentNodeIgnoringParents(node) || node,
);
if (overloadIndex === undefined) {
return symbol.declarations?.map(
(node) =>
declarationToCommentNodeIgnoringParents(node) || node,
);
} else if (symbol.declarations?.[overloadIndex]) {
const parentSigNode = symbol.declarations[overloadIndex];
return [
declarationToCommentNodeIgnoringParents(parentSigNode) ||
parentSigNode,
];
}
}
});

Expand Down
27 changes: 27 additions & 0 deletions src/test/converter2/issues/gh2755.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface Test {
/** A */
method(a: string): boolean;

/** B */
method(a: number): number;
}

export class Class implements Test {
method(a: string): boolean;
method(a: number): number;
method(a: string | number): number | boolean {
if (typeof a === "string") {
return false;
}
return 1;
}
}

export interface MultiCallSignature {
/** A */
(): string;
/** B */
(x: string): string;
}

export const Callable: MultiCallSignature = () => "";
12 changes: 12 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1798,4 +1798,16 @@ describe("Issue Tests", () => {
{ display: "Node", target: "https://typescriptlang.org" },
]);
});

it("#2755 handles multiple signature when discovering inherited comments", () => {
const project = convert();
equal(getSigComment(project, "Test.method", 0), "A");
equal(getSigComment(project, "Test.method", 1), "B");

equal(getSigComment(project, "Class.method", 0), "A");
equal(getSigComment(project, "Class.method", 1), "B");

equal(getSigComment(project, "Callable", 0), "A");
equal(getSigComment(project, "Callable", 1), "B");
});
});

0 comments on commit 18c4661

Please sign in to comment.