1
1
import { parseCode } from '@graphql-tools/graphql-tag-pluck' ;
2
- import { basename } from 'path' ;
3
2
4
3
const RELEVANT_KEYWORDS = [ 'gql' , 'graphql' , '/* GraphQL */' ] ;
5
4
6
5
type Block = {
7
6
text : string ;
8
7
filename : string ;
9
- lineOffset ? : number ;
10
- offset ? : number ;
8
+ lineOffset : number ;
9
+ offset : number ;
11
10
} ;
12
11
13
12
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
14
13
export function createGraphqlProcessor ( ) {
15
- const blocksMap = new Map < string , Block [ ] > ( ) ;
14
+ const blocksMap = new Map < string , Array < Block | string > > ( ) ;
16
15
17
16
return {
18
17
supportsAutofix : true ,
19
- preprocess : ( text : string , filename : string ) : Array < { text : string ; filename : string } > => {
20
- const blocks : Block [ ] = [ ] ;
18
+ preprocess : ( text : string , filename : string ) : Array < { text : string ; filename : string } | string > => {
19
+ const blocks : Array < Block | string > = [ ] ;
21
20
blocksMap . set ( filename , blocks ) ;
22
21
23
- // WORKAROUND: This restores the original filename for the block representing original code.
24
- // This allows to be compatible with other eslint plugins relying on filesystem
25
- // This is temporary, waiting for https://github.com/eslint/eslint/issues/11989
26
- const originalFileBlock = { text, filename : `/../../${ basename ( filename ) } ` } ;
27
-
28
22
if ( filename && text && RELEVANT_KEYWORDS . some ( keyword => text . includes ( keyword ) ) ) {
29
23
try {
30
24
const extractedDocuments = parseCode ( {
@@ -46,7 +40,7 @@ export function createGraphqlProcessor() {
46
40
} ) ;
47
41
}
48
42
49
- blocks . push ( originalFileBlock ) ;
43
+ blocks . push ( text ) ;
50
44
51
45
return blocks ;
52
46
}
@@ -56,15 +50,21 @@ export function createGraphqlProcessor() {
56
50
}
57
51
}
58
52
59
- return [ originalFileBlock ] ;
53
+ return [ text ] ;
60
54
} ,
61
55
postprocess : ( messageLists : any [ ] , filename : string ) : any [ ] => {
62
56
const blocks = blocksMap . get ( filename ) ;
63
57
64
58
if ( blocks && blocks . length > 0 ) {
65
59
for ( let i = 0 ; i < messageLists . length ; ++ i ) {
66
60
const messages = messageLists [ i ] ;
67
- const { lineOffset, offset } = blocks [ i ] ;
61
+ const block = blocks [ i ] ;
62
+
63
+ if ( typeof block === 'string' ) {
64
+ continue ;
65
+ }
66
+
67
+ const { lineOffset, offset } = block ;
68
68
69
69
for ( const message of messages ) {
70
70
message . line += lineOffset ;
0 commit comments