@@ -13,6 +13,7 @@ const RULE_ID = 'selection-set-depth';
13
13
const rule : GraphQLESLintRule < [ SelectionSetDepthRuleConfig ] > = {
14
14
meta : {
15
15
type : 'suggestion' ,
16
+ // eslint-disable-next-line eslint-plugin/require-meta-has-suggestions -- optional since we can't provide fixes for fragments located in separate files
16
17
hasSuggestions : true ,
17
18
docs : {
18
19
category : 'Operations' ,
@@ -109,30 +110,32 @@ const rule: GraphQLESLintRule<[SelectionSetDepthRuleConfig]> = {
109
110
getDocument : ( ) => document ,
110
111
reportError ( error : GraphQLError ) {
111
112
const { line, column } = error . locations [ 0 ] ;
113
+
114
+ const ancestors = context . getAncestors ( ) ;
115
+ const token = ( ancestors [ 0 ] as AST . Program ) . tokens . find (
116
+ token => token . loc . start . line === line && token . loc . start . column === column - 1
117
+ ) ;
118
+
112
119
context . report ( {
113
120
loc : {
114
121
line,
115
122
column : column - 1 ,
116
123
} ,
117
124
message : error . message ,
118
- suggest : [
119
- {
120
- desc : 'Remove selections' ,
121
- fix ( fixer ) {
122
- const ancestors = context . getAncestors ( ) ;
123
- const token = ( ancestors [ 0 ] as AST . Program ) . tokens . find (
124
- token => token . loc . start . line === line && token . loc . start . column === column - 1
125
- ) ;
126
- if ( ! token ) {
127
- return null ;
128
- }
129
- const sourceCode = context . getSourceCode ( ) ;
130
- const foundNode = sourceCode . getNodeByRangeIndex ( token . range [ 0 ] ) as any ;
131
- const parentNode = foundNode . parent . parent ;
132
- return fixer . remove ( foundNode . kind === 'Name' ? parentNode . parent : parentNode ) ;
125
+ // Don't provide suggestions for fragment that can be in a separate file
126
+ ...( token && {
127
+ suggest : [
128
+ {
129
+ desc : 'Remove selections' ,
130
+ fix ( fixer ) {
131
+ const sourceCode = context . getSourceCode ( ) ;
132
+ const foundNode = sourceCode . getNodeByRangeIndex ( token . range [ 0 ] ) as any ;
133
+ const parentNode = foundNode . parent . parent ;
134
+ return fixer . remove ( foundNode . kind === 'Name' ? parentNode . parent : parentNode ) ;
135
+ } ,
133
136
} ,
134
- } ,
135
- ] ,
137
+ ] ,
138
+ } ) ,
136
139
} ) ;
137
140
} ,
138
141
} ) ;
0 commit comments