Skip to content

Commit

Permalink
fix error report for no-unused-fields and no-unreachable-types ru…
Browse files Browse the repository at this point in the history
…le (#743)
  • Loading branch information
Dimitri POSTOLOV authored Oct 31, 2021
1 parent ebab010 commit 86b3709
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-vans-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix error report for `no-unused-fields` and `no-unreachable-types` rule
5 changes: 3 additions & 2 deletions packages/plugin/src/rules/no-unreachable-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Kind } from 'graphql';
import { GraphQLESLintRule } from '../types';
import { requireReachableTypesFromContext } from '../utils';
import { getLocation, requireReachableTypesFromContext } from '../utils';

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

if (!reachableTypes.has(typeName)) {
context.report({
node,
loc: getLocation(node.name.loc, typeName, { offsetStart: node.kind === Kind.DIRECTIVE_DEFINITION ? 2 : 1 }),
messageId: UNREACHABLE_TYPE,
data: { typeName },
fix: fixer => fixer.remove(node),
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/rules/no-unused-fields.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLESLintRule } from '../types';
import { requireUsedFieldsFromContext } from '../utils';
import { getLocation, requireUsedFieldsFromContext } from '../utils';

const UNUSED_FIELD = 'UNUSED_FIELD';
const RULE_NAME = 'no-unused-fields';
Expand Down Expand Up @@ -77,7 +77,7 @@ const rule: GraphQLESLintRule = {
}

context.report({
node,
loc: getLocation(node.loc, fieldName),
messageId: UNUSED_FIELD,
data: { fieldName },
fix(fixer) {
Expand Down
102 changes: 39 additions & 63 deletions packages/plugin/tests/__snapshots__/no-unreachable-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ exports[` 1`] = `
4 | }
5 |
> 6 | interface Node {
| ^^^^^^^^^^^^^^^
> 7 | id: ID!
| ^^^^^^^^^^^^^^^^^
> 8 | }
| ^^^^^^^^^^ Type "Node" is unreachable
| ^^^^ Type "Node" is unreachable
7 | id: ID!
8 | }
9 |
10 | interface AnotherNode {
11 | createdAt: String
Expand Down Expand Up @@ -45,13 +43,10 @@ exports[` 2`] = `
12 | }
13 |
> 14 | interface User implements Node {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 15 | id: ID!
| ^^^^^^^^^^^^^^^^^
> 16 | name: String
| ^^^^^^^^^^^^^^^^^
> 17 | }
| ^^^^^^^^^^ Type "User" is unreachable
| ^^^^ Type "User" is unreachable
15 | id: ID!
16 | name: String
17 | }
18 |
19 | type SuperUser implements User & Node {
20 | id: ID!
Expand Down Expand Up @@ -81,23 +76,19 @@ exports[` 3`] = `
17 | }
18 |
> 19 | type SuperUser implements User & Node {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 20 | id: ID!
| ^^^^^^^^^^^^^^^^^
> 21 | name: String
| ^^^^^^^^^^^^^^^^^
> 22 | address: String
| ^^^^^^^^^^^^^^^^^
> 23 | }
| ^^^^^^^^^^ Type "SuperUser" is unreachable
| ^^^^^^^^^ Type "SuperUser" is unreachable
20 | id: ID!
21 | name: String
22 | address: String
23 | }
24 |
`;

exports[` 4`] = `
1 |
2 | # ScalarTypeDefinition
> 3 | scalar DateTime
| ^^^^^^^ Type "DateTime" is unreachable
| ^^^^^^^^ Type "DateTime" is unreachable
4 |
5 | # EnumTypeDefinition
6 | enum Role {
Expand Down Expand Up @@ -137,13 +128,10 @@ exports[` 5`] = `
4 |
5 | # EnumTypeDefinition
> 6 | enum Role {
| ^^^^^^^^^^
> 7 | ADMIN
| ^^^^^^^^^^^^^^^
> 8 | USER
| ^^^^^^^^^^^^^^^
> 9 | }
| ^^^^^^^^^^ Type "Role" is unreachable
| ^^^^ Type "Role" is unreachable
7 | ADMIN
8 | USER
9 | }
10 |
11 | # DirectiveDefinition
12 | directive @auth(role: [String!]!) on FIELD_DEFINITION
Expand Down Expand Up @@ -183,7 +171,7 @@ exports[` 6`] = `
10 |
11 | # DirectiveDefinition
> 12 | directive @auth(role: [String!]!) on FIELD_DEFINITION
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type "auth" is unreachable
| ^^^^^ Type "auth" is unreachable
13 |
14 | # UnionTypeDefinition
15 | union Union = String | Boolean
Expand Down Expand Up @@ -223,7 +211,7 @@ exports[` 7`] = `
13 |
14 | # UnionTypeDefinition
> 15 | union Union = String | Boolean
| ^^^^^^^^^^^^^^^^^^^^^^^ Type "Union" is unreachable
| ^^^^^ Type "Union" is unreachable
16 |
17 | # InputObjectTypeDefinition
18 | input UsersFilter {
Expand Down Expand Up @@ -263,11 +251,9 @@ exports[` 8`] = `
16 |
17 | # InputObjectTypeDefinition
> 18 | input UsersFilter {
| ^^^^^^^^^^^^^^^^^^
> 19 | limit: Int
| ^^^^^^^^^^^^^^^^^^^^
> 20 | }
| ^^^^^^^^^^ Type "UsersFilter" is unreachable
| ^^^^^^^^^^^ Type "UsersFilter" is unreachable
19 | limit: Int
20 | }
21 |
22 | # InterfaceTypeDefinition
23 | interface Address {
Expand Down Expand Up @@ -307,11 +293,9 @@ exports[` 9`] = `
21 |
22 | # InterfaceTypeDefinition
> 23 | interface Address {
| ^^^^^^^^^^^^^^^^^^
> 24 | city: String
| ^^^^^^^^^^^^^^^^^^^^^^
> 25 | }
| ^^^^^^^^^^ Type "Address" is unreachable
| ^^^^^^^ Type "Address" is unreachable
24 | city: String
25 | }
26 |
27 | # ObjectTypeDefinition
28 | type User implements Address {
Expand Down Expand Up @@ -351,11 +335,9 @@ exports[` 10`] = `
26 |
27 | # ObjectTypeDefinition
> 28 | type User implements Address {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 29 | city: String
| ^^^^^^^^^^^^^^^^^^^^^^
> 30 | }
| ^^^^^^^^^^ Type "User" is unreachable
| ^^^^ Type "User" is unreachable
29 | city: String
30 | }
31 |
32 | type Query
33 |
Expand All @@ -381,18 +363,16 @@ exports[` 11`] = `
17 | }
18 |
> 19 | scalar DateTime
| ^^^^^^^ Type "DateTime" is unreachable
| ^^^^^^^^ Type "DateTime" is unreachable
20 |
`;

exports[` 12`] = `
1 |
> 2 | interface User {
| ^^^^^^^^^^^^^^^
> 3 | id: String
| ^^^^^^^^^^^^^^^^^^^^
> 4 | }
| ^^^^^^^^^^ Type "User" is unreachable
| ^^^^ Type "User" is unreachable
3 | id: String
4 | }
5 |
6 | interface AnotherUser {
7 | createdAt: String
Expand Down Expand Up @@ -424,11 +404,9 @@ exports[` 13`] = `
8 | }
9 |
> 10 | type SuperUser implements User {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 11 | id: String
| ^^^^^^^^^^^^^^^^^^^^
> 12 | }
| ^^^^^^^^^^ Type "SuperUser" is unreachable
| ^^^^^^^^^ Type "SuperUser" is unreachable
11 | id: String
12 | }
13 |
14 | # ObjectTypeExtension
15 | extend type SuperUser {
Expand Down Expand Up @@ -457,11 +435,9 @@ exports[` 14`] = `
13 |
14 | # ObjectTypeExtension
> 15 | extend type SuperUser {
| ^^^^^^^^^^^^^^^^^^^^^^
> 16 | detail: String
| ^^^^^^^^^^^^^^^^^^^^^^^^
> 17 | }
| ^^^^^^^^^^ Type "SuperUser" is unreachable
| ^^^^^^^^^ Type "SuperUser" is unreachable
16 | detail: String
17 | }
18 |
19 | type Query {
20 | user: AnotherUser!
Expand Down Expand Up @@ -491,6 +467,6 @@ exports[` 15`] = `
19 | }
20 |
> 21 | scalar DateTime
| ^^^^^^^ Type "DateTime" is unreachable
| ^^^^^^^^ Type "DateTime" is unreachable
22 |
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[` 1`] = `
3 | type User {
4 | id: ID!
> 5 | firstName: String
| ^^^^^^^^^^^ Field "firstName" is unused
| ^^^^^^^^^ Field "firstName" is unused
6 | }
7 |
`;
Expand All @@ -20,7 +20,7 @@ exports[` 2`] = `
6 |
7 | type Mutation {
> 8 | deleteUser(id: ID!): User
| ^^^^^^^^^^^^^^^^^^^^^ Field "deleteUser" is unused
| ^^^^^^^^^^ Field "deleteUser" is unused
9 | }
10 |
`;

0 comments on commit 86b3709

Please sign in to comment.