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 Apr 30, 2016
1 parent a99fbf7 commit eaedf6a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/rules/noInferrableTypesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ export class Rule extends Lint.Rules.AbstractRule {
}

class NoInferrableTypesWalker extends Lint.RuleWalker {
public ignoreParameters: boolean = false;

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);

this.ignoreParameters = (options.ruleArguments.indexOf("ignore-params") !== -1);
}

public visitVariableDeclaration(node: ts.VariableDeclaration) {
this.checkDeclaration(node);
super.visitVariableDeclaration(node);
}

public visitParameterDeclaration(node: ts.ParameterDeclaration) {
this.checkDeclaration(node);
if (!this.ignoreParameters) {
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 eaedf6a

Please sign in to comment.