File tree 3 files changed +64
-9
lines changed
3 files changed +64
-9
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,23 @@ interface Bar {
38
38
interface Baz extends Foo , Bar {}
39
39
```
40
40
41
+ ### Options
42
+
43
+ This rule accepts a single object option with the following default configuration:
44
+
45
+ ``` json
46
+ {
47
+ "@typescript-eslint/no-empty-interface" : [
48
+ " error" ,
49
+ {
50
+ "allowSingleExtends" : false
51
+ }
52
+ ]
53
+ }
54
+ ```
55
+
56
+ - ` allowSingleExtends: true ` will silence warnings about extending a single interface without adding additional members
57
+
41
58
## When Not To Use It
42
59
43
60
If you don't care about having empty/meaningless interfaces, then you will not need this rule.
Original file line number Diff line number Diff line change 6
6
import { TSESTree } from '@typescript-eslint/typescript-estree' ;
7
7
import * as util from '../util' ;
8
8
9
- export default util . createRule ( {
9
+ type Options = [
10
+ {
11
+ allowSingleExtends ?: boolean ;
12
+ }
13
+ ] ;
14
+ type MessageIds = 'noEmpty' | 'noEmptyWithSuper' ;
15
+
16
+ export default util . createRule < Options , MessageIds > ( {
10
17
name : 'no-empty-interface' ,
11
18
meta : {
12
19
type : 'suggestion' ,
@@ -21,13 +28,28 @@ export default util.createRule({
21
28
noEmptyWithSuper :
22
29
'An interface declaring no members is equivalent to its supertype.'
23
30
} ,
24
- schema : [ ]
31
+ schema : [
32
+ {
33
+ type : 'object' ,
34
+ additionalProperties : false ,
35
+ properties : {
36
+ allowSingleExtends : {
37
+ type : 'boolean'
38
+ }
39
+ }
40
+ }
41
+ ]
25
42
} ,
26
- defaultOptions : [ ] ,
27
- create ( context ) {
43
+ defaultOptions : [
44
+ {
45
+ allowSingleExtends : false
46
+ }
47
+ ] ,
48
+ create ( context , [ { allowSingleExtends } ] ) {
28
49
return {
29
50
TSInterfaceDeclaration ( node : TSESTree . TSInterfaceDeclaration ) {
30
51
if ( node . body . body . length !== 0 ) {
52
+ // interface contains members --> Nothing to report
31
53
return ;
32
54
}
33
55
@@ -37,10 +59,15 @@ export default util.createRule({
37
59
messageId : 'noEmpty'
38
60
} ) ;
39
61
} else if ( node . extends . length === 1 ) {
40
- context . report ( {
41
- node : node . id ,
42
- messageId : 'noEmptyWithSuper'
43
- } ) ;
62
+ // interface extends exactly 1 interface --> Report depending on rule setting
63
+ if ( allowSingleExtends ) {
64
+ return ;
65
+ } else {
66
+ context . report ( {
67
+ node : node . id ,
68
+ messageId : 'noEmptyWithSuper'
69
+ } ) ;
70
+ }
44
71
}
45
72
}
46
73
} ;
Original file line number Diff line number Diff line change @@ -23,7 +23,17 @@ interface Bar {
23
23
24
24
// valid because extending multiple interfaces can be used instead of a union type
25
25
interface Baz extends Foo, Bar {}
26
- `
26
+ ` ,
27
+ {
28
+ code : `
29
+ interface Foo {
30
+ name: string;
31
+ }
32
+
33
+ interface Bar extends Foo {}
34
+ ` ,
35
+ options : [ { allowSingleExtends : true } ]
36
+ }
27
37
] ,
28
38
invalid : [
29
39
{
@@ -54,6 +64,7 @@ interface Foo {
54
64
55
65
interface Bar extends Foo {}
56
66
` ,
67
+ options : [ { allowSingleExtends : false } ] ,
57
68
errors : [
58
69
{
59
70
messageId : 'noEmptyWithSuper' ,
You can’t perform that action at this time.
0 commit comments