Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Enabled the no-any rule. (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
reduckted authored and Josh Goldberg committed Oct 23, 2018
1 parent 1736651 commit 31529b2
Show file tree
Hide file tree
Showing 43 changed files with 231 additions and 174 deletions.
17 changes: 9 additions & 8 deletions src/exportNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ export class Rule extends Lint.Rules.AbstractRule {
return options.ruleArguments[0];
}
if (options instanceof Array) {
return typeof options[0] === 'object' ?
options[0].allow :
<string[]><any>options; // MSE version of tslint somehow requires this
return typeof options[0] === 'object' ? options[0].allow : options;
}
return undefined;
}
Expand Down Expand Up @@ -147,16 +145,19 @@ export class ExportNameWalker extends ErrorTolerantWalker {
private validateExportedElements(exportedElements: ts.Statement[]): void {
// only validate the exported elements when a single export statement is made
if (exportedElements.length === 1) {
if (exportedElements[0].kind === ts.SyntaxKind.ModuleDeclaration ||
exportedElements[0].kind === ts.SyntaxKind.ClassDeclaration ||
exportedElements[0].kind === ts.SyntaxKind.FunctionDeclaration) {
this.validateExport((<any>exportedElements[0]).name.text, exportedElements[0]);
const element = exportedElements[0];
if (ts.isModuleDeclaration(element) ||
ts.isClassDeclaration(element) ||
ts.isFunctionDeclaration(element)) {
if (element.name !== undefined) {
this.validateExport(element.name!.text, exportedElements[0]);
}
} else if (exportedElements[0].kind === ts.SyntaxKind.VariableStatement) {
const variableStatement: ts.VariableStatement = <ts.VariableStatement>exportedElements[0];
// ignore comma separated variable lists
if (variableStatement.declarationList.declarations.length === 1) {
const variableDeclaration: ts.VariableDeclaration = variableStatement.declarationList.declarations[0];
this.validateExport((<any>variableDeclaration.name).text, variableDeclaration);
this.validateExport(variableDeclaration.name.getText(), variableDeclaration);
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/functionNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Lint from 'tslint';
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker';
import {AstUtils} from './utils/AstUtils';
import {ExtendedMetadata} from './utils/ExtendedMetadata';
import { isObject } from './utils/TypeGuard';

const METHOD_REGEX = 'method-regex';
const PRIVATE_METHOD_REGEX = 'private-method-regex';
Expand All @@ -17,14 +18,14 @@ const VALIDATE_PRIVATE_STATICS_AS_EITHER = 'validate-private-statics-as-either';

const VALID_ARGS = [VALIDATE_PRIVATE_STATICS_AS_PRIVATE, VALIDATE_PRIVATE_STATICS_AS_STATIC, VALIDATE_PRIVATE_STATICS_AS_EITHER];

function parseOptions(ruleArguments: any[]): Options {
function parseOptions(ruleArguments: unknown[]): Options {

if (ruleArguments.length === 0) {
return {
validateStatics: VALIDATE_PRIVATE_STATICS_AS_PRIVATE
};
}
const staticsValidateOption: string = ruleArguments[1];
const staticsValidateOption: string = <string>ruleArguments[1];
if (VALID_ARGS.indexOf(staticsValidateOption) > -1) {
return {
validateStatics: staticsValidateOption
Expand Down Expand Up @@ -100,8 +101,8 @@ class FunctionNameRuleWalker extends ErrorTolerantWalker {
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
this.args = parseOptions(options.ruleArguments);
this.getOptions().forEach((opt: any) => {
if (typeof(opt) === 'object') {
this.getOptions().forEach((opt: unknown) => {
if (isObject(opt)) {
this.methodRegex = this.getOptionOrDefault(opt, METHOD_REGEX, this.methodRegex);
this.privateMethodRegex = this.getOptionOrDefault(opt, PRIVATE_METHOD_REGEX, this.privateMethodRegex);
this.protectedMethodRegex = this.getOptionOrDefault(opt, PROTECTED_METHOD_REGEX, this.protectedMethodRegex);
Expand Down Expand Up @@ -153,10 +154,11 @@ class FunctionNameRuleWalker extends ErrorTolerantWalker {
super.visitFunctionDeclaration(node);
}

private getOptionOrDefault(option: any, key: string, defaultValue: RegExp): RegExp {
private getOptionOrDefault(option: {[key: string]: unknown}, key: string, defaultValue: RegExp): RegExp {
try {
if (option[key] !== undefined) {
return new RegExp(option[key]);
const value = option[key];
if (value !== undefined && (typeof value === 'string' || value instanceof RegExp)) {
return new RegExp(value);
}
} catch (e) {
/* tslint:disable:no-console */
Expand Down
7 changes: 4 additions & 3 deletions src/importNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Lint from 'tslint';
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker';
import {Utils} from './utils/Utils';
import {ExtendedMetadata} from './utils/ExtendedMetadata';
import { isObject } from './utils/TypeGuard';

/**
* Implementation of the import-name rule.
Expand Down Expand Up @@ -42,10 +43,10 @@ class ImportNameRuleWalker extends ErrorTolerantWalker {

private extractOptions(): { [index: string]: string; } {
const result : { [index: string]: string; } = {};
this.getOptions().forEach((opt: any) => {
if (typeof(opt) === 'object') {
this.getOptions().forEach((opt: unknown) => {
if (isObject(opt)) {
Object.keys(opt).forEach((key: string): void => {
const value: any = opt[key];
const value: unknown = opt[key];
if (typeof value === 'string') {
result[key] = value;
}
Expand Down
27 changes: 14 additions & 13 deletions src/maxFuncBodyLengthRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {AstUtils} from './utils/AstUtils';
import {Utils} from './utils/Utils';
import {ExtendedMetadata} from './utils/ExtendedMetadata';
import {forEachTokenWithTrivia} from 'tsutils';
import { isObject } from './utils/TypeGuard';

/**
* Implementation of the max-func-body-length rule.
Expand Down Expand Up @@ -148,20 +149,20 @@ class MaxFunctionBodyLengthRuleWalker extends Lint.RuleWalker {
}

private parseOptions () {
this.getOptions().forEach((opt: any) => {
this.getOptions().forEach((opt: unknown) => {
if (typeof(opt) === 'number') {
this.maxBodyLength = opt;
return;
}

if (typeof(opt) === 'object') {
this.maxFuncBodyLength = opt[FUNC_BODY_LENGTH];
this.maxFuncExpressionBodyLength = opt[FUNC_EXPRESSION_BODY_LENGTH];
this.maxArrowBodyLength = opt[ARROW_BODY_LENGTH];
this.maxMethodBodyLength = opt[METHOD_BODY_LENGTH];
this.maxCtorBodyLength = opt[CTOR_BODY_LENGTH];
this.ignoreComments = opt[IGNORE_COMMENTS];
const regex: string = opt[IGNORE_PARAMETERS_TO_FUNCTION];
if (isObject(opt)) {
this.maxFuncBodyLength = <number>opt[FUNC_BODY_LENGTH];
this.maxFuncExpressionBodyLength = <number>opt[FUNC_EXPRESSION_BODY_LENGTH];
this.maxArrowBodyLength = <number>opt[ARROW_BODY_LENGTH];
this.maxMethodBodyLength = <number>opt[METHOD_BODY_LENGTH];
this.maxCtorBodyLength = <number>opt[CTOR_BODY_LENGTH];
this.ignoreComments = !!opt[IGNORE_COMMENTS];
const regex: string = <string>opt[IGNORE_PARAMETERS_TO_FUNCTION];
if (regex) {
this.ignoreParametersToFunctionRegex = new RegExp(regex);
}
Expand All @@ -182,10 +183,10 @@ class MaxFunctionBodyLengthRuleWalker extends Lint.RuleWalker {

private formatPlaceText (node: ts.FunctionLikeDeclaration) {
const funcTypeText = this.getFuncTypeText(node.kind);
if (node.kind === ts.SyntaxKind.MethodDeclaration ||
node.kind === ts.SyntaxKind.FunctionDeclaration ||
node.kind === ts.SyntaxKind.FunctionExpression) {
return ` in ${ funcTypeText } ${ (<any>node.name || {text: ''}).text }()`;
if (ts.isMethodDeclaration(node) ||
ts.isFunctionDeclaration(node) ||
ts.isFunctionExpression(node)) {
return ` in ${ funcTypeText } ${ node.name ? node.name.getText() : '' }()`;
} else if (node.kind === ts.SyntaxKind.Constructor) {
return ` in class ${ this.currentClassName }`;
}
Expand Down
7 changes: 4 additions & 3 deletions src/mochaNoSideEffectCodeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {ExtendedMetadata} from './utils/ExtendedMetadata';
import {AstUtils} from './utils/AstUtils';
import {MochaUtils} from './utils/MochaUtils';
import {Utils} from './utils/Utils';
import { isObject } from './utils/TypeGuard';

const FAILURE_STRING: string = 'Mocha test contains dangerous variable initialization. Move to before()/beforeEach(): ';

Expand Down Expand Up @@ -44,9 +45,9 @@ class MochaNoSideEffectCodeRuleWalker extends ErrorTolerantWalker {
}

private parseOptions() {
this.getOptions().forEach((opt: any) => {
if (typeof(opt) === 'object') {
if (opt.ignore !== undefined) {
this.getOptions().forEach((opt: unknown) => {
if (isObject(opt)) {
if (opt.ignore !== undefined && (typeof opt.ignore === 'string' || opt.ignore instanceof RegExp)) {
this.ignoreRegex = new RegExp(opt.ignore);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/noConstantConditionRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Lint from 'tslint';
import {AstUtils} from './utils/AstUtils';
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker';
import {ExtendedMetadata} from './utils/ExtendedMetadata';
import { isObject } from './utils/TypeGuard';

/**
* Implementation of the no-constant-condition rule.
Expand Down Expand Up @@ -43,8 +44,8 @@ class NoConstantConditionRuleWalker extends ErrorTolerantWalker {

private extractBoolean(keyName: string): boolean {
let result : boolean = true;
this.getOptions().forEach((opt: any) => {
if (typeof(opt) === 'object') {
this.getOptions().forEach((opt: unknown) => {
if (isObject(opt)) {
if (opt[keyName] === false || opt[keyName] === 'false') {
result = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/noDuplicateParameterNamesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class NoDuplicateParameterNamesWalker extends ErrorTolerantWalker {
private validateParameterNames(node : ts.SignatureDeclaration) {
const seenNames : {[index: string]: boolean} = {};
node.parameters.forEach((parameter : ts.ParameterDeclaration) : void => {
const parameterName : string = (<any>parameter.name).text; // how does one check if the union type is Identifier?
const parameterName : string = parameter.name.getText(); // how does one check if the union type is Identifier?
if (parameterName !== undefined) {
if (seenNames[parameterName]) {
this.addFailureAt(
Expand Down
2 changes: 1 addition & 1 deletion src/noHttpStringRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class NoHttpStringWalker extends ErrorTolerantWalker {
return options.ruleArguments[0];
}
if (options instanceof Array) {
return <string[]><any>options; // MSE version of tslint somehow requires this
return options;
}
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion src/noMissingVisibilityModifiersRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MissingVisibilityModifierWalker extends ErrorTolerantWalker {
super.visitMethodDeclaration(node);
}

private isMissingVisibilityModifier(node: ts.Node) : boolean {
private isMissingVisibilityModifier(node: ts.Declaration) : boolean {
return !(AstUtils.isPrivate(node) || AstUtils.isProtected(node) || AstUtils.isPublic(node));
}

Expand Down
6 changes: 3 additions & 3 deletions src/noUnexternalizedStringsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class NoUnexternalizedStringsRuleWalker extends ErrorTolerantWalker {
this.ignores = Object.create(null);
/* tslint:enable:no-null-keyword */

const options: any[] = this.getOptions();
const first: UnexternalizedStringsOptions = options && options.length > 0 ? options[0] : undefined;
const options: unknown = this.getOptions();
const first: UnexternalizedStringsOptions = options && Array.isArray(options) && options.length > 0 ? options[0] : undefined;
if (first) {
if (Array.isArray(first.signatures)) {
first.signatures.forEach((signature: string) => this.signatures[signature] = true);
Expand Down Expand Up @@ -114,7 +114,7 @@ class NoUnexternalizedStringsRuleWalker extends ErrorTolerantWalker {
const kind = parent.kind;
if (kind === kinds.CallExpression) {
const callExpression = <ts.CallExpression>parent;
return { callInfo: { callExpression: callExpression, argIndex: callExpression.arguments.indexOf(<any>node) }};
return { callInfo: { callExpression: callExpression, argIndex: callExpression.arguments.indexOf(<ts.Expression>node) }};
} else if (kind === kinds.ImportEqualsDeclaration || kind === kinds.ImportDeclaration || kind === kinds.ExportDeclaration) {
return { ignoreUsage: true };
} else if (kind === kinds.VariableDeclaration || kind === kinds.FunctionDeclaration || kind === kinds.PropertyDeclaration
Expand Down
2 changes: 1 addition & 1 deletion src/noUnsupportedBrowserCodeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class NoUnsupportedBrowserCodeRuleWalker extends Lint.RuleWalker {
private parseSupportedBrowsers(): { [key: string]: BrowserVersion } {
const result: { [key: string]: BrowserVersion } = {};

this.getOptions().forEach((option: any) => {
this.getOptions().forEach((option: unknown) => {
if (option instanceof Array) {
option.forEach((browserString: string) => {
const browser = this.parseBrowserString(browserString);
Expand Down
5 changes: 3 additions & 2 deletions src/preferArrayLiteralRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Lint from 'tslint';
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker';
import {AstUtils} from './utils/AstUtils';
import {ExtendedMetadata} from './utils/ExtendedMetadata';
import { isObject } from './utils/TypeGuard';

/**
* Implementation of the prefer-array-literal rule.
Expand Down Expand Up @@ -39,8 +40,8 @@ class NoGenericArrayWalker extends ErrorTolerantWalker {

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
this.getOptions().forEach((opt: any) => {
if (typeof(opt) === 'object') {
this.getOptions().forEach((opt: unknown) => {
if (isObject(opt)) {
this.allowTypeParameters = opt['allow-type-parameters'] === true;
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/promiseMustCompleteRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PromiseAnalyzer extends ErrorTolerantWalker {

protected visitNewExpression(node: ts.NewExpression): void {
if (this.isPromiseDeclaration(node) && node.arguments !== undefined) {
const functionArgument: ts.FunctionLikeDeclaration = <ts.FunctionLikeDeclaration><any>node.arguments[0];
const functionArgument: ts.FunctionLikeDeclaration = <ts.FunctionLikeDeclaration>node.arguments[0];
const functionBody = functionArgument.body;

if (functionBody !== undefined) {
Expand Down
7 changes: 4 additions & 3 deletions src/reactA11yAnchorsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getStringLiteral,
isEmpty
} from './utils/JsxAttribute';
import { isObject } from './utils/TypeGuard';

export const OPTION_IGNORE_CASE: string = 'ignore-case';
export const OPTION_IGNORE_WHITESPACE: string = 'ignore-whitespace';
Expand Down Expand Up @@ -88,13 +89,13 @@ class ReactA11yAnchorsRuleWalker extends ErrorTolerantWalker {
}

private parseOptions(): void {
this.getOptions().forEach((opt: any) => {
this.getOptions().forEach((opt: unknown) => {
if (typeof opt === 'string' && opt === OPTION_IGNORE_CASE) {
this.ignoreCase = true;
}

if (typeof opt === 'object') {
this.ignoreWhitespace = opt[OPTION_IGNORE_WHITESPACE];
if (isObject(opt)) {
this.ignoreWhitespace = <string>opt[OPTION_IGNORE_WHITESPACE];
}
});
}
Expand Down
10 changes: 5 additions & 5 deletions src/reactA11yAriaUnsupportedElementsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { IDom } from './utils/attributes/IDom';
import { IAria } from './utils/attributes/IAria';

// tslint:disable:no-require-imports no-var-requires
const DOM_SCHEMA: IDom[] = require('./utils/attributes/domSchema.json');
const ARIA_SCHEMA: IAria[] = require('./utils/attributes/ariaSchema.json');
const DOM_SCHEMA: { [key: string]: IDom } = require('./utils/attributes/domSchema.json');
const ARIA_SCHEMA: { [key: string]: IAria } = require('./utils/attributes/ariaSchema.json');
// tslint:enable:no-require-imports no-var-requires

export function getFailureString(tagName: string, ariaAttributeNames: string[]): string {
Expand Down Expand Up @@ -55,12 +55,12 @@ class ReactA11yAriaUnsupportedElementsWalker extends Lint.RuleWalker {
private validateOpeningElement(node: ts.JsxOpeningLikeElement): void {
const tagName: string = node.tagName.getText();

if (!(<any>DOM_SCHEMA)[tagName]) {
if (!DOM_SCHEMA[tagName]) {
return;
}

const supportAria: boolean = (<any>DOM_SCHEMA)[tagName].supportAria !== undefined
? (<any>DOM_SCHEMA)[tagName].supportAria
const supportAria: boolean = DOM_SCHEMA[tagName].supportAria !== undefined
? DOM_SCHEMA[tagName].supportAria
: false;

if (supportAria) {
Expand Down
4 changes: 2 additions & 2 deletions src/reactA11yEventHasRoleRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ExtendedMetadata } from './utils/ExtendedMetadata';
import { IDom } from './utils/attributes/IDom';

// tslint:disable-next-line:no-require-imports no-var-requires
const DOM_SCHEMA: IDom[] = require('./utils/attributes/domSchema.json');
const DOM_SCHEMA: { [key: string]: IDom } = require('./utils/attributes/domSchema.json');
const FAILURE_STRING: string = 'Elements with event handlers must have role attribute.';
const ROLE_STRING: string = 'role';
const TARGET_EVENTS: string[] = ['click', 'keyup', 'keydown', 'keypress', 'mousedown', 'mouseup',
Expand Down Expand Up @@ -52,7 +52,7 @@ class ReactA11yEventHasRoleWalker extends Lint.RuleWalker {
private checkJsxOpeningElement(node: ts.JsxOpeningLikeElement): void {
const tagName: string = node.tagName.getText();

if (!(<any>DOM_SCHEMA)[tagName]) {
if (!DOM_SCHEMA[tagName]) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/reactA11yNoOnchangeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class ReactA11yNoOnchangeRuleWalker extends ErrorTolerantWalker {

private checkJsxOpeningElement(node: ts.JsxOpeningLikeElement) {
const tagName: string = node.tagName.getText();
const options: any[] = this.getOptions();
const options: unknown = this.getOptions();

const additionalTagNames: string[] = options.length > 0 ? options[0] : [];
const additionalTagNames: string[] = Array.isArray(options) && options.length > 0 ? options[0] : [];

const targetTagNames: string[] = ['select', ...additionalTagNames];

Expand Down
6 changes: 3 additions & 3 deletions src/reactA11yRoleHasRequiredAriaPropsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { IAria } from './utils/attributes/IAria';

// tslint:disable-next-line:no-require-imports no-var-requires
const ROLES_SCHEMA: IRoleSchema = require('./utils/attributes/roleSchema.json');
const ROLES: IRole[] = ROLES_SCHEMA.roles;
const ROLES: { [key: string]: IRole } = ROLES_SCHEMA.roles;

// tslint:disable-next-line:no-require-imports no-var-requires
const ARIA_ATTRIBUTES: { [attributeName: string]: IAria } = require('./utils/attributes/ariaSchema.json');
Expand Down Expand Up @@ -82,7 +82,7 @@ class A11yRoleHasRequiredAriaPropsWalker extends Lint.RuleWalker {
const roleValue = roleProp ? getStringLiteral(roleProp) : getImplicitRole(node);
const isImplicitRole: boolean = !roleProp && !!roleValue;
const normalizedRoles: string[] = (roleValue || '').toLowerCase().split(' ')
.filter((role: string) => !!(<any>ROLES)[role]);
.filter((role: string) => !!ROLES[role]);

if (normalizedRoles.length === 0) {
return;
Expand All @@ -91,7 +91,7 @@ class A11yRoleHasRequiredAriaPropsWalker extends Lint.RuleWalker {
let requiredAttributeNames: string[] = [];

normalizedRoles.forEach((role: string) => {
requiredAttributeNames = requiredAttributeNames.concat((<any>ROLES)[role].requiredProps || []);
requiredAttributeNames = requiredAttributeNames.concat(ROLES[role].requiredProps || []);
});

const attributeNamesInElement: string[] = Object.keys(attributesInElement)
Expand Down
Loading

0 comments on commit 31529b2

Please sign in to comment.