aboutsummaryrefslogtreecommitdiffstats
path: root/packages/tslint-config
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-05-16 20:58:01 +0800
committerFabio Berger <me@fabioberger.com>2018-05-16 20:58:01 +0800
commit45fa26dc6cf48d8b8baf123429f0b64ad0a431c9 (patch)
treeb78f23cee7c90514512daf6871d0cbeaec388cbf /packages/tslint-config
parentf49d432fdc6e0cbe5cca574159e7e2f857daa542 (diff)
downloaddexon-sol-tools-45fa26dc6cf48d8b8baf123429f0b64ad0a431c9.tar
dexon-sol-tools-45fa26dc6cf48d8b8baf123429f0b64ad0a431c9.tar.gz
dexon-sol-tools-45fa26dc6cf48d8b8baf123429f0b64ad0a431c9.tar.bz2
dexon-sol-tools-45fa26dc6cf48d8b8baf123429f0b64ad0a431c9.tar.lz
dexon-sol-tools-45fa26dc6cf48d8b8baf123429f0b64ad0a431c9.tar.xz
dexon-sol-tools-45fa26dc6cf48d8b8baf123429f0b64ad0a431c9.tar.zst
dexon-sol-tools-45fa26dc6cf48d8b8baf123429f0b64ad0a431c9.zip
Implement boolean variable naming custom TSLint rule
Diffstat (limited to 'packages/tslint-config')
-rw-r--r--packages/tslint-config/rules/booleanNamingRule.ts69
1 files changed, 69 insertions, 0 deletions
diff --git a/packages/tslint-config/rules/booleanNamingRule.ts b/packages/tslint-config/rules/booleanNamingRule.ts
new file mode 100644
index 000000000..bc0b42e71
--- /dev/null
+++ b/packages/tslint-config/rules/booleanNamingRule.ts
@@ -0,0 +1,69 @@
+import * as _ from 'lodash';
+import * as Lint from 'tslint';
+import * as ts from 'typescript';
+
+const VALID_BOOLEAN_PREFIXES = ['is', 'does', 'should', 'was', 'has', 'can', 'did', 'would'];
+
+export class Rule extends Lint.Rules.TypedRule {
+ public static FAILURE_STRING = `Boolean variable names should begin with: ${VALID_BOOLEAN_PREFIXES.join(', ')}`;
+
+ public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
+ return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
+ }
+}
+
+function walk(ctx: Lint.WalkContext<void>, tc: ts.TypeChecker): void {
+ traverse(ctx.sourceFile);
+
+ function traverse(node: ts.Node): void {
+ checkNodeForViolations(ctx, node, tc);
+ return ts.forEachChild(node, traverse);
+ }
+}
+
+function checkNodeForViolations(ctx: Lint.WalkContext<void>, node: ts.Node, tc: ts.TypeChecker): void {
+ switch (node.kind) {
+ // Handle: const { timestamp } = ...
+ case ts.SyntaxKind.BindingElement: {
+ const bindingElementNode = node as ts.BindingElement;
+ if (bindingElementNode.name.kind === ts.SyntaxKind.Identifier) {
+ handleBooleanNaming(bindingElementNode, tc, ctx);
+ }
+ break;
+ }
+
+ // Handle regular assignments: const block = ...
+ case ts.SyntaxKind.VariableDeclaration:
+ const variableDeclarationNode = node as ts.VariableDeclaration;
+ if (variableDeclarationNode.name.kind === ts.SyntaxKind.Identifier) {
+ handleBooleanNaming(node as ts.VariableDeclaration, tc, ctx);
+ }
+ break;
+
+ default:
+ // noop
+ }
+}
+
+function handleBooleanNaming(
+ node: ts.VariableDeclaration | ts.BindingElement,
+ tc: ts.TypeChecker,
+ ctx: Lint.WalkContext<void>,
+): void {
+ const nodeName = node.name;
+ const variableName = nodeName.getText();
+ const lowercasedName = _.toLower(variableName);
+ // tslint:disable-next-line
+ const typeNode = tc.getTypeAtLocation(node);
+ const typeName = (typeNode as any).intrinsicName;
+ if (typeName === 'boolean') {
+ const hasProperName = !_.isUndefined(
+ _.find(VALID_BOOLEAN_PREFIXES, prefix => {
+ return _.startsWith(lowercasedName, prefix);
+ }),
+ );
+ if (!hasProperName) {
+ ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
+ }
+ }
+}