Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
No-inferrable-types: Clean up code (#2677)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson authored and nchen63 committed May 6, 2017
1 parent 96a5a4e commit 113763f
Showing 1 changed file with 40 additions and 54 deletions.
94 changes: 40 additions & 54 deletions src/rules/noInferrableTypesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* limitations under the License.
*/

import { hasAccessModifier, hasModifier } from "tsutils";
import { hasModifier } from "tsutils";
import * as ts from "typescript";

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;
}
Expand Down Expand Up @@ -72,68 +72,54 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

class NoInferrableTypesWalker extends Lint.AbstractWalker<IOptions> {
class NoInferrableTypesWalker extends Lint.AbstractWalker<Options> {
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;
}
}

0 comments on commit 113763f

Please sign in to comment.