@@ -4,44 +4,38 @@ import debugFactory from 'debug';
4
4
import { convertToESTree , extractComments , extractTokens } from './estree-converter' ;
5
5
import { GraphQLESLintParseResult , ParserOptions } from './types' ;
6
6
import { getSchema } from './schema' ;
7
- import { getSiblingOperations } from './sibling-operations ' ;
7
+ import { getDocuments } from './documents ' ;
8
8
import { loadGraphQLConfig } from './graphql-config' ;
9
- import { getOnDiskFilepath } from './utils' ;
9
+ import { CWD , VIRTUAL_DOCUMENT_REGEX } from './utils' ;
10
10
11
11
const debug = debugFactory ( 'graphql-eslint:parser' ) ;
12
12
13
- debug ( 'cwd %o' , process . cwd ( ) ) ;
13
+ debug ( 'cwd %o' , CWD ) ;
14
14
15
15
export function parseForESLint ( code : string , options : ParserOptions ) : GraphQLESLintParseResult {
16
16
try {
17
17
const { filePath } = options ;
18
- const realFilepath = getOnDiskFilepath ( filePath ) ;
19
-
20
- const gqlConfig = loadGraphQLConfig ( options ) ;
21
- const projectForFile = realFilepath
22
- ? gqlConfig . getProjectForFile ( realFilepath )
23
- : gqlConfig . getDefault ( ) ;
24
-
25
- const schema = getSchema ( projectForFile , options ) ;
26
- const siblingOperations = getSiblingOperations ( projectForFile ) ;
27
-
18
+ // First parse code from file, in case of syntax error do not try load schema,
19
+ // documents or even graphql-config instance
28
20
const { document } = parseGraphQLSDL ( filePath , code , {
29
21
...options . graphQLParserOptions ,
30
22
noLocation : false ,
31
23
} ) ;
24
+ const gqlConfig = loadGraphQLConfig ( options ) ;
25
+ const realFilepath = filePath . replace ( VIRTUAL_DOCUMENT_REGEX , '' ) ;
26
+ const project = gqlConfig . getProjectForFile ( realFilepath ) ;
32
27
33
- const comments = extractComments ( document . loc ) ;
34
- const tokens = extractTokens ( filePath , code ) ;
28
+ const schema = getSchema ( project , options . schemaOptions ) ;
35
29
const rootTree = convertToESTree ( document , schema instanceof GraphQLSchema ? schema : null ) ;
36
30
37
31
return {
38
32
services : {
39
33
schema,
40
- siblingOperations,
34
+ siblingOperations : getDocuments ( project ) ,
41
35
} ,
42
36
ast : {
43
- comments,
44
- tokens,
37
+ comments : extractComments ( document . loc ) ,
38
+ tokens : extractTokens ( filePath , code ) ,
45
39
loc : rootTree . loc ,
46
40
range : rootTree . range as [ number , number ] ,
47
41
type : 'Program' ,
0 commit comments