Skip to content

Commit

Permalink
Improve support for inheriting documentation from accessors
Browse files Browse the repository at this point in the history
Resolves #1968
  • Loading branch information
Gerrit0 committed Jun 29, 2022
1 parent 1bae4ff commit fa51445
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,11 @@
### Bug Fixes

- TypeDoc will no longer crash if a comment contains an empty `@example` tag, #1967.
- TypeDoc will now detect attempted inheritance from accessors and inherit from the getter or setter, #1968.

### Features

- Added support for TypeDoc specific `:getter` and `:setter` meaning keywords in declaration references.

## v0.23.2 (2022-06-28)

Expand Down
4 changes: 4 additions & 0 deletions src/lib/converter/comments/declarationReference.ts
Expand Up @@ -23,6 +23,10 @@ export const MeaningKeywords = [
"new", // SymbolFlags.Signature (for __new)
"index", // SymbolFlags.Signature (for __index)
"complex", // Any complex type

// TypeDoc specific
"getter",
"setter",
] as const;
export type MeaningKeyword = typeof MeaningKeywords[number];

Expand Down
17 changes: 16 additions & 1 deletion src/lib/converter/comments/declarationReferenceResolver.ts
Expand Up @@ -5,7 +5,7 @@ import {
Reflection,
ReflectionKind,
} from "../../models";
import { filterMap } from "../../utils";
import { assertNever, filterMap } from "../../utils";
import type {
ComponentPath,
DeclarationReference,
Expand Down Expand Up @@ -166,6 +166,21 @@ function resolveKeyword(
case "complex":
if (refl.kindOf(ReflectionKind.SomeType)) return [refl];
break;

case "getter":
if ((refl as DeclarationReflection).getSignature) {
return [(refl as DeclarationReflection).getSignature!];
}
break;

case "setter":
if ((refl as DeclarationReflection).setSignature) {
return [(refl as DeclarationReflection).setSignature!];
}
break;

default:
assertNever(kw);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/lib/converter/plugins/InheritDocPlugin.ts
@@ -1,6 +1,7 @@
import {
Comment,
DeclarationReflection,
ReflectionKind,
SignatureReflection,
} from "../../models";
import { Component, ConverterComponent } from "../components";
Expand Down Expand Up @@ -77,6 +78,16 @@ export class InheritDocPlugin extends ConverterComponent {
}
}

if (
sourceRefl instanceof DeclarationReflection &&
sourceRefl.kindOf(ReflectionKind.Accessor)
) {
// Accessors, like functions, never have comments on their actual root reflection.
// If the user didn't specify whether to inherit from the getter or setter, then implicitly
// try to inherit from the getter, #1968.
sourceRefl = sourceRefl.getSignature || sourceRefl.setSignature;
}

if (!sourceRefl) {
this.application.logger.warn(
`Failed to find "${source}" to inherit the comment from in the comment for ${reflection.getFullName()}`
Expand Down
19 changes: 19 additions & 0 deletions src/test/converter2/issues/gh1968.ts
@@ -0,0 +1,19 @@
export class Foo {
/** getter */
get x() {
return 1;
}
/** setter */
set x(value: number) {
throw new Error();
}
}

export class Bar {
/** {@inheritDoc Foo.x} */
x = 1;
/** {@inheritDoc Foo.x:getter} */
y = 2;
/** {@inheritDoc Foo.x:setter} */
z = 3;
}
7 changes: 7 additions & 0 deletions src/test/issueTests.ts
Expand Up @@ -548,4 +548,11 @@ export const issueTests: {
},
]);
},

gh1968(project) {
const comments = ["Bar.x", "Bar.y", "Bar.z"].map((n) =>
Comment.combineDisplayParts(query(project, n).comment?.summary)
);
equal(comments, ["getter", "getter", "setter"]);
},
};

0 comments on commit fa51445

Please sign in to comment.