1
1
import * as util from '../util' ;
2
+ import {
3
+ AST_NODE_TYPES ,
4
+ TSESLint ,
5
+ } from '@typescript-eslint/experimental-utils' ;
2
6
3
7
type Options = [
4
8
{
@@ -43,6 +47,7 @@ export default util.createRule<Options, MessageIds>({
43
47
return {
44
48
TSInterfaceDeclaration ( node ) : void {
45
49
const sourceCode = context . getSourceCode ( ) ;
50
+ const filename = context . getFilename ( ) ;
46
51
47
52
if ( node . body . body . length !== 0 ) {
48
53
// interface contains members --> Nothing to report
@@ -58,21 +63,46 @@ export default util.createRule<Options, MessageIds>({
58
63
} else if ( extend . length === 1 ) {
59
64
// interface extends exactly 1 interface --> Report depending on rule setting
60
65
if ( ! allowSingleExtends ) {
66
+ const fix = ( fixer : TSESLint . RuleFixer ) : TSESLint . RuleFix => {
67
+ let typeParam = '' ;
68
+ if ( node . typeParameters ) {
69
+ typeParam = sourceCode . getText ( node . typeParameters ) ;
70
+ }
71
+ return fixer . replaceText (
72
+ node ,
73
+ `type ${ sourceCode . getText (
74
+ node . id ,
75
+ ) } ${ typeParam } = ${ sourceCode . getText ( extend [ 0 ] ) } `,
76
+ ) ;
77
+ } ;
78
+
79
+ // Check if interface is within ambient declaration
80
+ let useAutoFix = true ;
81
+ if ( util . isDefinitionFile ( filename ) ) {
82
+ const scope = context . getScope ( ) ;
83
+ if (
84
+ scope . block . parent &&
85
+ scope . block . parent . type ===
86
+ AST_NODE_TYPES . TSModuleDeclaration &&
87
+ scope . block . parent . declare
88
+ ) {
89
+ useAutoFix = false ;
90
+ }
91
+ }
92
+
61
93
context . report ( {
62
94
node : node . id ,
63
95
messageId : 'noEmptyWithSuper' ,
64
- fix : fixer => {
65
- let typeParam = '' ;
66
- if ( node . typeParameters ) {
67
- typeParam = sourceCode . getText ( node . typeParameters ) ;
68
- }
69
- return fixer . replaceText (
70
- node ,
71
- `type ${ sourceCode . getText (
72
- node . id ,
73
- ) } ${ typeParam } = ${ sourceCode . getText ( extend [ 0 ] ) } `,
74
- ) ;
75
- } ,
96
+ ...( useAutoFix
97
+ ? { fix }
98
+ : {
99
+ suggest : [
100
+ {
101
+ messageId : 'noEmptyWithSuper' ,
102
+ fix,
103
+ } ,
104
+ ] ,
105
+ } ) ,
76
106
} ) ;
77
107
}
78
108
}
0 commit comments