Skip to content

Commit 8bc0370

Browse files
authored
added the returns annotation to the generated documentation (#61)
fixes: #60 Signed-off-by: Erik Jan de Wit <[email protected]>
1 parent 12c09b1 commit 8bc0370

File tree

5 files changed

+54
-23
lines changed

5 files changed

+54
-23
lines changed

src/lib/markdown.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Row = Required<Pick<DocEntry, 'name' | 'type' | 'documentation'>> &
1616
Pick<DocEntry, 'url'> & {
1717
params: Params[];
1818
examples: string[];
19+
returnType?: string;
1920
};
2021

2122
const toParams = (parameters?: DocEntry[]): Params[] =>
@@ -208,6 +209,19 @@ const toMarkdown = ({
208209
headingLevel: MarkdownHeadingLevel | '####';
209210
docType: 'Constant' | 'Function' | 'Method' | 'Property' | 'Type' | 'Enum';
210211
} & Pick<MarkdownOptions, 'emoji'>): string => {
212+
const jsDocsToReturnType = (jsDocs: JSDocTagInfo[]): string => {
213+
const returns: JSDocTagInfo[] = jsDocs.filter(({name}: JSDocTagInfo) => name === 'returns');
214+
const texts: Array<SymbolDisplayPart[] | undefined> = returns.map(({text}) => text);
215+
const returnType = texts.reduce((acc: SymbolDisplayPart[][], values: SymbolDisplayPart[] | undefined) => {
216+
if (values === undefined) {
217+
return acc;
218+
}
219+
220+
return [...acc, values];
221+
}, []);
222+
223+
return returnType.map((parts) => parts.map(({text}) => text).join('')).join(' ');
224+
};
211225
const jsDocsToParams = (jsDocs: JSDocTagInfo[]): Params[] => {
212226
const params: JSDocTagInfo[] = jsDocs.filter(({name}: JSDocTagInfo) => name === 'param');
213227
const texts: Array<SymbolDisplayPart[] | undefined> = params.map(({text}) => text);
@@ -251,12 +265,13 @@ const toMarkdown = ({
251265
type: type ?? '',
252266
documentation: documentation ?? '',
253267
params: [...toParams(parameters), ...jsDocsToParams(jsDocs ?? [])],
268+
returnType: jsDocsToReturnType(jsDocs ?? []),
254269
examples: [...jsDocsToExamples(jsDocs ?? [])],
255270
url
256271
})
257272
);
258273

259-
const rowToMarkdown = ({name, documentation, type, params, examples, url}: Row): string => {
274+
const rowToMarkdown = ({name, documentation, type, params, returnType, examples, url}: Row): string => {
260275
const markdown: string[] = [
261276
`${headingLevel}# ${emoji === undefined || emoji === null ? '' : ':gear: '}${name}\n`
262277
];
@@ -276,6 +291,9 @@ const toMarkdown = ({
276291
markdown.push(...inlineParams(params));
277292
markdown.push('\n');
278293
}
294+
if (returnType) {
295+
markdown.push(`Returns:\n\t${returnType}\n`);
296+
}
279297
if (examples.length) {
280298
markdown.push('Examples:\n');
281299
markdown.push(...examples);

src/test/markdown.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('markdown', () => {
2121
expect(markdown).toEqual(expectedDoc);
2222
});
2323

24-
it.each([35, 118, 146])('should generate a markdown link to line %s', (line) => {
24+
it.each([35, 119, 147])('should generate a markdown link to line %s', (line) => {
2525
const doc = buildDocumentation({
2626
inputFiles: ['./src/test/mock.ts'],
2727
options: {

src/test/mock.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@
173173
"kind": "link"
174174
}
175175
]
176+
},
177+
{
178+
"name": "returns",
179+
"text": [
180+
{
181+
"text": "The balance of the specified account identifier",
182+
"kind": "text"
183+
}
184+
]
176185
}
177186
],
178187
"doc_type": "function"
@@ -551,4 +560,4 @@
551560
],
552561
"fileName": "src/test/mock.ts"
553562
}
554-
]
563+
]

src/test/mock.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ function submit() {
8787
```
8888

8989

90-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L238)
90+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L239)
9191

9292
### :gear: MyObject.someFunction
9393

9494
| Function | Type |
9595
| ---------- | ---------- |
9696
| `MyObject.someFunction` | `() => void` |
9797

98-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L281)
98+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L282)
9999

100100

101101
## :wrench: Constants
@@ -132,7 +132,7 @@ console.log(result.success); // true or false
132132
```
133133

134134

135-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L298)
135+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L299)
136136

137137

138138
## :factory: LedgerCanister
@@ -179,7 +179,10 @@ Returns the balance of the specified account identifier.
179179
| ---------- | ---------- |
180180
| `accountBalance` | `({ certified }: { certified?: boolean or undefined; }) => Promise<{ icp: bigint; }>` |
181181

182-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L69)
182+
Returns:
183+
The balance of the specified account identifier
184+
185+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L70)
183186

184187
#### :gear: shouldBeDocumented
185188

@@ -189,7 +192,7 @@ Public method.
189192
| ---------- | ---------- |
190193
| `shouldBeDocumented` | `() => void` |
191194

192-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L97)
195+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L98)
193196

