Skip to content

Commit 657045c

Browse files
committed
Fix duplicate comments caused by signatures
Resolves #2509
1 parent 9751e31 commit 657045c

File tree

6 files changed

+54
-23
lines changed

6 files changed

+54
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Bug Fixes
44

55
- Constructed references to enum types will be properly linked with `@interface`, #2508.
6+
- Comments on property-methods will no longer be duplicated in generated documentation, #2509.
67
- Reduced rendered docs size by writing icons to a referenced SVG asset, #2505.
78
For TypeDoc's docs, this reduced the rendered documentation size by ~30%.
89
- The HTML docs now attempt to reduce repaints caused by dynamically loading the navigation, #2491.

src/lib/converter/plugins/CommentPlugin.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -453,23 +453,27 @@ export class CommentPlugin extends ConverterComponent {
453453
}
454454

455455
// Since this reflection has signatures, we need to remove the comment from the non-primary
456-
// declaration location. For functions, this means removing it from the Function reflection.
457-
// For type aliases, this means removing it from reflection.type.declaration.
456+
// declaration location. For functions/methods/constructors, this means removing it from
457+
// the wrapping reflection. For type aliases, classes, and interfaces, this means removing
458+
// it from the contained signatures... if it's the same as what is on the signature.
458459
// This is important so that in type aliases we don't end up with a comment rendered twice.
459-
if (
460-
reflection.kindOf(
461-
ReflectionKind.FunctionOrMethod | ReflectionKind.Constructor,
462-
)
463-
) {
460+
if (reflection.kindOf(ReflectionKind.SignatureContainer)) {
464461
delete reflection.comment;
465-
}
466-
if (reflection.kindOf(ReflectionKind.TypeAlias)) {
462+
} else {
467463
reflection.comment?.removeTags("@param");
468464
reflection.comment?.removeTags("@typeParam");
469465
reflection.comment?.removeTags("@template");
470466

467+
const parentComment = Comment.combineDisplayParts(
468+
reflection.comment?.summary,
469+
);
471470
for (const sig of signatures) {
472-
delete sig.comment;
471+
if (
472+
Comment.combineDisplayParts(sig.comment?.summary) ===
473+
parentComment
474+
) {
475+
delete sig.comment;
476+
}
473477
}
474478
}
475479
}

src/lib/output/components.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ export abstract class ContextAwareRendererComponent extends RendererComponent {
2929
/**
3030
* The url of the document that is being currently generated.
3131
* Set when a page begins rendering.
32+
*
33+
* Defaulted to '.' so that tests don't have to set up events.
3234
*/
33-
private location!: string;
35+
private location = ".";
3436

3537
/**
3638
* Regular expression to test if a string looks like an external url.

src/lib/validation/documentation.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ export function validateDocumentation(
6161
toProcess.push(ref.parent);
6262
continue;
6363
}
64-
// Ditto for signatures on type aliases.
64+
// Call signatures are considered documented if they have a comment directly, or their
65+
// container has a comment and they are directly within a type literal belonging to that container.
6566
if (
6667
ref.kindOf(ReflectionKind.CallSignature) &&
67-
ref.parent?.parent?.kindOf(ReflectionKind.TypeAlias)
68+
ref.parent?.kindOf(ReflectionKind.TypeLiteral)
6869
) {
69-
toProcess.push(ref.parent.parent);
70+
toProcess.push(ref.parent.parent!);
7071
continue;
7172
}
7273

@@ -78,9 +79,13 @@ export function validateDocumentation(
7879

7980
if (signatures.length) {
8081
// We've been asked to validate this reflection, so we should validate that
81-
// signatures all have comments, but we'll still have a comment here because
82-
// type aliases always have their own comment.
82+
// signatures all have comments
8383
toProcess.push(...signatures);
84+
85+
if (ref.kindOf(ReflectionKind.SignatureContainer)) {
86+
// Comments belong to each signature, and will not be included on this object.
87+
continue;
88+
}
8489
}
8590
}
8691

src/test/converter2/issues/gh2509.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface Int {
2+
/** Cb */
3+
cb: () => Promise<any>;
4+
5+
nested: {
6+
/** Cb2 */
7+
cb: () => any;
8+
};
9+
}

src/test/issues.c2.test.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ describe("Issue Tests", () => {
373373

374374
equal(
375375
Comment.combineDisplayParts(
376-
query(project, "Foo.baz").signatures?.[0]?.comment?.summary,
376+
query(project, "Foo.baz").comment?.summary,
377377
),
378378
"Some property style doc.",
379379
"Property methods declared in interface should still allow comment inheritance",
@@ -944,12 +944,7 @@ describe("Issue Tests", () => {
944944
const project = convert();
945945
const hook = query(project, "Camera.useCameraPermissions");
946946
equal(hook.type?.type, "reflection" as const);
947-
equal(
948-
Comment.combineDisplayParts(
949-
hook.type.declaration.signatures![0].comment?.summary,
950-
),
951-
"One",
952-
);
947+
equal(Comment.combineDisplayParts(hook.comment?.summary), "One");
953948
});
954949

955950
it("#2150", () => {
@@ -1420,4 +1415,19 @@ describe("Issue Tests", () => {
14201415
equal(refl.type.toString(), "Color");
14211416
equal(refl.type.reflection?.id, query(project, "Color").id);
14221417
});
1418+
1419+
it("Does not duplicate comments due to signatures being present, #2509", () => {
1420+
const project = convert();
1421+
const cb = query(project, "Int.cb");
1422+
equal(Comment.combineDisplayParts(cb.comment?.summary), "Cb");
1423+
equal(cb.type?.type, "reflection");
1424+
equal(cb.type.declaration.signatures![0].comment, undefined);
1425+
1426+
const nested = query(project, "Int.nested");
1427+
equal(nested.type?.type, "reflection");
1428+
const cb2 = nested.type.declaration.children![0];
1429+
equal(Comment.combineDisplayParts(cb2.comment?.summary), "Cb2");
1430+
equal(cb2.type?.type, "reflection");
1431+
equal(cb2.type.declaration.signatures![0].comment, undefined);
1432+
});
14231433
});

0 commit comments

Comments
 (0)