-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
MemberSignatureBody.tsx
123 lines (102 loc) · 3.24 KB
/
MemberSignatureBody.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// https://github.com/TypeStrong/typedoc-default-themes/blob/master/src/default/partials/member.signature.body.hbs
import React from 'react';
import type { JSONOutput } from 'typedoc';
import { useMinimalLayout } from '../hooks/useMinimalLayout';
import { Comment, hasComment } from './Comment';
import { CommentBadges, isCommentWithModifiers } from './CommentBadges';
import { DefaultValue } from './DefaultValue';
import { Flags } from './Flags';
import { hasSources, MemberSources } from './MemberSources';
import { Parameter } from './Parameter';
import { Type } from './Type';
import { TypeParameters } from './TypeParameters';
export function hasSigBody(
sig: JSONOutput.SignatureReflection | undefined,
minimal: boolean,
hideSources: boolean = false,
) {
if (!sig) {
return false;
}
return (
(!hideSources && hasSources(sig)) ||
hasComment(sig.comment) ||
(sig.typeParameter && sig.typeParameter.length > 0) ||
(!minimal && sig.parameters && sig.parameters.length > 0) ||
(!minimal && sig.type)
);
}
export interface MemberSignatureBodyProps {
hideSources?: boolean;
sig: JSONOutput.SignatureReflection;
}
function excludeBlockTags(comment?: JSONOutput.Comment): JSONOutput.Comment | undefined {
if (comment) {
const { blockTags, ...rest } = comment;
return rest;
}
return undefined;
}
function intoReturnComment(comment?: JSONOutput.Comment): JSONOutput.Comment | undefined {
if (comment?.blockTags) {
const tags = comment.blockTags.map((tag) => tag.tag);
if (tags.includes('@returns')) {
const index = tags.indexOf('@returns');
return {
summary: comment.blockTags[index].content,
};
}
}
return undefined;
}
// eslint-disable-next-line complexity
export function MemberSignatureBody({ hideSources, sig }: MemberSignatureBodyProps) {
const minimal = useMinimalLayout();
const showTypes = sig.typeParameter && sig.typeParameter.length > 0;
const showParams = !minimal && sig.parameters && sig.parameters.length > 0;
const showReturn = !minimal && sig.type;
return (
<>
{!hideSources && <MemberSources reflection={sig} />}
{isCommentWithModifiers(sig.comment) && <CommentBadges comment={sig.comment} />}
<Comment comment={excludeBlockTags(sig.comment)} />
{hasComment(sig.comment) && (showTypes || showParams || showReturn) && (
<hr className="tsd-divider" />
)}
{showTypes && (
<>
<h4 className="tsd-type-parameters-title">Type parameters</h4>
<TypeParameters params={sig.typeParameter} />
</>
)}
{showParams && (
<>
<h4 className="tsd-parameters-title">Parameters</h4>
<ul className="tsd-parameters">
{sig.parameters?.map((param) => (
<li key={param.id}>
<h5>
<Flags flags={param.flags} />
{param.flags?.isRest && <span className="tsd-signature-symbol">...</span>}
{`${param.name}: `}
<Type type={param.type} />
<DefaultValue type={param.defaultValue} />
</h5>
<Comment comment={param.comment} />
</li>
))}
</ul>
</>
)}
{showReturn && (
<>
<h4 className="tsd-returns-title">
Returns <Type type={sig.type} />
</h4>
<Comment comment={intoReturnComment(sig.comment)} />
<Parameter param={sig.type?.declaration} />
</>
)}
</>
);
}