From a9cd6fb893074e4f2ca9ad3497eaddfacb3cfd25 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 3 Aug 2020 10:18:15 -0700 Subject: [PATCH] feat(eslint-plugin): [typedef] remove all defaults (#2352) --- packages/eslint-plugin/docs/rules/typedef.md | 45 +++++--- packages/eslint-plugin/src/rules/typedef.ts | 12 ++- .../eslint-plugin/tests/rules/typedef.test.ts | 101 ++++++++++++++++++ 3 files changed, 138 insertions(+), 20 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/typedef.md b/packages/eslint-plugin/docs/rules/typedef.md index 594f7b65e7d..6c96fa0317d 100644 --- a/packages/eslint-plugin/docs/rules/typedef.md +++ b/packages/eslint-plugin/docs/rules/typedef.md @@ -16,29 +16,43 @@ class ContainsText { } ``` -> Note: requiring type annotations unnecessarily can be cumbersome to maintain and generally reduces code readability. -> TypeScript is often better at inferring types than easily written type annotations would allow. -> Instead of enabling `typedef`, it is generally recommended to use the `--noImplicitAny` and/or `--strictPropertyInitialization` compiler options to enforce type annotations only when useful. +**_Note:_** requiring type annotations unnecessarily can be cumbersome to maintain and generally reduces code readability. +TypeScript is often better at inferring types than easily written type annotations would allow. + +**Instead of enabling `typedef`, it is generally recommended to use the `--noImplicitAny` and `--strictPropertyInitialization` compiler options to enforce type annotations only when useful.** ## Rule Details This rule can enforce type annotations in locations regardless of whether they're required. This is typically used to maintain consistency for element types that sometimes require them. -> To enforce type definitions existing on call signatures as per TSLint's `arrow-call-signature` and `call-signature` options, use `explicit-function-return-type`. +> To enforce type definitions existing on call signatures as per TSLint's `arrow-call-signature` and `call-signature` options, use `explicit-function-return-type`, or `explicit-module-boundary-types`. ## Options -This rule has an object option that may receive any of the following as booleans: +```ts +type Options = { + arrayDestructuring?: boolean; + arrowParameter?: boolean; + memberVariableDeclaration?: boolean; + objectDestructuring?: boolean; + parameter?: boolean; + propertyDeclaration?: boolean; + variableDeclaration?: boolean; + variableDeclarationIgnoreFunction?: boolean; +}; -- `"arrayDestructuring"` -- `"arrowParameter"`: `true` by default -- `"memberVariableDeclaration"`: `true` by default -- `"objectDestructuring"` -- `"parameter"`: `true` by default -- `"propertyDeclaration"`: `true` by default -- `"variableDeclaration"`, -- `"variableDeclarationIgnoreFunction"` +const defaultOptions: Options = { + arrayDestructuring: false, + arrowParameter: false, + memberVariableDeclaration: false, + objectDestructuring: false, + parameter: false, + propertyDeclaration: false, + variableDeclaration: false, + variableDeclarationIgnoreFunction: false, +}; +``` For example, with the following configuration: @@ -48,7 +62,7 @@ For example, with the following configuration: "@typescript-eslint/typedef": [ "error", { - "arrowParameter": false, + "arrowParameter": true, "variableDeclaration": true } ] @@ -56,9 +70,8 @@ For example, with the following configuration: } ``` -- Type annotations on arrow function parameters are not required +- Type annotations on arrow function parameters are required - Type annotations on variables are required -- Options otherwise adhere to the defaults ### `arrayDestructuring` diff --git a/packages/eslint-plugin/src/rules/typedef.ts b/packages/eslint-plugin/src/rules/typedef.ts index 9545364c839..1d66cff4290 100644 --- a/packages/eslint-plugin/src/rules/typedef.ts +++ b/packages/eslint-plugin/src/rules/typedef.ts @@ -50,10 +50,14 @@ export default util.createRule<[Options], MessageIds>({ }, defaultOptions: [ { - [OptionKeys.ArrowParameter]: true, - [OptionKeys.MemberVariableDeclaration]: true, - [OptionKeys.Parameter]: true, - [OptionKeys.PropertyDeclaration]: true, + [OptionKeys.ArrayDestructuring]: false, + [OptionKeys.ArrowParameter]: false, + [OptionKeys.MemberVariableDeclaration]: false, + [OptionKeys.ObjectDestructuring]: false, + [OptionKeys.Parameter]: false, + [OptionKeys.PropertyDeclaration]: false, + [OptionKeys.VariableDeclaration]: false, + [OptionKeys.VariableDeclarationIgnoreFunction]: false, }, ], create(context, [options]) { diff --git a/packages/eslint-plugin/tests/rules/typedef.test.ts b/packages/eslint-plugin/tests/rules/typedef.test.ts index e624670d206..93528da7594 100644 --- a/packages/eslint-plugin/tests/rules/typedef.test.ts +++ b/packages/eslint-plugin/tests/rules/typedef.test.ts @@ -492,6 +492,11 @@ class Foo { messageId: 'expectedTypedefNamed', }, ], + options: [ + { + arrowParameter: true, + }, + ], }, { code: 'const receivesStrings = (a, b): void => {};', @@ -505,6 +510,11 @@ class Foo { messageId: 'expectedTypedefNamed', }, ], + options: [ + { + arrowParameter: true, + }, + ], }, // Member variable declarations { @@ -519,6 +529,11 @@ class Foo { messageId: 'expectedTypedefNamed', }, ], + options: [ + { + memberVariableDeclaration: true, + }, + ], }, { code: ` @@ -531,6 +546,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + memberVariableDeclaration: true, + }, + ], }, // Function parameters { @@ -541,6 +561,11 @@ class Foo { messageId: 'expectedTypedefNamed', }, ], + options: [ + { + parameter: true, + }, + ], }, { code: 'function receivesStrings(a, b): void {}', @@ -554,6 +579,11 @@ class Foo { messageId: 'expectedTypedefNamed', }, ], + options: [ + { + parameter: true, + }, + ], }, { code: 'function receivesNumber([a]): void {}', @@ -563,6 +593,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + parameter: true, + }, + ], }, { code: 'function receivesNumbers([a, b]): void {}', @@ -572,6 +607,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + parameter: true, + }, + ], }, { code: 'function receivesString({ a }): void {}', @@ -581,6 +621,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + parameter: true, + }, + ], }, { code: 'function receivesStrings({ a, b }): void {}', @@ -590,6 +635,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + parameter: true, + }, + ], }, // Constructor parameters { @@ -604,6 +654,11 @@ class Foo { messageId: 'expectedTypedefNamed', }, ], + options: [ + { + parameter: true, + }, + ], }, { code: ` @@ -617,6 +672,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + parameter: true, + }, + ], }, { code: ` @@ -630,6 +690,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + parameter: true, + }, + ], }, // Method parameters { @@ -646,6 +711,11 @@ class Foo { messageId: 'expectedTypedefNamed', }, ], + options: [ + { + parameter: true, + }, + ], }, { code: ` @@ -661,6 +731,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + parameter: true, + }, + ], }, { code: ` @@ -674,6 +749,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + parameter: true, + }, + ], }, // Property declarations { @@ -688,6 +768,11 @@ class Foo { messageId: 'expectedTypedefNamed', }, ], + options: [ + { + propertyDeclaration: true, + }, + ], }, { code: ` @@ -701,6 +786,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + propertyDeclaration: true, + }, + ], }, { code: ` @@ -714,6 +804,11 @@ class Foo { messageId: 'expectedTypedefNamed', }, ], + options: [ + { + propertyDeclaration: true, + }, + ], }, { code: ` @@ -727,6 +822,11 @@ class Foo { messageId: 'expectedTypedef', }, ], + options: [ + { + propertyDeclaration: true, + }, + ], }, // Variable declarations { @@ -879,6 +979,7 @@ class Foo { ], options: [ { + memberVariableDeclaration: true, variableDeclaration: true, variableDeclarationIgnoreFunction: false, },