Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [typedef] remove all defaults #2352

Merged
merged 1 commit into from Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 29 additions & 16 deletions packages/eslint-plugin/docs/rules/typedef.md
Expand Up @@ -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:

Expand All @@ -48,17 +62,16 @@ For example, with the following configuration:
"@typescript-eslint/typedef": [
"error",
{
"arrowParameter": false,
"arrowParameter": true,
"variableDeclaration": true
}
]
}
}
```

- 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`

Expand Down
12 changes: 8 additions & 4 deletions packages/eslint-plugin/src/rules/typedef.ts
Expand Up @@ -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]) {
Expand Down
101 changes: 101 additions & 0 deletions packages/eslint-plugin/tests/rules/typedef.test.ts
Expand Up @@ -492,6 +492,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
arrowParameter: true,
},
],
},
{
code: 'const receivesStrings = (a, b): void => {};',
Expand All @@ -505,6 +510,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
arrowParameter: true,
},
],
},
// Member variable declarations
{
Expand All @@ -519,6 +529,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
memberVariableDeclaration: true,
},
],
},
{
code: `
Expand All @@ -531,6 +546,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
memberVariableDeclaration: true,
},
],
},
// Function parameters
{
Expand All @@ -541,6 +561,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesStrings(a, b): void {}',
Expand All @@ -554,6 +579,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesNumber([a]): void {}',
Expand All @@ -563,6 +593,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesNumbers([a, b]): void {}',
Expand All @@ -572,6 +607,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesString({ a }): void {}',
Expand All @@ -581,6 +621,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: 'function receivesStrings({ a, b }): void {}',
Expand All @@ -590,6 +635,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
// Constructor parameters
{
Expand All @@ -604,6 +654,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
Expand All @@ -617,6 +672,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
Expand All @@ -630,6 +690,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
// Method parameters
{
Expand All @@ -646,6 +711,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
Expand All @@ -661,6 +731,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
{
code: `
Expand All @@ -674,6 +749,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
parameter: true,
},
],
},
// Property declarations
{
Expand All @@ -688,6 +768,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
{
code: `
Expand All @@ -701,6 +786,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
{
code: `
Expand All @@ -714,6 +804,11 @@ class Foo {
messageId: 'expectedTypedefNamed',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
{
code: `
Expand All @@ -727,6 +822,11 @@ class Foo {
messageId: 'expectedTypedef',
},
],
options: [
{
propertyDeclaration: true,
},
],
},
// Variable declarations
{
Expand Down Expand Up @@ -879,6 +979,7 @@ class Foo {
],
options: [
{
memberVariableDeclaration: true,
variableDeclaration: true,
variableDeclarationIgnoreFunction: false,
},
Expand Down