Implement first custom linter rule async-suffix
This commit is contained in:
23
packages/tslint-config/rules/walkers/async_suffix.ts
Normal file
23
packages/tslint-config/rules/walkers/async_suffix.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user