Skip to content

Commit 1512721

Browse files
devversionjelbourn
authored andcommitted
build: set @returns description for void async methods automatically (#17751)
Automatically sets the `@returns` tag description for asynchronous methods which do not return any value. This is a proposal based on the commit in: #17723 (comment)
1 parent b53e092 commit 1512721

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

tools/dgeni/docs-package.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Package} from 'dgeni';
22
import {Host} from 'dgeni-packages/typescript/services/ts-host/host';
33
import {HighlightNunjucksExtension} from './nunjucks-tags/highlight';
44
import {patchLogService} from './patch-log-service';
5+
import {AsyncReturnDescriptionProcessor} from './processors/async-return-description';
56
import {DocsPrivateFilter} from './processors/docs-private-filter';
67
import {Categorizer} from './processors/categorizer';
78
import {FilterDuplicateExports} from './processors/filter-duplicate-exports';
@@ -49,14 +50,17 @@ apiDocsPackage.processor(new Categorizer());
4950
// Processor to group docs into top-level entry-points such as "tabs", "sidenav", etc.
5051
apiDocsPackage.processor(new EntryPointGrouper());
5152

53+
// Processor that automatically adds a return description for async methods.
54+
apiDocsPackage.processor(new AsyncReturnDescriptionProcessor());
55+
5256
// Configure the log level of the API docs dgeni package.
5357
apiDocsPackage.config(function(log: any) {
5458
return log.level = 'warning';
5559
});
5660

5761
// Configure the processor for reading files from the file system.
5862
apiDocsPackage.config(function(readFilesProcessor: any) {
59-
// Disable we currently only use the "readTypeScriptModules" processor
63+
// Disabled since we only use the "readTypeScriptModules" processor
6064
readFilesProcessor.$enabled = false;
6165
});
6266

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {DocCollection, Processor} from 'dgeni';
2+
import {ApiDoc} from 'dgeni-packages/typescript/api-doc-types/ApiDoc';
3+
import {FunctionExportDoc} from 'dgeni-packages/typescript/api-doc-types/FunctionExportDoc';
4+
import {MethodMemberDoc} from 'dgeni-packages/typescript/api-doc-types/MethodMemberDoc';
5+
import * as ts from 'typescript';
6+
7+
/** Type describing a function-like API doc (i.e. a function, or a class method member). */
8+
type FunctionLikeDoc = (FunctionExportDoc|MethodMemberDoc) & {returns?: {description: string}};
9+
10+
/**
11+
* Processor that automatically sets the @return description for
12+
* asynchronous methods which do not return any value.
13+
*/
14+
export class AsyncReturnDescriptionProcessor implements Processor {
15+
name = 'async-return-description';
16+
$runBefore = ['categorizer'];
17+
18+
$process(docs: DocCollection) {
19+
docs.forEach((doc: ApiDoc) => {
20+
if (!isFunctionLikeDoc(doc)) {
21+
return;
22+
}
23+
const typeString = getTypeOfFunctionLikeDoc(doc);
24+
if (!doc.returns && typeString === 'Promise<void>') {
25+
doc.returns = {description: 'Promise that resolves when the action completes.'};
26+
}
27+
});
28+
}
29+
}
30+
31+
/**
32+
* Gets the type of the function-like doc. If no explicit type has been specified,
33+
* the type checker is used to compute a type string based on the function body.
34+
*/
35+
function getTypeOfFunctionLikeDoc(doc: FunctionLikeDoc): string|null {
36+
if (doc.type) {
37+
return doc.type;
38+
}
39+
40+
const decl = doc.declaration as ts.MethodDeclaration|ts.FunctionDeclaration;
41+
const signature = doc.typeChecker.getSignatureFromDeclaration(decl);
42+
43+
if (!signature) {
44+
return null;
45+
}
46+
47+
const returnType = doc.typeChecker.getReturnTypeOfSignature(signature);
48+
return doc.typeChecker.typeToString(returnType);
49+
}
50+
51+
/** Whether the given API doc is a function-like doc. */
52+
function isFunctionLikeDoc(doc: ApiDoc): doc is FunctionLikeDoc {
53+
return doc instanceof FunctionExportDoc || doc instanceof MethodMemberDoc;
54+
}

tools/dgeni/processors/categorizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {sortCategorizedMethodMembers, sortCategorizedPropertyMembers} from '../c
3636
*/
3737
export class Categorizer implements Processor {
3838
name = 'categorizer';
39-
$runBefore = ['docs-processed'];
39+
$runBefore = ['docs-processed', 'entryPointGrouper'];
4040

4141
$process(docs: DocCollection) {
4242
docs.filter(doc => doc.docType === 'class' || doc.docType === 'interface')

0 commit comments

Comments
 (0)