From c23d42fea5ca817a5cfb4f7b03ae723cb607f887 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 14 Dec 2017 21:49:19 +0100 Subject: Implement first custom linter rule async-suffix --- packages/dev-utils/src/blockchain_lifecycle.ts | 3 --- packages/subproviders/src/globals.d.ts | 1 + .../subproviders/src/subproviders/redundant_rpc.ts | 1 + packages/tslint-config/package.json | 8 ++++++++ packages/tslint-config/rules/asyncSuffixRule.ts | 10 ++++++++++ .../tslint-config/rules/walkers/async_suffix.ts | 23 ++++++++++++++++++++++ packages/tslint-config/tsconfig.json | 15 ++++++++++++++ packages/tslint-config/tslint.json | 4 +++- 8 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 packages/tslint-config/rules/asyncSuffixRule.ts create mode 100644 packages/tslint-config/rules/walkers/async_suffix.ts create mode 100644 packages/tslint-config/tsconfig.json (limited to 'packages') diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 19c5ac184..0e493eb48 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -20,7 +20,4 @@ export class BlockchainLifecycle { throw new Error(`Snapshot with id #${snapshotId} failed to revert`); } } - public async mineABlock(): Promise { - await this.rpc.mineBlockAsync(); - } } diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts index 400bbef5b..adef23806 100644 --- a/packages/subproviders/src/globals.d.ts +++ b/packages/subproviders/src/globals.d.ts @@ -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 diff --git a/packages/subproviders/src/subproviders/redundant_rpc.ts b/packages/subproviders/src/subproviders/redundant_rpc.ts index 80462bbfb..f688061ea 100644 --- a/packages/subproviders/src/subproviders/redundant_rpc.ts +++ b/packages/subproviders/src/subproviders/redundant_rpc.ts @@ -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 { const rpcsCopy = this.rpcs.slice(); diff --git a/packages/tslint-config/package.json b/packages/tslint-config/package.json index a4b5d4466..3b320e774 100644 --- a/packages/tslint-config/package.json +++ b/packages/tslint-config/package.json @@ -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" } } 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); + } +} diff --git a/packages/tslint-config/tsconfig.json b/packages/tslint-config/tsconfig.json new file mode 100644 index 000000000..7661a9d07 --- /dev/null +++ b/packages/tslint-config/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "lib": [ "es2017", "dom"], + "outDir": "lib", + "sourceMap": true, + "declaration": true, + "noImplicitAny": true, + "strictNullChecks": true + }, + "include": [ + "./rules/**/*" + ] +} diff --git a/packages/tslint-config/tslint.json b/packages/tslint-config/tslint.json index 0921a3954..d3ee51a63 100644 --- a/packages/tslint-config/tslint.json +++ b/packages/tslint-config/tslint.json @@ -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" } -- cgit v1.2.3