Skip to content

Commit b8de8bf

Browse files
committed
Prefer variable comments
Resolves #2042
1 parent 041c35b commit b8de8bf

File tree

7 files changed

+73
-3
lines changed

7 files changed

+73
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
- TypeDoc will now treat `@typedef {import("foo").Bar<Z>} Baz` type declarations which forward type parameters to the imported
66
symbol as re-exports of that symbol, #2044.
77

8+
### Bug Fixes
9+
10+
- TypeDoc will now prefer comments on variable declarations over signature comments, #2042.
11+
812
## v0.23.14 (2022-09-03)
913

1014
### Features

src/lib/converter/factories/signature.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from "typescript";
22
import * as assert from "assert";
33
import {
4+
ConversionFlags,
45
DeclarationReflection,
56
IntrinsicType,
67
ParameterReflection,
@@ -41,7 +42,15 @@ export function createSignature(
4142
kind,
4243
context.scope
4344
);
44-
if (declaration) {
45+
46+
// If we are creating signatures for a variable and the variable has a comment associated with it
47+
// then we should prefer that variable's comment over any comment on the signature. The comment plugin
48+
// will copy the comment down if this signature doesn't have one, so don't set one.
49+
if (
50+
declaration &&
51+
(!context.scope.comment ||
52+
!(context.scope.conversionFlags & ConversionFlags.VariableSource))
53+
) {
4554
sigRef.comment = getSignatureComment(
4655
declaration,
4756
context.converter.config,

src/lib/converter/symbols.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Reflection,
99
ReflectionFlag,
1010
ReflectionKind,
11+
ConversionFlags,
1112
} from "../models";
1213
import {
1314
getEnumFlags,
@@ -894,7 +895,7 @@ function convertVariableAsFunction(
894895
exportSymbol
895896
);
896897
setModifiers(symbol, accessDeclaration, reflection);
897-
898+
reflection.conversionFlags |= ConversionFlags.VariableSource;
898899
context.finalizeDeclarationReflection(reflection);
899900

900901
const reflectionContext = context.withScope(reflection);

src/lib/models/reflections/declaration.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export interface DeclarationHierarchy {
2929
isTarget?: boolean;
3030
}
3131

32+
/**
33+
* @internal
34+
*/
35+
export enum ConversionFlags {
36+
None = 0,
37+
VariableSource = 1,
38+
}
39+
3240
/**
3341
* A reflection that represents a single declaration emitted by the TypeScript compiler.
3442
*
@@ -140,6 +148,12 @@ export class DeclarationReflection extends ContainerReflection {
140148
*/
141149
version?: string;
142150

151+
/**
152+
* Flags for information about a reflection which is needed solely during conversion.
153+
* @internal
154+
*/
155+
conversionFlags = ConversionFlags.None;
156+
143157
override hasGetterOrSetter(): boolean {
144158
return !!this.getSignature || !!this.setSignature;
145159
}

src/lib/models/reflections/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export {
66
} from "./abstract";
77
export type { TraverseCallback } from "./abstract";
88
export { ContainerReflection } from "./container";
9-
export { DeclarationReflection } from "./declaration";
9+
export { DeclarationReflection, ConversionFlags } from "./declaration";
1010
export type { DeclarationHierarchy } from "./declaration";
1111
export { ReflectionKind } from "./kind";
1212
export { ParameterReflection } from "./parameter";

src/test/converter2/issues/gh2042.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function factory() {
2+
/** inner docs */
3+
function fn() {}
4+
return fn;
5+
}
6+
7+
export const built = factory();
8+
9+
/** outer docs */
10+
export const built2 = factory();
11+
12+
const obj = {
13+
/** inner docs */
14+
fn(x: unknown) {},
15+
};
16+
17+
export const fn = obj.fn;
18+
19+
/**
20+
* outer docs
21+
* @param x param-docs
22+
*/
23+
export const fn2 = obj.fn;

src/test/issueTests.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,25 @@ export const issueTests: {
768768
equal(AnotherCtor.type.declaration.signatures?.length, 1);
769769
},
770770

771+
gh2042(project) {
772+
for (const [name, docs] of [
773+
["built", "inner docs"],
774+
["built2", "outer docs"],
775+
["fn", "inner docs"],
776+
["fn2", "outer docs"],
777+
]) {
778+
const refl = query(project, name);
779+
ok(refl.signatures?.[0]);
780+
equal(
781+
Comment.combineDisplayParts(
782+
refl.signatures[0].comment?.summary
783+
),
784+
docs,
785+
name
786+
);
787+
}
788+
},
789+
771790
gh2044(project) {
772791
for (const [name, ref] of [
773792
["Foo", false],

0 commit comments

Comments
 (0)