aboutsummaryrefslogtreecommitdiffstats
path: root/packages/tslint-config/rules/walkers/async_suffix.ts
blob: eaec9c5f67528df1a8e824dd652615304277ab64 (plain) (blame)
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
import * as _ from 'lodash';
import * as Lint from 'tslint';
import * as ts from 'typescript';

export class AsyncSuffixWalker extends Lint.RuleWalker {
    public static FAILURE_STRING = 'async functions must have an Async suffix';
    public visitMethodDeclaration(node: ts.MethodDeclaration): void {
        const methodNameNode = node.name;
        const methodName = methodNameNode.getText();
        if (!_.isUndefined(node.type)) {
            if (node.type.kind === ts.SyntaxKind.TypeReference) {
                // tslint:disable-next-line:no-unnecessary-type-assertion
                const returnTypeName = (node.type as ts.TypeReferenceNode).typeName.getText();
                if (returnTypeName === 'Promise' && !methodName.endsWith('Async')) {
                    const failure = this.createFailure(
                        methodNameNode.getStart(),
                        methodNameNode.getWidth(),
                        AsyncSuffixWalker.FAILURE_STRING,
                    );
                    this.addFailure(failure);
                }
            }
        }
        super.visitMethodDeclaration(node);
    }
}