1
1
import { convertToESTree } from './estree-parser/converter' ;
2
2
import { GraphQLParseOptions , parseGraphQLSDL } from '@graphql-tools/utils' ;
3
- import { buildSchema , GraphQLError , GraphQLSchema , TypeInfo } from 'graphql' ;
3
+ import { buildSchema , GraphQLError , GraphQLSchema , Source , Lexer , TypeInfo } from 'graphql' ;
4
4
import { loadConfigSync , GraphQLProjectConfig } from 'graphql-config' ;
5
5
import { loadSchemaSync } from '@graphql-tools/load' ;
6
6
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader' ;
7
7
import { JsonFileLoader } from '@graphql-tools/json-file-loader' ;
8
8
import { UrlLoader } from '@graphql-tools/url-loader' ;
9
- import { Linter } from 'eslint' ;
9
+ import { Linter , AST } from 'eslint' ;
10
10
import { GraphQLESLintParseResult , ParserOptions } from './types' ;
11
11
12
12
const DEFAULT_CONFIG : ParserOptions = {
@@ -18,6 +18,33 @@ export function parse(code: string, options?: GraphQLParseOptions): Linter.ESLin
18
18
return parseForESLint ( code , options ) . ast ;
19
19
}
20
20
21
+ export function extractTokens ( source : string ) : AST . Token [ ] {
22
+ const lexer = new Lexer ( new Source ( source ) ) ;
23
+ const tokens : AST . Token [ ] = [ ] ;
24
+ let token = lexer . advance ( ) ;
25
+
26
+ while ( token && token . kind !== '<EOF>' ) {
27
+ tokens . push ( {
28
+ type : token . kind as any ,
29
+ loc : {
30
+ start : {
31
+ line : token . line ,
32
+ column : token . column ,
33
+ } ,
34
+ end : {
35
+ line : token . line ,
36
+ column : token . column ,
37
+ } ,
38
+ } ,
39
+ value : token . value ,
40
+ range : [ token . start , token . end ] ,
41
+ } ) ;
42
+ token = lexer . advance ( ) ;
43
+ }
44
+
45
+ return tokens ;
46
+ }
47
+
21
48
export function parseForESLint ( code : string , options ?: ParserOptions ) : GraphQLESLintParseResult {
22
49
try {
23
50
const config = {
@@ -83,6 +110,7 @@ export function parseForESLint(code: string, options?: ParserOptions): GraphQLES
83
110
} ) ;
84
111
85
112
const { rootTree, comments } = convertToESTree ( graphqlAst . document , schema ? new TypeInfo ( schema ) : null ) ;
113
+ const tokens = extractTokens ( code ) ;
86
114
87
115
return {
88
116
services : parserServices ,
@@ -94,7 +122,7 @@ export function parseForESLint(code: string, options?: ParserOptions): GraphQLES
94
122
comments,
95
123
loc : rootTree . loc ,
96
124
range : rootTree . range as [ number , number ] ,
97
- tokens : [ ] ,
125
+ tokens,
98
126
} ,
99
127
} ;
100
128
} catch ( e ) {
0 commit comments