Skip to content

Commit 8bcaf8d

Browse files
committed
added parser tests
1 parent 7653e4c commit 8bcaf8d

File tree

3 files changed

+237
-2
lines changed

3 files changed

+237
-2
lines changed

packages/plugin/src/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ export function parse(code: string, options?: GraphQLParseOptions): Linter.ESLin
1818
return parseForESLint(code, options).ast;
1919
}
2020

21-
export function parseForESLint(code: string, options: ParserOptions): GraphQLESLintParseResult {
21+
export function parseForESLint(code: string, options?: ParserOptions): GraphQLESLintParseResult {
2222
try {
2323
const config = {
2424
...DEFAULT_CONFIG,
25-
...options,
25+
...(options || {}),
2626
};
2727

2828
let schema: GraphQLSchema = null;
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Parser parseForESLint() should return ast and tokens 1`] = `
4+
Object {
5+
"body": Array [
6+
Object {
7+
"definitions": Array [
8+
Object {
9+
"description": Object {
10+
"block": true,
11+
"gqlLocation": Object {
12+
"end": 52,
13+
"start": 7,
14+
},
15+
"kind": "StringValue",
16+
"leadingComments": Array [],
17+
"loc": Object {
18+
"end": Object {
19+
"column": 7,
20+
"line": 2,
21+
},
22+
"source": "
23+
\\"\\"\\"
24+
generic query placeholder
25+
\\"\\"\\"
26+
type Query
27+
",
28+
"start": Object {
29+
"column": 7,
30+
"line": 2,
31+
},
32+
},
33+
"range": Array [
34+
7,
35+
52,
36+
],
37+
"rawNode": [Function],
38+
"type": "StringValue",
39+
"typeInfo": [Function],
40+
"value": "generic query placeholder",
41+
},
42+
"directives": Array [],
43+
"fields": Array [],
44+
"gqlLocation": Object {
45+
"end": 69,
46+
"start": 7,
47+
},
48+
"interfaces": Array [],
49+
"kind": "ObjectTypeDefinition",
50+
"leadingComments": Array [
51+
Object {
52+
"type": "Block",
53+
"value": "generic query placeholder",
54+
},
55+
],
56+
"loc": Object {
57+
"end": Object {
58+
"column": 12,
59+
"line": 5,
60+
},
61+
"source": "
62+
\\"\\"\\"
63+
generic query placeholder
64+
\\"\\"\\"
65+
type Query
66+
",
67+
"start": Object {
68+
"column": 7,
69+
"line": 2,
70+
},
71+
},
72+
"name": Object {
73+
"gqlLocation": Object {
74+
"end": 69,
75+
"start": 64,
76+
},
77+
"kind": "Name",
78+
"leadingComments": Array [],
79+
"loc": Object {
80+
"end": Object {
81+
"column": 12,
82+
"line": 5,
83+
},
84+
"source": "
85+
\\"\\"\\"
86+
generic query placeholder
87+
\\"\\"\\"
88+
type Query
89+
",
90+
"start": Object {
91+
"column": 12,
92+
"line": 5,
93+
},
94+
},
95+
"range": Array [
96+
64,
97+
69,
98+
],
99+
"rawNode": [Function],
100+
"type": "Name",
101+
"typeInfo": [Function],
102+
"value": "Query",
103+
},
104+
"range": Array [
105+
7,
106+
69,
107+
],
108+
"rawNode": [Function],
109+
"type": "ObjectTypeDefinition",
110+
"typeInfo": [Function],
111+
},
112+
],
113+
"gqlLocation": Object {
114+
"end": 74,
115+
"start": 0,
116+
},
117+
"kind": "Document",
118+
"leadingComments": Array [],
119+
"loc": Object {
120+
"end": Object {
121+
"column": 5,
122+
"line": 6,
123+
},
124+
"source": "
125+
\\"\\"\\"
126+
generic query placeholder
127+
\\"\\"\\"
128+
type Query
129+
",
130+
"start": Object {
131+
"column": 0,
132+
"line": 0,
133+
},
134+
},
135+
"range": Array [
136+
0,
137+
74,
138+
],
139+
"rawNode": [Function],
140+
"type": "Document",
141+
"typeInfo": [Function],
142+
},
143+
],
144+
"comments": Array [],
145+
"loc": Object {
146+
"end": Object {
147+
"column": 5,
148+
"line": 6,
149+
},
150+
"source": "
151+
\\"\\"\\"
152+
generic query placeholder
153+
\\"\\"\\"
154+
type Query
155+
",
156+
"start": Object {
157+
"column": 0,
158+
"line": 0,
159+
},
160+
},
161+
"range": Array [
162+
0,
163+
74,
164+
],
165+
"sourceType": "script",
166+
"tokens": Array [],
167+
"type": "Program",
168+
}
169+
`;

packages/plugin/tests/parser.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { parseForESLint } from '../src/parser';
2+
3+
describe('Parser', () => {
4+
it('parseForESLint() should return ast and tokens', () => {
5+
const code = /* GraphQL */ `
6+
"""
7+
generic query placeholder
8+
"""
9+
type Query
10+
`;
11+
12+
const result = parseForESLint(code, { filePath: 'test.graphql' });
13+
expect(result.ast).toMatchSnapshot();
14+
expect(result.ast.tokens).toBeTruthy();
15+
});
16+
17+
it('should throw on invalid code', () => {
18+
const code = 'Hello World!';
19+
20+
expect(() => {
21+
parseForESLint(code);
22+
}).toThrow();
23+
});
24+
25+
it('should correctly preserve "type" property (as "gqlType" field)', () => {
26+
const code = /* GraphQL */ `
27+
query GetUser($userId: ID!) {
28+
user(id: $userId) {
29+
id
30+
}
31+
}
32+
`;
33+
34+
const result = parseForESLint(code, { filePath: 'test.graphql' });
35+
const field = (result.ast.body[0] as any).definitions[0].variableDefinitions[0];
36+
37+
expect(field.type).toBe('VariableDefinition');
38+
expect(field.gqlType.type).toBe('NonNullType');
39+
});
40+
41+
it('should provide type info when available', () => {
42+
const schema = /* GraphQL */ `
43+
type User {
44+
id: ID!
45+
}
46+
47+
type Query {
48+
user(id: ID!): User!
49+
}
50+
`;
51+
const code = /* GraphQL */ `
52+
query user($id: ID!) {
53+
user(id: $id) {
54+
id
55+
}
56+
}
57+
`;
58+
59+
const result = parseForESLint(code, { filePath: 'test.graphql', schema, skipGraphQLConfig: true });
60+
const selectionSet = (result.ast.body[0] as any).definitions[0].selectionSet;
61+
const typeInfo = selectionSet.typeInfo();
62+
expect(typeInfo).toBeDefined();
63+
expect(typeInfo.gqlType.name).toEqual('Query');
64+
expect(typeInfo.parentType.name).toEqual('Query');
65+
});
66+
});

0 commit comments

Comments
 (0)