diff options
Diffstat (limited to 'packages/tslint-config/rules')
6 files changed, 0 insertions, 314 deletions
diff --git a/packages/tslint-config/rules/asyncSuffixRule.ts b/packages/tslint-config/rules/asyncSuffixRule.ts deleted file mode 100644 index 5215c7151..000000000 --- a/packages/tslint-config/rules/asyncSuffixRule.ts +++ /dev/null @@ -1,10 +0,0 @@ -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/booleanNamingRule.ts b/packages/tslint-config/rules/booleanNamingRule.ts deleted file mode 100644 index 6590f689b..000000000 --- a/packages/tslint-config/rules/booleanNamingRule.ts +++ /dev/null @@ -1,69 +0,0 @@ -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', 'are']; -// tslint:disable:no-unnecessary-type-assertion -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); - } - } -} -// tslint:enable:no-unnecessary-type-assertion diff --git a/packages/tslint-config/rules/customNoMagicNumbersRule.ts b/packages/tslint-config/rules/customNoMagicNumbersRule.ts deleted file mode 100644 index cb124f738..000000000 --- a/packages/tslint-config/rules/customNoMagicNumbersRule.ts +++ /dev/null @@ -1,79 +0,0 @@ -import * as Lint from 'tslint'; -import { isPrefixUnaryExpression } from 'tsutils'; -import * as ts from 'typescript'; - -// tslint:disable:no-unnecessary-type-assertion -/** - * 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( - // tslint:disable-next-line:no-inferred-empty-object-type - 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 -} -// tslint:enable:no-unnecessary-type-assertion diff --git a/packages/tslint-config/rules/enumNamingRule.ts b/packages/tslint-config/rules/enumNamingRule.ts deleted file mode 100644 index 56499618f..000000000 --- a/packages/tslint-config/rules/enumNamingRule.ts +++ /dev/null @@ -1,60 +0,0 @@ -import * as Lint from 'tslint'; -import * as ts from 'typescript'; - -export class Rule extends Lint.Rules.AbstractRule { - public static FAILURE_STRING = `Enum member names should be PascalCase`; - - public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - return this.applyWithFunction(sourceFile, walk); - } -} - -function walk(ctx: Lint.WalkContext<void>): void { - // Recursively walk the AST starting with root node, `ctx.sourceFile`. - // Call the function `cb` (defined below) for each child. - return ts.forEachChild(ctx.sourceFile, cb); - - function cb(node: ts.Node): void { - if (node.kind === ts.SyntaxKind.EnumMember) { - const keyNode = node.getFirstToken(ctx.sourceFile); - if (keyNode !== undefined) { - const keyText = keyNode.getText(ctx.sourceFile); - if (!isPascalCase(keyText)) { - return ctx.addFailureAtNode(node, Rule.FAILURE_STRING, getFix(keyText, node)); - } - } - } - // Continue recursion into the AST by calling function `cb` for every child of the current node. - return ts.forEachChild(node, cb); - } - - function getFix(text: string, node: ts.Node): Lint.Replacement { - let fix = toPascalCase(text); - // check for `member = value` - if (node.getChildCount(ctx.sourceFile) === 3) { - const value = node.getLastToken(ctx.sourceFile); - if (value !== undefined) { - fix += ` = ${value.getText(ctx.sourceFile)}`; - } - } - return new Lint.Replacement(node.getStart(ctx.sourceFile), node.getWidth(ctx.sourceFile), fix); - } -} - -// Modified from: https://github.com/jonschlinkert/pascalcase/ -function toPascalCase(str: string): string { - let result = str.replace(/([a-z0-9\W])([A-Z])/g, '$1 $2'); - if (result.length === 1) { - return result.toUpperCase(); - } - result = result.replace(/^[\W_\.]+|[\W_\.]+$/g, '').toLowerCase(); - result = result.charAt(0).toUpperCase() + result.slice(1); - return result.replace(/[\W_\.]+(\w|$)/g, (_, ch) => { - return ch.toUpperCase(); - }); -} -function isPascalCase(s: string): boolean { - const regex = /^([A-Z0-9]+[a-z0-9]+)+$/g; - const key = s.split('=')[0].trim(); - return regex.test(key); -} diff --git a/packages/tslint-config/rules/underscorePrivateAndProtectedRule.ts b/packages/tslint-config/rules/underscorePrivateAndProtectedRule.ts deleted file mode 100644 index 8c039bea7..000000000 --- a/packages/tslint-config/rules/underscorePrivateAndProtectedRule.ts +++ /dev/null @@ -1,61 +0,0 @@ -import * as Lint from 'tslint'; -import * as ts from 'typescript'; - -const UNDERSCORE = '_'; - -type RelevantClassMember = - | ts.MethodDeclaration - | ts.PropertyDeclaration - | ts.GetAccessorDeclaration - | ts.SetAccessorDeclaration; - -// Copied from: https://github.com/DanielRosenwasser/underscore-privates-tslint-rule -// The version on github is not published on npm -export class Rule extends Lint.Rules.AbstractRule { - public static FAILURE_STRING = 'private and protected members must be prefixed with an underscore'; - - public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - return this.applyWithFunction(sourceFile, walk); - } -} -function walk(ctx: Lint.WalkContext<void>): void { - traverse(ctx.sourceFile); - - function traverse(node: ts.Node): void { - checkNodeForViolations(ctx, node); - return ts.forEachChild(node, traverse); - } -} -function checkNodeForViolations(ctx: Lint.WalkContext<void>, node: ts.Node): void { - if (!isRelevantClassMember(node)) { - return; - } - // The declaration might have a computed property name or a numeric name. - const name = node.name; - if (!nameIsIdentifier(name)) { - return; - } - if (!nameStartsWithUnderscore(name.text) && memberIsPrivate(node)) { - ctx.addFailureAtNode(name, Rule.FAILURE_STRING); - } -} -function isRelevantClassMember(node: ts.Node): node is RelevantClassMember { - switch (node.kind) { - case ts.SyntaxKind.MethodDeclaration: - case ts.SyntaxKind.PropertyDeclaration: - case ts.SyntaxKind.GetAccessor: - case ts.SyntaxKind.SetAccessor: - return true; - default: - return false; - } -} -function nameStartsWithUnderscore(text: string): boolean { - return text.charCodeAt(0) === UNDERSCORE.charCodeAt(0); -} -function memberIsPrivate(node: ts.Declaration): boolean { - return Lint.hasModifier(node.modifiers, ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword); -} -function nameIsIdentifier(node: ts.Node): node is ts.Identifier { - return node.kind === ts.SyntaxKind.Identifier; -} diff --git a/packages/tslint-config/rules/walkers/async_suffix.ts b/packages/tslint-config/rules/walkers/async_suffix.ts deleted file mode 100644 index 4e12152e8..000000000 --- a/packages/tslint-config/rules/walkers/async_suffix.ts +++ /dev/null @@ -1,35 +0,0 @@ -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/methods must have an Async suffix'; - public visitFunctionDeclaration(node: ts.FunctionDeclaration): void { - this._visitFunctionOrMethodDeclaration(node); - super.visitFunctionDeclaration(node); - } - public visitMethodDeclaration(node: ts.MethodDeclaration): void { - this._visitFunctionOrMethodDeclaration(node); - super.visitMethodDeclaration(node); - } - private _visitFunctionOrMethodDeclaration(node: ts.MethodDeclaration | ts.FunctionDeclaration): void { - const nameNode = node.name; - if (!_.isUndefined(nameNode)) { - const name = nameNode.getText(); - if (!_.isUndefined(node.type)) { - if (node.type.kind === ts.SyntaxKind.TypeReference) { - // tslint:disable-next-line:no-unnecessary-type-assertion - const returnTypeName = (node.type as ts.TypeReferenceNode).typeName.getText(); - if (returnTypeName === 'Promise' && !name.endsWith('Async')) { - const failure = this.createFailure( - nameNode.getStart(), - nameNode.getWidth(), - AsyncSuffixWalker.FAILURE_STRING, - ); - this.addFailure(failure); - } - } - } - } - } -} |