From 113763f9b0607805de4bec9e737f2b5d6767105e Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 5 May 2017 21:24:11 -0700 Subject: [PATCH] No-inferrable-types: Clean up code (#2677) --- src/rules/noInferrableTypesRule.ts | 94 +++++++++++++----------------- 1 file changed, 40 insertions(+), 54 deletions(-) diff --git a/src/rules/noInferrableTypesRule.ts b/src/rules/noInferrableTypesRule.ts index cacada4ac55..6ceb060ff26 100644 --- a/src/rules/noInferrableTypesRule.ts +++ b/src/rules/noInferrableTypesRule.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { hasAccessModifier, hasModifier } from "tsutils"; +import { hasModifier } from "tsutils"; import * as ts from "typescript"; import * as Lint from "../index"; @@ -23,7 +23,7 @@ import * as Lint from "../index"; const OPTION_IGNORE_PARMS = "ignore-params"; const OPTION_IGNORE_PROPERTIES = "ignore-properties"; -interface IOptions { +interface Options { ignoreParameters: boolean; ignoreProperties: boolean; } @@ -72,68 +72,54 @@ export class Rule extends Lint.Rules.AbstractRule { } } -class NoInferrableTypesWalker extends Lint.AbstractWalker { +class NoInferrableTypesWalker extends Lint.AbstractWalker { public walk(sourceFile: ts.SourceFile) { const cb = (node: ts.Node): void => { - switch (node.kind) { - case ts.SyntaxKind.Parameter: - if (!this.options.ignoreParameters && - !hasModifier(node.modifiers, ts.SyntaxKind.ReadonlyKeyword) && - // "ignore-properties" also works for parameter properties - (!this.options.ignoreProperties || !hasAccessModifier(node as ts.ParameterDeclaration))) { - this.checkDeclaration(node as ts.ParameterDeclaration); - } - break; - case ts.SyntaxKind.PropertyDeclaration: - if (this.options.ignoreProperties || - hasModifier(node.modifiers, ts.SyntaxKind.ReadonlyKeyword)) { - break; - } - /* falls through*/ - case ts.SyntaxKind.VariableDeclaration: - this.checkDeclaration(node as ts.VariableLikeDeclaration); + if (shouldCheck(node, this.options)) { + const { name, type, initializer } = node; + if (type !== undefined && initializer !== undefined && typeIsInferrable(type.kind, initializer.kind)) { + const fix = Lint.Replacement.deleteFromTo(name.end, type.end); + this.addFailureAtNode(type, Rule.FAILURE_STRING_FACTORY(ts.tokenToString(type.kind)), fix); + } } return ts.forEachChild(node, cb); }; return ts.forEachChild(sourceFile, cb); } +} - private checkDeclaration(node: ts.VariableLikeDeclaration) { - if (node.type != null && node.initializer != null) { - let failure: string | null = null; +function shouldCheck(node: ts.Node, { ignoreParameters, ignoreProperties }: Options): node is ts.VariableLikeDeclaration { + switch (node.kind) { + case ts.SyntaxKind.Parameter: + return !ignoreParameters && + !hasModifier(node.modifiers, ts.SyntaxKind.ReadonlyKeyword) && + // "ignore-properties" also works for parameter properties + !(ignoreProperties && node.modifiers); + case ts.SyntaxKind.PropertyDeclaration: + return !ignoreProperties && !hasModifier(node.modifiers, ts.SyntaxKind.ReadonlyKeyword); + case ts.SyntaxKind.VariableDeclaration: + return true; + default: + return false; + } +} - switch (node.type.kind) { - case ts.SyntaxKind.BooleanKeyword: - if (node.initializer.kind === ts.SyntaxKind.TrueKeyword || node.initializer.kind === ts.SyntaxKind.FalseKeyword) { - failure = "boolean"; - } - break; - case ts.SyntaxKind.NumberKeyword: - if (node.initializer.kind === ts.SyntaxKind.NumericLiteral) { - failure = "number"; - } - break; - case ts.SyntaxKind.StringKeyword: - switch (node.initializer.kind) { - case ts.SyntaxKind.StringLiteral: - case ts.SyntaxKind.NoSubstitutionTemplateLiteral: - case ts.SyntaxKind.TemplateExpression: - failure = "string"; - break; - default: - break; - } - break; +function typeIsInferrable(type: ts.SyntaxKind, initializer: ts.SyntaxKind): boolean { + switch (type) { + case ts.SyntaxKind.BooleanKeyword: + return initializer === ts.SyntaxKind.TrueKeyword || initializer === ts.SyntaxKind.FalseKeyword; + case ts.SyntaxKind.NumberKeyword: + return initializer === ts.SyntaxKind.NumericLiteral; + case ts.SyntaxKind.StringKeyword: + switch (initializer) { + case ts.SyntaxKind.StringLiteral: + case ts.SyntaxKind.NoSubstitutionTemplateLiteral: + case ts.SyntaxKind.TemplateExpression: + return true; default: - break; - } - - if (failure != null) { - this.addFailureAtNode(node.type, - Rule.FAILURE_STRING_FACTORY(failure), - Lint.Replacement.deleteFromTo(node.name.end, node.type.end), - ); + return false; } - } + default: + return false; } }