Skip to content

Commit 589c01a

Browse files
refactor: extract jsDocs metadata render helper (#72)
1 parent 3dba048 commit 589c01a

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

src/lib/parser/jdocs/mapper.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {JSDocTagInfo, SymbolDisplayPart} from 'typescript';
2-
import type {Params} from '../types';
2+
import type {JsDocsMetadata, Params} from '../types';
33

44
const jsDocsToSymbolDisplayParts = ({
55
jsDocs = [],
@@ -20,12 +20,12 @@ const jsDocsToSymbolDisplayParts = ({
2020
}, []);
2121
};
2222

23-
export const jsDocsToReturnType = (jsDocs?: JSDocTagInfo[]): string => {
23+
const jsDocsToReturnType = (jsDocs?: JSDocTagInfo[]): string => {
2424
const returns = jsDocsToSymbolDisplayParts({jsDocs, tagInfoName: 'returns'});
2525
return returns.map((parts) => parts.map(({text}) => text).join('')).join(' ');
2626
};
2727

28-
export const jsDocsToReferences = (jsDocs?: JSDocTagInfo[]): string[] => {
28+
const jsDocsToReferences = (jsDocs?: JSDocTagInfo[]): string[] => {
2929
const sees = jsDocsToSymbolDisplayParts({jsDocs, tagInfoName: 'see'});
3030

3131
return sees
@@ -55,11 +55,17 @@ export const jsDocsToParams = (jsDocs?: JSDocTagInfo[]): Params[] => {
5555
return params.map(toParam).filter((param) => param !== undefined) as Params[];
5656
};
5757

58-
export const jsDocsToExamples = (jsDocs: JSDocTagInfo[]): string[] => {
58+
const jsDocsToExamples = (jsDocs: JSDocTagInfo[]): string[] => {
5959
const examples: JSDocTagInfo[] = jsDocs.filter(({name}: JSDocTagInfo) => name === 'example');
6060
const texts = examples
6161
.map(({text}) => text)
6262
.filter(Boolean)
6363
.flat(1) as SymbolDisplayPart[];
6464
return texts.map(({text}) => text).filter(Boolean);
6565
};
66+
67+
export const jsDocsMetadata = (jsDocs?: JSDocTagInfo[]): JsDocsMetadata => ({
68+
returnType: jsDocsToReturnType(jsDocs),
69+
references: jsDocsToReferences(jsDocs),
70+
examples: [...jsDocsToExamples(jsDocs ?? [])]
71+
});

src/lib/parser/markdown.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import type {
66
MarkdownHeadingLevel,
77
MarkdownOptions
88
} from '../types';
9-
import {
10-
jsDocsToExamples,
11-
jsDocsToParams,
12-
jsDocsToReferences,
13-
jsDocsToReturnType
14-
} from './jdocs/mapper';
9+
import {jsDocsMetadata, jsDocsToParams} from './jdocs/mapper';
1510
import {emojiTitle, metadataToMarkdown, sourceCodeLink} from './render';
1611
import type {Params, Row} from './types';
1712

@@ -205,9 +200,7 @@ const toMarkdown = ({
205200
type: type ?? '',
206201
documentation: documentation ?? '',
207202
params: [...toParams(parameters), ...jsDocsToParams(jsDocs)],
208-
returnType: jsDocsToReturnType(jsDocs),
209-
references: jsDocsToReferences(jsDocs),
210-
examples: [...jsDocsToExamples(jsDocs ?? [])],
203+
...jsDocsMetadata(jsDocs),
211204
url
212205
})
213206
);

src/lib/parser/render.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {DocEntry, MarkdownEmoji, MarkdownOptions} from '../types';
22
import {inlineReferences} from './jdocs/render';
3-
import type {MappedJsDocs} from './types';
3+
import type {JsDocsMetadata} from './types';
44

55
export const emojiTitle = ({
66
emoji,
@@ -22,7 +22,8 @@ export const metadataToMarkdown = ({
2222
examples,
2323
url,
2424
emoji
25-
}: MappedJsDocs & Pick<DocEntry, 'url'> & {emoji: MarkdownEmoji | null | undefined}): string[] => {
25+
}: JsDocsMetadata &
26+
Pick<DocEntry, 'url'> & {emoji: MarkdownEmoji | null | undefined}): string[] => {
2627
const markdown: string[] = [];
2728

2829
if (returnType !== undefined && returnType !== '') {

src/lib/parser/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface Params {
55
documentation: string;
66
}
77

8-
export interface MappedJsDocs {
8+
export interface JsDocsMetadata {
99
examples: string[];
1010
returnType?: string;
1111
references?: string[];
@@ -14,4 +14,4 @@ export interface MappedJsDocs {
1414
export type Row = Required<Pick<DocEntry, 'name' | 'type' | 'documentation'>> &
1515
Pick<DocEntry, 'url'> & {
1616
params: Params[];
17-
} & MappedJsDocs;
17+
} & JsDocsMetadata;

0 commit comments

Comments
 (0)