Skip to content

Commit 86b3709

Browse files
author
Dimitri POSTOLOV
authored
fix error report for no-unused-fields and no-unreachable-types rule (#743)
1 parent ebab010 commit 86b3709

File tree

5 files changed

+51
-69
lines changed

5 files changed

+51
-69
lines changed

.changeset/breezy-vans-buy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
fix error report for `no-unused-fields` and `no-unreachable-types` rule

packages/plugin/src/rules/no-unreachable-types.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { Kind } from 'graphql';
12
import { GraphQLESLintRule } from '../types';
2-
import { requireReachableTypesFromContext } from '../utils';
3+
import { getLocation, requireReachableTypesFromContext } from '../utils';
34

45
const UNREACHABLE_TYPE = 'UNREACHABLE_TYPE';
56
const RULE_NAME = 'no-unreachable-types';
@@ -55,7 +56,7 @@ const rule: GraphQLESLintRule = {
5556

5657
if (!reachableTypes.has(typeName)) {
5758
context.report({
58-
node,
59+
loc: getLocation(node.name.loc, typeName, { offsetStart: node.kind === Kind.DIRECTIVE_DEFINITION ? 2 : 1 }),
5960
messageId: UNREACHABLE_TYPE,
6061
data: { typeName },
6162
fix: fixer => fixer.remove(node),

packages/plugin/src/rules/no-unused-fields.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GraphQLESLintRule } from '../types';
2-
import { requireUsedFieldsFromContext } from '../utils';
2+
import { getLocation, requireUsedFieldsFromContext } from '../utils';
33

44
const UNUSED_FIELD = 'UNUSED_FIELD';
55
const RULE_NAME = 'no-unused-fields';
@@ -77,7 +77,7 @@ const rule: GraphQLESLintRule = {
7777
}
7878

7979
context.report({
80-
node,
80+
loc: getLocation(node.loc, fieldName),
8181
messageId: UNUSED_FIELD,
8282
data: { fieldName },
8383
fix(fixer) {

packages/plugin/tests/__snapshots__/no-unreachable-types.spec.ts.snap

+39-63
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ exports[` 1`] = `
77
4 | }
88
5 |
99
> 6 | interface Node {
10-
| ^^^^^^^^^^^^^^^
11-
> 7 | id: ID!
12-
| ^^^^^^^^^^^^^^^^^
13-
> 8 | }
14-
| ^^^^^^^^^^ Type "Node" is unreachable
10+
| ^^^^ Type "Node" is unreachable
11+
7 | id: ID!
12+
8 | }
1513
9 |
1614
10 | interface AnotherNode {
1715
11 | createdAt: String
@@ -45,13 +43,10 @@ exports[` 2`] = `
4543
12 | }
4644
13 |
4745
> 14 | interface User implements Node {
48-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
49-
> 15 | id: ID!
50-
| ^^^^^^^^^^^^^^^^^
51-
> 16 | name: String
52-
| ^^^^^^^^^^^^^^^^^
53-
> 17 | }
54-
| ^^^^^^^^^^ Type "User" is unreachable
46+
| ^^^^ Type "User" is unreachable
47+
15 | id: ID!
48+
16 | name: String
49+
17 | }
5550
18 |
5651
19 | type SuperUser implements User & Node {
5752
20 | id: ID!
@@ -81,23 +76,19 @@ exports[` 3`] = `
8176
17 | }
8277
18 |
8378
> 19 | type SuperUser implements User & Node {
84-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85-
> 20 | id: ID!
86-
| ^^^^^^^^^^^^^^^^^
87-
> 21 | name: String
88-
| ^^^^^^^^^^^^^^^^^
89-
> 22 | address: String
90-
| ^^^^^^^^^^^^^^^^^
91-
> 23 | }
92-
| ^^^^^^^^^^ Type "SuperUser" is unreachable
79+
| ^^^^^^^^^ Type "SuperUser" is unreachable
80+
20 | id: ID!
81+
21 | name: String
82+
22 | address: String
83+
23 | }
9384
24 |
9485
`;
9586

9687
exports[` 4`] = `
9788
1 |
9889
2 | # ScalarTypeDefinition
9990
> 3 | scalar DateTime
100-
| ^^^^^^^ Type "DateTime" is unreachable
91+
| ^^^^^^^^ Type "DateTime" is unreachable
10192
4 |
10293
5 | # EnumTypeDefinition
10394
6 | enum Role {
@@ -137,13 +128,10 @@ exports[` 5`] = `
137128
4 |
138129
5 | # EnumTypeDefinition
139130
> 6 | enum Role {
140-
| ^^^^^^^^^^
141-
> 7 | ADMIN
142-
| ^^^^^^^^^^^^^^^
143-
> 8 | USER
144-
| ^^^^^^^^^^^^^^^
145-
> 9 | }
146-
| ^^^^^^^^^^ Type "Role" is unreachable
131+
| ^^^^ Type "Role" is unreachable
132+
7 | ADMIN
133+
8 | USER
134+
9 | }
147135
10 |
148136
11 | # DirectiveDefinition
149137
12 | directive @auth(role: [String!]!) on FIELD_DEFINITION
@@ -183,7 +171,7 @@ exports[` 6`] = `
183171
10 |
184172
11 | # DirectiveDefinition
185173
> 12 | directive @auth(role: [String!]!) on FIELD_DEFINITION
186-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type "auth" is unreachable
174+
| ^^^^^ Type "auth" is unreachable
187175
13 |
188176
14 | # UnionTypeDefinition
189177
15 | union Union = String | Boolean
@@ -223,7 +211,7 @@ exports[` 7`] = `
223211
13 |
224212
14 | # UnionTypeDefinition
225213
> 15 | union Union = String | Boolean
226-
| ^^^^^^^^^^^^^^^^^^^^^^^ Type "Union" is unreachable
214+
| ^^^^^ Type "Union" is unreachable
227215
16 |
228216
17 | # InputObjectTypeDefinition
229217
18 | input UsersFilter {
@@ -263,11 +251,9 @@ exports[` 8`] = `
263251
16 |
264252
17 | # InputObjectTypeDefinition
265253
> 18 | input UsersFilter {
266-
| ^^^^^^^^^^^^^^^^^^
267-
> 19 | limit: Int
268-
| ^^^^^^^^^^^^^^^^^^^^
269-
> 20 | }
270-
| ^^^^^^^^^^ Type "UsersFilter" is unreachable
254+
| ^^^^^^^^^^^ Type "UsersFilter" is unreachable
255+
19 | limit: Int
256+
20 | }
271257
21 |
272258
22 | # InterfaceTypeDefinition
273259
23 | interface Address {
@@ -307,11 +293,9 @@ exports[` 9`] = `
307293
21 |
308294
22 | # InterfaceTypeDefinition
309295
> 23 | interface Address {
310-
| ^^^^^^^^^^^^^^^^^^
311-
> 24 | city: String
312-
| ^^^^^^^^^^^^^^^^^^^^^^
313-
> 25 | }
314-
| ^^^^^^^^^^ Type "Address" is unreachable
296+
| ^^^^^^^ Type "Address" is unreachable
297+
24 | city: String
298+
25 | }
315299
26 |
316300
27 | # ObjectTypeDefinition
317301
28 | type User implements Address {
@@ -351,11 +335,9 @@ exports[` 10`] = `
351335
26 |
352336
27 | # ObjectTypeDefinition
353337
> 28 | type User implements Address {
354-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
355-
> 29 | city: String
356-
| ^^^^^^^^^^^^^^^^^^^^^^
357-
> 30 | }
358-
| ^^^^^^^^^^ Type "User" is unreachable
338+
| ^^^^ Type "User" is unreachable
339+
29 | city: String
340+
30 | }
359341
31 |
360342
32 | type Query
361343
33 |
@@ -381,18 +363,16 @@ exports[` 11`] = `
381363
17 | }
382364
18 |
383365
> 19 | scalar DateTime
384-
| ^^^^^^^ Type "DateTime" is unreachable
366+
| ^^^^^^^^ Type "DateTime" is unreachable
385367
20 |
386368
`;
387369

388370
exports[` 12`] = `
389371
1 |
390372
> 2 | interface User {
391-
| ^^^^^^^^^^^^^^^
392-
> 3 | id: String
393-
| ^^^^^^^^^^^^^^^^^^^^
394-
> 4 | }
395-
| ^^^^^^^^^^ Type "User" is unreachable
373+
| ^^^^ Type "User" is unreachable
374+
3 | id: String
375+
4 | }
396376
5 |
397377
6 | interface AnotherUser {
398378
7 | createdAt: String
@@ -424,11 +404,9 @@ exports[` 13`] = `
424404
8 | }
425405
9 |
426406
> 10 | type SuperUser implements User {
427-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
428-
> 11 | id: String
429-
| ^^^^^^^^^^^^^^^^^^^^
430-
> 12 | }
431-
| ^^^^^^^^^^ Type "SuperUser" is unreachable
407+
| ^^^^^^^^^ Type "SuperUser" is unreachable
408+
11 | id: String
409+
12 | }
432410
13 |
433411
14 | # ObjectTypeExtension
434412
15 | extend type SuperUser {
@@ -457,11 +435,9 @@ exports[` 14`] = `
457435
13 |
458436
14 | # ObjectTypeExtension
459437
> 15 | extend type SuperUser {
460-
| ^^^^^^^^^^^^^^^^^^^^^^
461-
> 16 | detail: String
462-
| ^^^^^^^^^^^^^^^^^^^^^^^^
463-
> 17 | }
464-
| ^^^^^^^^^^ Type "SuperUser" is unreachable
438+
| ^^^^^^^^^ Type "SuperUser" is unreachable
439+
16 | detail: String
440+
17 | }
465441
18 |
466442
19 | type Query {
467443
20 | user: AnotherUser!
@@ -491,6 +467,6 @@ exports[` 15`] = `
491467
19 | }
492468
20 |
493469
> 21 | scalar DateTime
494-
| ^^^^^^^ Type "DateTime" is unreachable
470+
| ^^^^^^^^ Type "DateTime" is unreachable
495471
22 |
496472
`;

packages/plugin/tests/__snapshots__/no-unused-fields.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports[` 1`] = `
66
3 | type User {
77
4 | id: ID!
88
> 5 | firstName: String
9-
| ^^^^^^^^^^^ Field "firstName" is unused
9+
| ^^^^^^^^^ Field "firstName" is unused
1010
6 | }
1111
7 |
1212
`;
@@ -20,7 +20,7 @@ exports[` 2`] = `
2020
6 |
2121
7 | type Mutation {
2222
> 8 | deleteUser(id: ID!): User
23-
| ^^^^^^^^^^^^^^^^^^^^^ Field "deleteUser" is unused
23+
| ^^^^^^^^^^ Field "deleteUser" is unused
2424
9 | }
2525
10 |
2626
`;

0 commit comments

Comments
 (0)