8000 feat(eslint-plugin): [typedef] remove all defaults (#2352) · esetnik/typescript-eslint@a9cd6fb · GitHub
[go: up one dir, main page]

Skip to content

Commit a9cd6fb

Browse files
committed
feat(eslint-plugin): [typedef] remove all defaults (typescript-eslint#2352)
1 parent a3f163a commit a9cd6fb

File tree

3 files changed

+138
-20
lines changed

3 files changed

+138
-20
lines changed

packages/eslint-plugin/docs/rules/typedef.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,43 @@ class ContainsText {
1616
}
1717
```
1818

19-
> Note: requiring type annotations unnecessarily can be cumbersome to maintain and generally reduces code readability.
20-
> TypeScript is often better at inferring types than easily written type annotations would allow.
21-
> Instead of enabling `typedef`, it is generally recommended to use the `--noImplicitAny` and/or `--strictPropertyInitialization` compiler options to enforce type annotations only when useful.
19+
**_Note:_** requiring type annotations unnecessarily can be cumbersome to maintain and generally reduces code readability.
20+
TypeScript is often better at inferring types than easily written type annotations would allow.
21+
22+
**Instead of enabling `typedef`, it is generally recommended to use the `--noImplicitAny` and `--strictPropertyInitialization` compiler options to enforce type annotations only when useful.**
2223

2324
## Rule Details
2425

2526
This rule can enforce type annotations in locations regardless of whether they're required.
2627
This is typically used to maintain consistency for element types that sometimes require them.
2728

28-
> To enforce type definitions existing on call signatures as per TSLint's `arrow-call-signature` and `call-signature` options, use `explicit-function-return-type`.
29+
> 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`.
2930
3031
## Options
3132

32-
This rule has an object option that may receive any of the following as booleans:
33+
```ts
34+
type Options = {
35+
arrayDestructuring?: boolean;
36+
arrowParameter?: boolean;
37+
memberVariableDeclaration?: boolean;
38+
objectDestructuring?: boolean;
39+
parameter?: boolean;
40+
propertyDeclaration?: boolean;
41+
variableDeclaration?: boolean;
42+
variableDeclarationIgnoreFunction?: boolean;
43+
};
3344

34-
- `"arrayDestructuring"`
35-
- `"arrowParameter"`: `true` by default
36-
- `"memberVariableDeclaration"`: `true` by default
37-
- `"objectDestructuring"`
38-
- `"parameter"`: `true` by default
39-
- `"propertyDeclaration"`: `true` by default
40-
- `"variableDeclaration"`,
41-
- `"variableDeclarationIgnoreFunction"`
45+
const defaultOptions: Options = {
46+
arrayDestructuring: false,
47+
arrowParameter: false,
48+
memberVariableDeclaration: false,
49+
objectDestructuring: false,
50+
parameter: false,
51+
propertyDeclaration: false,
52+
variableDeclaration: false,
53+
variableDeclarationIgnoreFunction: false,
54+
};
55+
```
4256

4357
For example, with the following configuration:
4458

@@ -48,17 +62,16 @@ For example, with the following configuration:
4862
"@typescript-eslint/typedef": [
4963
"error",
5064
{
51-
"arrowParameter": false,
65+
"arrowParameter": true,
5266
"variableDeclaration": true
5367
}
5468
]
5569
}
5670
}
5771
```
5872

59-
- Type annotations on arrow function parameters are not required
73+
- Type annotations on arrow function parameters are required
6074
- Type annotations on variables are required
61-
- Options otherwise adhere to the defaults
6275

6376
### `arrayDestructuring`
6477

packages/eslint-plugin/src/rules/typedef.ts

Lines changed: 8 additions & 4 deletions
< 1E79 td data-grid-cell-id="diff-227ec0035a25622ad452ab1c03b7aa1fabe179c2327eaa38e5925229a11115dd-51-51-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">51
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ export default util.createRule<[Options], MessageIds>({
5050
},
51
defaultOptions: [
5252
{
53-
[OptionKeys.ArrowParameter]: true,
54-
[OptionKeys.MemberVariableDeclaration]: true,
55-
[OptionKeys.Parameter]: true,
56-
[OptionKeys.PropertyDeclaration]: true,
53+
[OptionKeys.ArrayDestructuring]: false,
54+
[OptionKeys.ArrowParameter]: false,
55+
[OptionKeys.MemberVariableDeclaration]: false,
56+
[OptionKeys.ObjectDestructuring]: false,
57+
[OptionKeys.Parameter]: false,
58+
[OptionKeys.PropertyDeclaration]: false,
59+
[OptionKeys.VariableDeclaration]: false,
60+
[OptionKeys.VariableDeclarationIgnoreFunction]: false,
5761
},
5862
],
5963
create(context, [options]) {

packages/eslint-plugin/tests/rules/typedef.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,11 @@ class Foo {
492492
messageId: 'expectedTypedefNamed',
493493
},
494494
],
495+
options: [
496+
{
497+
arrowParameter: true,
498+
},
499+
],
495500
},
496501
{
497502
code: 'const receivesStrings = (a, b): void => {};',
@@ -505,6 +510,11 @@ class Foo {
505510
messageId: 'expectedTypedefNamed',
506511
},
507512
],
513+
options: [
514+
{
515+
arrowParameter: true,
516+
},
517+
],
508518
},
509519
// Member variable declarations
510520
{
@@ -519,6 +529,11 @@ class Foo {
519529
messageId: 'expectedTypedefNamed',
520530
},
521531
],
532+
options: [
533+
{
534+
memberVariableDeclaration: true,
535+
},
536+
],
522537
},
523538
{
524539
code: `
@@ -531,6 +546,11 @@ class Foo {
531546
messageId: 'expectedTypedef',
532547
},
533548
],
549+
options: [
550+
{
551+
memberVariableDeclaration: true,
552+
},
553+
],
534554
},
535555
// Function parameters
536556
{
@@ -541,6 +561,11 @@ class Foo {
541561
messageId: 'expectedTypedefNamed',
542562
},
543563
],
564+
options: [
565+
{
566+
parameter: true,
567+
},
568+
],
544569
},
545570
{
546571
code: 'function receivesStrings(a, b): void {}',
@@ -554,6 +579,11 @@ class Foo {
554579
messageId: 'expectedTypedefNamed',
555580
},
556581
],
582+
options: [
583+
{
584+
parameter: true,
585+
},
586+
],
557587
},
558588
{
559589
code: 'function receivesNumber([a]): void {}',
@@ -563,6 +593,11 @@ class Foo {
5635 10000 93
messageId: 'expectedTypedef',
564594
},
565595
],
596+
options: [
597+
{
598+
parameter: true,
599+
},
600+
],
566601
},
567602
{
568603
code: 'function receivesNumbers([a, b]): void {}',
@@ -572,6 +607,11 @@ class Foo {
572607
messageId: 'expectedTypedef',
573608
},
574609
],
610+
options: [
611+
{
612+
parameter: true,
613+
},
614+
],
575615
},
576616
{
577617
code: 'function receivesString({ a }): void {}',
@@ -581,6 +621,11 @@ class Foo {
581621
messageId: 'expectedTypedef',
582622
},
583623
],
624+
options: [
625+
{
626+
parameter: true,
627+
},
628+
],
584629
},
585630
{
586631
code: 'function receivesStrings({ a, b }): void {}',
@@ -590,6 +635,11 @@ class Foo {
590635
messageId: 'expectedTypedef',
591636
},
592637
],
638+
options: [
639+
{
640+
parameter: true,
641+
},
642+
],
593643
},
594644
// Constructor parameters
595645
{
@@ -604,6 +654,11 @@ class Foo {
604654
messageId: 'expectedTypedefNamed',
605655
},
606656
],
657+
options: [
658+
{
659+
parameter: true,
660+
},
661+
],
607662
},
608663
{
609664
code: `
@@ -617,6 +672,11 @@ class Foo {
617672
messageId: 'expectedTypedef',
618673
},
619674
],
675+
options: [
676+
{
677+
parameter: true,
678+
},
679+
],
620680
},
621681
{
622682
code: `
@@ -630,6 +690,11 @@ class Foo {
630690
messageId: 'expectedTypedef',
631691
},
632692
],
693+
options: [
694+
{
695+
parameter: true,
696+
},
697+
],
633698
},
634699
// Method parameters
635700
{
@@ -646,6 +711,11 @@ class Foo {
646711
messageId: 'expectedTypedefNamed',
647712
},
648713
],
714+
options: [
715+
{
716+
parameter: true,
717+
},
718+
],
649719
},
650720
{
651721
code: `
@@ -661,6 +731,11 @@ class Foo {
661731
messageId: 'expectedTypedef',
662732
},
663733
],
734+
options: [
735+
{
736+
parameter: true,
737+
},
738+
],
664739
},
665740
{
666741
code: `
@@ -674,6 +749,11 @@ class Foo {
674749
messageId: 'expectedTypedef',
675750
},
676751
],
752+
options: [
753+
{
754+
parameter: true,
755+
},
756+
],
677757
},
678758
// Property declarations
679759
{
@@ -688,6 +768,11 @@ class Foo {
688768
messageId: 'expectedTypedefNamed',
689769
},
690770
],
771+
options: [
772+
{
773+
propertyDeclaration: true,
774+
},
775+
],
691776
},
692777
{
693778
code: `
@@ -701,6 +786,11 @@ class Foo {
701786
messageId: 'expectedTypedef',
702787
},
703788
],
789+
options: [
790+
{
791+
propertyDeclaration: true,
792+
},
793+
],
704794
},
705795
{
706796
code: `
@@ -714,6 +804,11 @@ class Foo {
714804
messageId: 'expectedTypedefNamed',
715805
},
716806
],
807+
options: [
808+
{
809+
propertyDeclaration: true,
810+
},
811+
],
717812
},
718813
{
719814
code: `
@@ -727,6 +822,11 @@ class Foo {
727822
messageId: 'expectedTypedef',
728823
},
729824
],
825+
options: [
826+
{
827+
propertyDeclaration: true,
828+
},
829+
],
730830
},
731831
// Variable declarations
732832
{
@@ -879,6 +979,7 @@ class Foo {
879979
],
880980
options: [
881981
{
982+
memberVariableDeclaration: true,
882983
variableDeclaration: true,
883984
variableDeclarationIgnoreFunction: false,
884985
},

0 commit comments

Comments
 (0)
0