194197
### Properties
195198

@@ -203,11 +206,11 @@ The documentation of the public property.
203206
| ---------- | ---------- |
204207
| `publicShouldBeDocumented` | `string` |
205208

206-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L80)
209+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L81)
207210

208211
## :factory: SnsLedgerCanister
209212

210-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L118)
213+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L119)
211214

212215
### Constructors
213216

@@ -227,7 +230,7 @@ This create function is public as well.
227230
| ---------- | ---------- |
228231
| `create` | `(options: { canisterId?: string or undefined; }) => SnsLedgerCanister` |
229232

230-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L133)
233+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L134)
231234

232235
### Methods
233236

@@ -241,11 +244,11 @@ The token metadata (name, symbol, etc.).
241244
| ---------- | ---------- |
242245
| `metadata` | `(params: QueryParams) => Promise<SnsTokenMetadataResponse>` |
243246

244-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L142)
247+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L143)
245248

246249
## :factory: default
247250

248-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L146)
251+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L147)
249252

250253
### Methods
251254

@@ -259,13 +262,13 @@ Description
259262
| ---------- | ---------- |
260263
| `bar` | `() => void` |
261264

262-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L150)
265+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L151)
263266

264267
## :factory: Number
265268

266269
Should differentiate methods / properties and static methods / properties
267270

268-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L315)
271+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L316)
269272

270273
### Static Methods
271274

@@ -277,7 +280,7 @@ Should differentiate methods / properties and static methods / properties
277280
| ---------- | ---------- |
278281
| `add` | `(n1: Number, n2: Number) => Number` |
279282

280-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L325)
283+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L326)
281284

282285
### Methods
283286

@@ -289,7 +292,7 @@ Should differentiate methods / properties and static methods / properties
289292
| ---------- | ---------- |
290293
| `add` | `(n: Number) => Number` |
291294

292-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L321)
295+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L322)
293296

294297
### Static Properties
295298

@@ -301,7 +304,7 @@ Should differentiate methods / properties and static methods / properties
301304
| ---------- | ---------- |
302305
| `world` | `string` |
303306

304-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L317)
307+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L318)
305308

306309
### Properties
307310

@@ -313,7 +316,7 @@ Should differentiate methods / properties and static methods / properties
313316
| ---------- | ---------- |
314317
| `hello` | `string` |
315318

316-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L316)
319+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L317)
317320

318321
## :nut_and_bolt: Enum
319322

@@ -383,7 +386,7 @@ A type yolo
383386
| ---------- | ---------- |
384387
| `yolo` | `'string'` |
385388

386-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L175)
389+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L176)
387390

388391
### :gear: Abc
389392

@@ -393,21 +396,21 @@ A type yolo
393396
| ---------- | ---------- |
394397
| `Abc` | `Foo and {hello: string}` |
395398

396-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L180)
399+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L181)
397400

398401
### :gear: StorageConfigSourceGlob
399402

400403
| Type | Type |
401404
| ---------- | ---------- |
402405
| `StorageConfigSourceGlob` | |
403406

404-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L251)
407+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L252)
405408

406409
### :gear: SatelliteConfig
407410

408411
| Type | Type |
409412
| ---------- | ---------- |
410413
| `SatelliteConfig` | `Either<SatelliteId, SatelliteIds> and CliConfig and SatelliteConfigOptions` |
411414

412-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L277)
415+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L278)
413416

src/test/mock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class LedgerCanister {
6565
* @param params.certified Update calls?
6666
*
6767
* @throws an {@link Error}
68+
* @returns {Promise<{icp: bigint}>} The balance of the specified account identifier
6869
*/
6970
public accountBalance = async ({
7071
certified = true

0 commit comments

Comments
 (0)