Implement first custom linter rule async-suffix

This commit is contained in:
Leonid Logvinov
2017-12-14 21:49:19 +01:00
parent a0aa21103b
commit c23d42fea5
8 changed files with 61 additions and 4 deletions

View File

@@ -20,7 +20,4 @@ export class BlockchainLifecycle {
throw new Error(`Snapshot with id #${snapshotId} failed to revert`);
}
}
public async mineABlock(): Promise<void> {
await this.rpc.mineBlockAsync();
}
}

View File

@@ -5,6 +5,7 @@ declare module 'es6-promisify';
// tslint:disable:max-classes-per-file
// tslint:disable:class-name
// tslint:disable:async-suffix
// tslint:disable:completed-docs
// Ethereumjs-tx declarations

View File

@@ -33,6 +33,7 @@ export class RedundantRPCSubprovider extends Subprovider {
});
});
}
// tslint:disable-next-line:async-suffix
public async handleRequest(payload: JSONRPCPayload, next: () => void,
end: (err: Error|null, data?: any) => void): Promise<void> {
const rpcsCopy = this.rpcs.slice();

View File

@@ -3,6 +3,11 @@
"version": "0.2.1",
"description": "Lint rules related to 0xProject for TSLint",
"main": "tslint.json",
"scripts": {
"build": "tsc",
"clean": "shx rm -rf lib",
"lint": "tslint --project . 'rules/**/*.ts'"
},
"files": [
"tslint.js",
"README.md",
@@ -29,10 +34,13 @@
},
"homepage": "https://github.com/0xProject/0x.js/packages/tslint-config/README.md",
"devDependencies": {
"@types/lodash": "^4.14.86",
"shx": "^0.2.2",
"tslint": "5.8.0",
"typescript": "~2.6.1"
},
"dependencies": {
"lodash": "^4.17.4",
"tslint-react": "^3.2.0"
}
}

View File

@@ -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()));
}
}

View 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);
}
}

View File

@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": [ "es2017", "dom"],
"outDir": "lib",
"sourceMap": true,
"declaration": true,
"noImplicitAny": true,
"strictNullChecks": true
},
"include": [
"./rules/**/*"
]
}

View File

@@ -7,6 +7,7 @@
"adjacent-overload-signatures": true,
"arrow-parens": [true, "ban-single-arg-parens"],
"arrow-return-shorthand": true,
"async-suffix": true,
"await-promise": true,
"binary-expression-operand-order": true,
"callable-types": true,
@@ -101,5 +102,6 @@
"jsx-self-close": true,
"jsx-wrap-multiline": false,
"jsx-no-bind": false
}
},
"rulesDirectory": "lib"
}