aboutsummaryrefslogtreecommitdiffstats
path: root/packages/tslint-config/rules
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-12-15 04:49:19 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-12-15 04:49:19 +0800
commitc23d42fea5ca817a5cfb4f7b03ae723cb607f887 (patch)
tree14a3a89988f2eb283f0c80ca9d70e3a20296b98f /packages/tslint-config/rules
parenta0aa21103b51ad287de1579832a4a490ca90175a (diff)
downloaddexon-sol-tools-c23d42fea5ca817a5cfb4f7b03ae723cb607f887.tar
dexon-sol-tools-c23d42fea5ca817a5cfb4f7b03ae723cb607f887.tar.gz
dexon-sol-tools-c23d42fea5ca817a5cfb4f7b03ae723cb607f887.tar.bz2
dexon-sol-tools-c23d42fea5ca817a5cfb4f7b03ae723cb607f887.tar.lz
dexon-sol-tools-c23d42fea5ca817a5cfb4f7b03ae723cb607f887.tar.xz
dexon-sol-tools-c23d42fea5ca817a5cfb4f7b03ae723cb607f887.tar.zst
dexon-sol-tools-c23d42fea5ca817a5cfb4f7b03ae723cb607f887.zip
Implement first custom linter rule async-suffix
Diffstat (limited to 'packages/tslint-config/rules')
-rw-r--r--packages/tslint-config/rules/asyncSuffixRule.ts10
-rw-r--r--packages/tslint-config/rules/walkers/async_suffix.ts23
2 files changed, 33 insertions, 0 deletions
diff --git a/packages/tslint-config/rules/asyncSuffixRule.ts b/packages/tslint-config/rules/asyncSuffixRule.ts
new file mode 100644
index 000000000..c6ae5189c
--- /dev/null
+++ b/packages/tslint-config/rules/asyncSuffixRule.ts
@@ -0,0 +1,10 @@
+import * as Lint from 'tslint';
+import * as ts from 'typescript';
+
+import {AsyncSuffixWalker} from './walkers/async_suffix';
+
+export class Rule extends Lint.Rules.AbstractRule {
+ public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
+ return this.applyWithWalker(new AsyncSuffixWalker(sourceFile, this.getOptions()));
+ }
+}
diff --git a/packages/tslint-config/rules/walkers/async_suffix.ts b/packages/tslint-config/rules/walkers/async_suffix.ts
new file mode 100644
index 000000000..0c89bd99f
--- /dev/null
+++ b/packages/tslint-config/rules/walkers/async_suffix.ts
@@ -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);
+ }
+}