Skip to content

Commit d46608e

Browse files
committed
fixes for parser
1 parent 6d60bb5 commit d46608e

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

packages/plugin/src/estree-parser/converter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ function hasTypeField<T extends ASTNode>(obj: any): obj is T & { readonly type:
2020
return obj && !!(obj as any).type;
2121
}
2222

23+
/**
24+
* Strips tokens information from `location` object - this is needed since it's created as linked list in GraphQL-JS,
25+
* causing eslint to fail on circular JSON
26+
* @param location
27+
*/
2328
function stripTokens(location: Location): Pick<Location, 'start' | 'end'> {
2429
return {
2530
end: location.end,

packages/plugin/src/parser.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { convertToESTree } from './estree-parser/converter';
22
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';
44
import { loadConfigSync, GraphQLProjectConfig } from 'graphql-config';
55
import { loadSchemaSync } from '@graphql-tools/load';
66
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
77
import { JsonFileLoader } from '@graphql-tools/json-file-loader';
88
import { UrlLoader } from '@graphql-tools/url-loader';
9-
import { Linter } from 'eslint';
9+
import { Linter, AST } from 'eslint';
1010
import { GraphQLESLintParseResult, ParserOptions } from './types';
1111

1212
const DEFAULT_CONFIG: ParserOptions = {
@@ -18,6 +18,33 @@ export function parse(code: string, options?: GraphQLParseOptions): Linter.ESLin
1818
return parseForESLint(code, options).ast;
1919
}
2020

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+
2148
export function parseForESLint(code: string, options?: ParserOptions): GraphQLESLintParseResult {
2249
try {
2350
const config = {
@@ -83,6 +110,7 @@ export function parseForESLint(code: string, options?: ParserOptions): GraphQLES
83110
});
84111

85112
const { rootTree, comments } = convertToESTree(graphqlAst.document, schema ? new TypeInfo(schema) : null);
113+
const tokens = extractTokens(code);
86114

87115
return {
88116
services: parserServices,
@@ -94,7 +122,7 @@ export function parseForESLint(code: string, options?: ParserOptions): GraphQLES
94122
comments,
95123
loc: rootTree.loc,
96124
range: rootTree.range as [number, number],
97-
tokens: [],
125+
tokens,
98126
},
99127
};
100128
} catch (e) {

packages/plugin/tests/__snapshots__/parser.spec.ts.snap

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,62 @@ Object {
163163
74,
164164
],
165165
"sourceType": "script",
166-
"tokens": Array [],
166+
"tokens": Array [
167+
Object {
168+
"loc": Object {
169+
"end": Object {
170+
"column": 7,
171+
"line": 2,
172+
},
173+
"start": Object {
174+
"column": 7,
175+
"line": 2,
176+
},
177+
},
178+
"range": Array [
179+
7,
180+
52,
181+
],
182+
"type": "BlockString",
183+
"value": "generic query placeholder",
184+
},
185+
Object {
186+
"loc": Object {
187+
"end": Object {
188+
"column": 7,
189+
"line": 5,
190+
},
191+
"start": Object {
192+
"column": 7,
193+
"line": 5,
194+
},
195+
},
196+
"range": Array [
197+
59,
198+
63,
199+
],
200+
"type": "Name",
201+
"value": "type",
202+
},
203+
Object {
204+
"loc": Object {
205+
"end": Object {
206+
"column": 12,
207+
"line": 5,
208+
},
209+
"start": Object {
210+
"column": 12,
211+
"line": 5,
212+
},
213+
},
214+
"range": Array [
215+
64,
216+
69,
217+
],
218+
"type": "Name",
219+
"value": "Query",
220+
},
221+
],
167222
"type": "Program",
168223
}
169224
`;

0 commit comments

Comments
 (0)