Skip to content

Commit 7ac546c

Browse files
committed
Merge @returns and return type blocks in output
Resolves #2180
1 parent 53cf7f7 commit 7ac546c

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
### Features
44

55
- Added support for discovering a "module" comment on global files, #2165.
6+
- Function `@returns` blocks will now be rendered with the return type, #2180.
67

78
### Bug Fixes
89

910
- Type parameter constraints now respect the `--hideParameterTypesInTitle` option, #2226.
1011
- Even more contrast fixes, #2248.
1112
- Fix semantic highlighting for predicate type's parameter references, #2249.
1213
- Fixed inconsistent styling between type parameter lists and parameter lists.
14+
- TypeDoc will now warn if more than one `@returns` block is is present in a function, and ignore the duplicate blocks as specified by TSDoc.
1315

1416
## v0.24.4 (2023-04-16)
1517

src/lib/converter/comments/parser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ function postProcessComment(comment: Comment, warning: (msg: string) => void) {
154154
removeIf(comment.blockTags, (tag) => remarks.indexOf(tag) > 0);
155155
}
156156

157+
const returns = comment.blockTags.filter((tag) => tag.tag === "@returns");
158+
if (remarks.length > 1) {
159+
warning(
160+
"At most one @returns tag is expected in a comment, ignoring all but the first"
161+
);
162+
removeIf(comment.blockTags, (tag) => returns.indexOf(tag) > 0);
163+
}
164+
157165
const inheritDoc = comment.blockTags.filter(
158166
(tag) => tag.tag === "@inheritDoc"
159167
);

src/lib/output/themes/default/partials/comment.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import type { DefaultThemeRenderContext } from "../DefaultThemeRenderContext";
22
import { JSX, Raw } from "../../../../utils";
3-
import type { Reflection } from "../../../../models";
3+
import { Reflection, ReflectionKind } from "../../../../models";
44
import { camelToTitleCase } from "../../lib";
55

66
export function comment({ markdown }: DefaultThemeRenderContext, props: Reflection) {
77
if (!props.comment?.hasVisibleComponent()) return;
88

99
// Note: Comment modifiers are handled in `renderFlags`
1010

11+
const tags = props.kindOf(ReflectionKind.SomeSignature)
12+
? props.comment.blockTags.filter((tag) => tag.tag !== "@returns")
13+
: props.comment.blockTags;
14+
1115
return (
1216
<div class="tsd-comment tsd-typography">
1317
<Raw html={markdown(props.comment.summary)} />
14-
{props.comment.blockTags.map((item) => (
18+
{tags.map((item) => (
1519
<>
1620
<h3>{camelToTitleCase(item.tag.substring(1))}</h3>
1721
<Raw html={markdown(item.content)} />
Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,59 @@
11
import type { DefaultThemeRenderContext } from "../DefaultThemeRenderContext";
2-
import { JSX } from "../../../../utils";
2+
import { JSX, Raw } from "../../../../utils";
33
import { ReflectionType, SignatureReflection } from "../../../../models";
44
import { hasTypeParameters, renderFlags } from "../../lib";
55

6-
export const memberSignatureBody = (
6+
export function memberSignatureBody(
77
context: DefaultThemeRenderContext,
88
props: SignatureReflection,
99
{ hideSources = false }: { hideSources?: boolean } = {}
10-
) => (
11-
<>
12-
{renderFlags(props.flags, props.comment)}
13-
{context.comment(props)}
10+
) {
11+
const returnsTag = props.comment?.getTag("@returns");
1412

15-
{hasTypeParameters(props) && context.typeParameters(props.typeParameters)}
13+
return (
14+
<>
15+
{renderFlags(props.flags, props.comment)}
16+
{context.comment(props)}
1617

17-
{props.parameters && props.parameters.length > 0 && (
18-
<div class="tsd-parameters">
19-
<h4 class="tsd-parameters-title">Parameters</h4>
20-
<ul class="tsd-parameter-list">
21-
{props.parameters.map((item) => (
22-
<li>
23-
<h5>
24-
{renderFlags(item.flags, item.comment)}
25-
{!!item.flags.isRest && <span class="tsd-signature-symbol">...</span>}
26-
<span class="tsd-kind-parameter">{item.name}</span>
27-
{": "}
28-
{context.type(item.type)}
29-
{item.defaultValue != null && (
30-
<span class="tsd-signature-symbol">
31-
{" = "}
32-
{item.defaultValue}
33-
</span>
34-
)}
35-
</h5>
36-
{context.comment(item)}
37-
{item.type instanceof ReflectionType && context.parameter(item.type.declaration)}
38-
</li>
39-
))}
40-
</ul>
41-
</div>
42-
)}
43-
{props.type && (
44-
<>
45-
<h4 class="tsd-returns-title">
46-
{"Returns "}
47-
{context.type(props.type)}
48-
</h4>
49-
{props.type instanceof ReflectionType && context.parameter(props.type.declaration)}
50-
</>
51-
)}
52-
{!hideSources && context.memberSources(props)}
53-
</>
54-
);
18+
{hasTypeParameters(props) && context.typeParameters(props.typeParameters)}
19+
20+
{props.parameters && props.parameters.length > 0 && (
21+
<div class="tsd-parameters">
22+
<h4 class="tsd-parameters-title">Parameters</h4>
23+
<ul class="tsd-parameter-list">
24+
{props.parameters.map((item) => (
25+
<li>
26+
<h5>
27+
{renderFlags(item.flags, item.comment)}
28+
{!!item.flags.isRest && <span class="tsd-signature-symbol">...</span>}
29+
<span class="tsd-kind-parameter">{item.name}</span>
30+
{": "}
31+
{context.type(item.type)}
32+
{item.defaultValue != null && (
33+
<span class="tsd-signature-symbol">
34+
{" = "}
35+
{item.defaultValue}
36+
</span>
37+
)}
38+
</h5>
39+
{context.comment(item)}
40+
{item.type instanceof ReflectionType && context.parameter(item.type.declaration)}
41+
</li>
42+
))}
43+
</ul>
44+
</div>
45+
)}
46+
{props.type && (
47+
<>
48+
<h4 class="tsd-returns-title">
49+
{"Returns "}
50+
{context.type(props.type)}
51+
</h4>
52+
{returnsTag && <Raw html={context.markdown(returnsTag.content)} />}
53+
{props.type instanceof ReflectionType && context.parameter(props.type.declaration)}
54+
</>
55+
)}
56+
{!hideSources && context.memberSources(props)}
57+
</>
58+
);
59+
}

0 commit comments

Comments
 (0)