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: Add ignore-params flag
Browse files Browse the repository at this point in the history
This change allows using no-inferrable types at times when you
may want to still require types for method/function parameter lists
(for example when using the typedef rule)
  • Loading branch information
abierbaum committed May 2, 2016
1 parent a99fbf7 commit e3e6424
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/rules/noInferrableTypesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import * as ts from "typescript";
import * as Lint from "../lint";

const OPTION_IGNORE_PARMS = "ignore-params";

export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING_FACTORY = (type: string) => `LHS type (${type}) inferred by RHS expression, remove type annotation`;

Expand All @@ -33,7 +35,9 @@ class NoInferrableTypesWalker extends Lint.RuleWalker {
}

public visitParameterDeclaration(node: ts.ParameterDeclaration) {
this.checkDeclaration(node);
if (!this.hasOption(OPTION_IGNORE_PARMS)) {
this.checkDeclaration(node);
}
super.visitParameterDeclaration(node);
}

Expand Down
32 changes: 32 additions & 0 deletions test/rules/no-inferrable-types/ignore-params/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// errors, inferrable type is declared
let x: number = 7;
~~~~~~ [number]
let y: boolean = false;
~~~~~~~ [boolean]
let z: string = "foo";
~~~~~~ [string]

// not errors, we are skipping params
function foo (a: number = 5, b: boolean = true, c: string = "bah") { }

class TestClass {
doSomething(a: number = 5, b: boolean = true, c: string = "bah") {}
}

// not errors, inferrable type is not declared
let _x = 7;
let _y = false;
let _z = "foo";

// not error, type is not inferrable
let weird: any = 123;

// not errors, inferrable type is not declared
function bar(a = 5, b = true, c = "bah") { }

// not errors, types are not inferrable
function baz(a: any = 5, b: any = true, c: any = "bah") { }

[number]: LHS type (number) inferred by RHS expression, remove type annotation
[boolean]: LHS type (boolean) inferred by RHS expression, remove type annotation
[string]: LHS type (string) inferred by RHS expression, remove type annotation
8 changes: 8 additions & 0 deletions test/rules/no-inferrable-types/ignore-params/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rules": {
"no-inferrable-types": [
true,
"ignore-params"
]
}
}

0 comments on commit e3e6424

Please sign in to comment.