aboutsummaryrefslogtreecommitdiffstats
path: root/packages/tslint-config/rules/booleanNamingRule.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-05-23 00:04:50 +0800
committerGitHub <noreply@github.com>2018-05-23 00:04:50 +0800
commitcc840a6911850856c3254f7ac73cd70c527e1a5c (patch)
treec7435ac7ce09541a67f0106a85f391dc48921e10 /packages/tslint-config/rules/booleanNamingRule.ts
parenta26cc7c14d4e59102d5f84d534f75c42b8683cad (diff)
parentc4a2f700172e8be367225d90a1c51cef8cf0d7dd (diff)
downloaddexon-sol-tools-cc840a6911850856c3254f7ac73cd70c527e1a5c.tar.gz
dexon-sol-tools-cc840a6911850856c3254f7ac73cd70c527e1a5c.tar.zst
dexon-sol-tools-cc840a6911850856c3254f7ac73cd70c527e1a5c.zip
Merge pull request #594 from 0xProject/improvement/addCustomTslintRules
Add more tslint rules
Diffstat (limited to 'packages/tslint-config/rules/booleanNamingRule.ts')
-rw-r--r--packages/tslint-config/rules/booleanNamingRule.ts68
1 files changed, 68 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..f673afc6a
--- /dev/null
+++ b/packages/tslint-config/rules/booleanNamingRule.ts
@@ -0,0 +1,68 @@
+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);
+ 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);
+ }
+ }
+}