From e3e6424239b573f50409e3467774108af946eeed Mon Sep 17 00:00:00 2001 From: Allen Bierbaum Date: Sat, 30 Apr 2016 18:28:30 -0500 Subject: [PATCH] no-inferrable-types: Add ignore-params flag 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) --- src/rules/noInferrableTypesRule.ts | 6 +++- .../{ => default}/test.ts.lint | 0 .../{ => default}/tslint.json | 0 .../ignore-params/test.ts.lint | 32 +++++++++++++++++++ .../ignore-params/tslint.json | 8 +++++ 5 files changed, 45 insertions(+), 1 deletion(-) rename test/rules/no-inferrable-types/{ => default}/test.ts.lint (100%) rename test/rules/no-inferrable-types/{ => default}/tslint.json (100%) create mode 100644 test/rules/no-inferrable-types/ignore-params/test.ts.lint create mode 100644 test/rules/no-inferrable-types/ignore-params/tslint.json diff --git a/src/rules/noInferrableTypesRule.ts b/src/rules/noInferrableTypesRule.ts index 98b8526e81d..4d7f90db3d5 100644 --- a/src/rules/noInferrableTypesRule.ts +++ b/src/rules/noInferrableTypesRule.ts @@ -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`; @@ -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); } diff --git a/test/rules/no-inferrable-types/test.ts.lint b/test/rules/no-inferrable-types/default/test.ts.lint similarity index 100% rename from test/rules/no-inferrable-types/test.ts.lint rename to test/rules/no-inferrable-types/default/test.ts.lint diff --git a/test/rules/no-inferrable-types/tslint.json b/test/rules/no-inferrable-types/default/tslint.json similarity index 100% rename from test/rules/no-inferrable-types/tslint.json rename to test/rules/no-inferrable-types/default/tslint.json diff --git a/test/rules/no-inferrable-types/ignore-params/test.ts.lint b/test/rules/no-inferrable-types/ignore-params/test.ts.lint new file mode 100644 index 00000000000..8a57f8fdf69 --- /dev/null +++ b/test/rules/no-inferrable-types/ignore-params/test.ts.lint @@ -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 diff --git a/test/rules/no-inferrable-types/ignore-params/tslint.json b/test/rules/no-inferrable-types/ignore-params/tslint.json new file mode 100644 index 00000000000..bad8056f990 --- /dev/null +++ b/test/rules/no-inferrable-types/ignore-params/tslint.json @@ -0,0 +1,8 @@ +{ + "rules": { + "no-inferrable-types": [ + true, + "ignore-params" + ] + } +}