diff options
author | Fabio Berger <me@fabioberger.com> | 2018-05-16 20:57:28 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-05-16 20:57:28 +0800 |
commit | f49d432fdc6e0cbe5cca574159e7e2f857daa542 (patch) | |
tree | c7a31800aa249919825fd1896035448be093b7dc /packages | |
parent | 1a31ceff8ef021d1795fb1d95613340872321504 (diff) | |
download | dexon-sol-tools-f49d432fdc6e0cbe5cca574159e7e2f857daa542.tar dexon-sol-tools-f49d432fdc6e0cbe5cca574159e7e2f857daa542.tar.gz dexon-sol-tools-f49d432fdc6e0cbe5cca574159e7e2f857daa542.tar.bz2 dexon-sol-tools-f49d432fdc6e0cbe5cca574159e7e2f857daa542.tar.lz dexon-sol-tools-f49d432fdc6e0cbe5cca574159e7e2f857daa542.tar.xz dexon-sol-tools-f49d432fdc6e0cbe5cca574159e7e2f857daa542.tar.zst dexon-sol-tools-f49d432fdc6e0cbe5cca574159e7e2f857daa542.zip |
Implement custom no-magic-numbers rule that doesn't include magic numbers passed into BigNumber instantiations (e.g const amount = new BigNumber(5); )
Diffstat (limited to 'packages')
-rw-r--r-- | packages/tslint-config/package.json | 3 | ||||
-rw-r--r-- | packages/tslint-config/rules/customNoMagicNumbersRule.ts | 76 |
2 files changed, 78 insertions, 1 deletions
diff --git a/packages/tslint-config/package.json b/packages/tslint-config/package.json index 04e1ba2b6..33132210c 100644 --- a/packages/tslint-config/package.json +++ b/packages/tslint-config/package.json @@ -41,7 +41,8 @@ "lodash": "^4.17.4", "tslint": "5.8.0", "tslint-eslint-rules": "^4.1.1", - "tslint-react": "^3.2.0" + "tslint-react": "^3.2.0", + "tsutils": "^2.12.1" }, "publishConfig": { "access": "public" diff --git a/packages/tslint-config/rules/customNoMagicNumbersRule.ts b/packages/tslint-config/rules/customNoMagicNumbersRule.ts new file mode 100644 index 000000000..e358221eb --- /dev/null +++ b/packages/tslint-config/rules/customNoMagicNumbersRule.ts @@ -0,0 +1,76 @@ +import * as Lint from 'tslint'; +import { isPrefixUnaryExpression } from 'tsutils'; +import * as ts from 'typescript'; + +/** + * A modified version of the no-magic-numbers rule that allows for magic numbers + * when instantiating a BigNumber instance. + * E.g We want to be able to write: + * const amount = new BigNumber(5); + * Original source: https://github.com/palantir/tslint/blob/42b058a6baa688f8be8558b277eb056c3ff79818/src/rules/noMagicNumbersRule.ts + */ +export class Rule extends Lint.Rules.AbstractRule { + public static ALLOWED_NODES = new Set<ts.SyntaxKind>([ + ts.SyntaxKind.ExportAssignment, + ts.SyntaxKind.FirstAssignment, + ts.SyntaxKind.LastAssignment, + ts.SyntaxKind.PropertyAssignment, + ts.SyntaxKind.ShorthandPropertyAssignment, + ts.SyntaxKind.VariableDeclaration, + ts.SyntaxKind.VariableDeclarationList, + ts.SyntaxKind.EnumMember, + ts.SyntaxKind.PropertyDeclaration, + ts.SyntaxKind.Parameter, + ]); + + public static DEFAULT_ALLOWED = [-1, 0, 1]; + + public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { + const allowedNumbers = this.ruleArguments.length > 0 ? this.ruleArguments : Rule.DEFAULT_ALLOWED; + return this.applyWithWalker( + new CustomNoMagicNumbersWalker(sourceFile, this.ruleName, new Set(allowedNumbers.map(String))), + ); + } +} + +// tslint:disable-next-line:max-classes-per-file +class CustomNoMagicNumbersWalker extends Lint.AbstractWalker<Set<string>> { + public static FAILURE_STRING = "'magic numbers' are not allowed"; + private static _isNegativeNumberLiteral( + node: ts.Node, + ): node is ts.PrefixUnaryExpression & { operand: ts.NumericLiteral } { + return ( + isPrefixUnaryExpression(node) && + node.operator === ts.SyntaxKind.MinusToken && + node.operand.kind === ts.SyntaxKind.NumericLiteral + ); + } + public walk(sourceFile: ts.SourceFile): void { + const cb = (node: ts.Node): void => { + if (node.kind === ts.SyntaxKind.NumericLiteral) { + return this.checkNumericLiteral(node, (node as ts.NumericLiteral).text); + } + if (CustomNoMagicNumbersWalker._isNegativeNumberLiteral(node)) { + return this.checkNumericLiteral(node, `-${(node.operand as ts.NumericLiteral).text}`); + } + return ts.forEachChild(node, cb); + }; + return ts.forEachChild(sourceFile, cb); + } + + // tslint:disable:no-non-null-assertion + // tslint:disable-next-line:underscore-private-and-protected + private checkNumericLiteral(node: ts.Node, num: string): void { + if (!Rule.ALLOWED_NODES.has(node.parent!.kind) && !this.options.has(num)) { + if (node.parent!.kind === ts.SyntaxKind.NewExpression) { + const className = (node.parent! as any).expression.escapedText; + const BIG_NUMBER_NEW_EXPRESSION = 'BigNumber'; + if (className === BIG_NUMBER_NEW_EXPRESSION) { + return; // noop + } + } + this.addFailureAtNode(node, CustomNoMagicNumbersWalker.FAILURE_STRING); + } + } + // tslint:enable:no-non-null-assertion +} |