@@ -13,6 +13,7 @@ const RULE_ID = 'selection-set-depth';
1313const rule : GraphQLESLintRule < [ SelectionSetDepthRuleConfig ] > = {
1414 meta : {
1515 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
1617 hasSuggestions : true ,
1718 docs : {
1819 category : 'Operations' ,
@@ -109,30 +110,32 @@ const rule: GraphQLESLintRule<[SelectionSetDepthRuleConfig]> = {
109110 getDocument : ( ) => document ,
110111 reportError ( error : GraphQLError ) {
111112 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+
112119 context . report ( {
113120 loc : {
114121 line,
115122 column : column - 1 ,
116123 } ,
117124 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+ } ,
133136 } ,
134- } ,
135- ] ,
137+ ] ,
138+ } ) ,
136139 } ) ;
137140 } ,
138141 } ) ;
0 commit comments