diff --git a/src/lib/converter/plugins/CommentPlugin.ts b/src/lib/converter/plugins/CommentPlugin.ts index 86bdf429b..bd26055a8 100644 --- a/src/lib/converter/plugins/CommentPlugin.ts +++ b/src/lib/converter/plugins/CommentPlugin.ts @@ -7,6 +7,7 @@ import { ReflectionKind, TypeParameterReflection, DeclarationReflection, + SignatureReflection, } from "../../models/reflections/index"; import { Component, ConverterComponent } from "../components"; import { @@ -17,7 +18,7 @@ import { import { Converter } from "../converter"; import { Context } from "../context"; import { partition, uniq } from "lodash"; -import { SourceReference } from "../../models"; +import { ReflectionType, SourceReference } from "../../models"; import { BindOption, filterMap, removeIfPresent } from "../../utils"; /** @@ -269,54 +270,74 @@ export class CommentPlugin extends ConverterComponent { return; } - const signatures = reflection.getAllSignatures(); - if (signatures.length) { - const comment = reflection.comment; - if (comment && comment.hasTag("returns")) { - comment.returns = comment.getTag("returns")!.text; - comment.removeTags("returns"); - } + if (reflection.type instanceof ReflectionType) { + this.addCommentToSignatures( + reflection, + reflection.type.declaration.getNonIndexSignatures() + ); + } else { + this.addCommentToSignatures( + reflection, + reflection.getNonIndexSignatures() + ); + } + } - signatures.forEach((signature) => { - let childComment = signature.comment; - if (childComment && childComment.hasTag("returns")) { - childComment.returns = childComment.getTag("returns")!.text; - childComment.removeTags("returns"); - } + private addCommentToSignatures( + reflection: DeclarationReflection, + signatures: SignatureReflection[] + ) { + if (!signatures.length) { + return; + } - if (comment) { - if (!childComment) { - childComment = signature.comment = new Comment(); - } + let movedComment = false; + const comment = reflection.comment; + if (comment && comment.hasTag("returns")) { + comment.returns = comment.getTag("returns")!.text; + comment.removeTags("returns"); + } - childComment.shortText = - childComment.shortText || comment.shortText; - childComment.text = childComment.text || comment.text; - childComment.returns = - childComment.returns || comment.returns; - childComment.tags = childComment.tags || comment.tags; - } + signatures.forEach((signature) => { + let childComment = signature.comment; + if (childComment && childComment.hasTag("returns")) { + childComment.returns = childComment.getTag("returns")!.text; + childComment.removeTags("returns"); + } - if (signature.parameters) { - signature.parameters.forEach((parameter) => { - let tag: CommentTag | undefined; - if (childComment) { - tag = childComment.getTag("param", parameter.name); - } - if (comment && !tag) { - tag = comment.getTag("param", parameter.name); - } - if (tag) { - parameter.comment = new Comment(tag.text); - } - }); + if (comment) { + if (!childComment) { + movedComment = true; + childComment = signature.comment = new Comment(); } - childComment?.removeTags("param"); - }); + childComment.shortText = + childComment.shortText || comment.shortText; + childComment.text = childComment.text || comment.text; + childComment.returns = childComment.returns || comment.returns; + childComment.tags = childComment.tags || comment.tags; + } - comment?.removeTags("param"); - } + if (signature.parameters) { + signature.parameters.forEach((parameter) => { + let tag: CommentTag | undefined; + if (childComment) { + tag = childComment.getTag("param", parameter.name); + } + if (comment && !tag) { + tag = comment.getTag("param", parameter.name); + } + if (tag) { + parameter.comment = new Comment(tag.text); + } + }); + } + + childComment?.removeTags("param"); + }); + + comment?.removeTags("param"); + if (movedComment) reflection.comment = void 0; } private removeExcludedTags(comment: Comment) { diff --git a/src/lib/models/reflections/declaration.ts b/src/lib/models/reflections/declaration.ts index 8719a64eb..15408c9f7 100644 --- a/src/lib/models/reflections/declaration.ts +++ b/src/lib/models/reflections/declaration.ts @@ -161,6 +161,15 @@ export class DeclarationReflection return result; } + /** @internal */ + getNonIndexSignatures(): SignatureReflection[] { + return ([] as SignatureReflection[]).concat( + this.signatures ?? [], + this.setSignature ?? [], + this.getSignature ?? [] + ); + } + /** * Traverse all potential child reflections of this reflection. * diff --git a/src/test/converter/alias/specs.json b/src/test/converter/alias/specs.json index 32112ed75..3f13dbb1e 100644 --- a/src/test/converter/alias/specs.json +++ b/src/test/converter/alias/specs.json @@ -93,9 +93,6 @@ "kind": 4194304, "kindString": "Type alias", "flags": {}, - "comment": { - "shortText": "A type that describes a compare function, e.g. for array.sort()." - }, "typeParameter": [ { "id": 6, @@ -120,6 +117,9 @@ "kind": 4096, "kindString": "Call signature", "flags": {}, + "comment": { + "shortText": "A type that describes a compare function, e.g. for array.sort()." + }, "parameters": [ { "id": 4, diff --git a/src/test/converter/mixin/specs.json b/src/test/converter/mixin/specs.json index d37523969..e6a596155 100644 --- a/src/test/converter/mixin/specs.json +++ b/src/test/converter/mixin/specs.json @@ -831,9 +831,6 @@ "kind": 4194304, "kindString": "Type alias", "flags": {}, - "comment": { - "shortText": "Any constructor function" - }, "typeParameter": [ { "id": 10, @@ -862,6 +859,9 @@ "kind": 16384, "kindString": "Constructor signature", "flags": {}, + "comment": { + "shortText": "Any constructor function" + }, "parameters": [ { "id": 9, @@ -895,9 +895,6 @@ "kind": 4194304, "kindString": "Type alias", "flags": {}, - "comment": { - "shortText": "Any function" - }, "typeParameter": [ { "id": 5, @@ -926,6 +923,9 @@ "kind": 4096, "kindString": "Call signature", "flags": {}, + "comment": { + "shortText": "Any function" + }, "parameters": [ { "id": 4, diff --git a/src/test/renderer/specs/classes/flattened.flattenedclass.html b/src/test/renderer/specs/classes/flattened.flattenedclass.html index c7d1364da..04e3e4a96 100644 --- a/src/test/renderer/specs/classes/flattened.flattenedclass.html +++ b/src/test/renderer/specs/classes/flattened.flattenedclass.html @@ -171,19 +171,6 @@

callback

callback: (param: number, optionalParam?: string) => string
-
-
-

A member that holds a callback that requires a typed function signature.

-
-
-
param
-

A parameter of the typed function callback.

-
-
param
-

An optional parameter of the typed function callback.

-
-
-

Type declaration