diff --git a/.changeset/breezy-vans-buy.md b/.changeset/breezy-vans-buy.md new file mode 100644 index 00000000000..32024bfd1df --- /dev/null +++ b/.changeset/breezy-vans-buy.md @@ -0,0 +1,5 @@ +--- +'@graphql-eslint/eslint-plugin': patch +--- + +fix error report for `no-unused-fields` and `no-unreachable-types` rule diff --git a/packages/plugin/src/rules/no-unreachable-types.ts b/packages/plugin/src/rules/no-unreachable-types.ts index 6c0a006c97d..7b7389509ff 100644 --- a/packages/plugin/src/rules/no-unreachable-types.ts +++ b/packages/plugin/src/rules/no-unreachable-types.ts @@ -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'; @@ -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), diff --git a/packages/plugin/src/rules/no-unused-fields.ts b/packages/plugin/src/rules/no-unused-fields.ts index dde5a84a342..24a12cdf2a2 100644 --- a/packages/plugin/src/rules/no-unused-fields.ts +++ b/packages/plugin/src/rules/no-unused-fields.ts @@ -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'; @@ -77,7 +77,7 @@ const rule: GraphQLESLintRule = { } context.report({ - node, + loc: getLocation(node.loc, fieldName), messageId: UNUSED_FIELD, data: { fieldName }, fix(fixer) { diff --git a/packages/plugin/tests/__snapshots__/no-unreachable-types.spec.ts.snap b/packages/plugin/tests/__snapshots__/no-unreachable-types.spec.ts.snap index b9092a381b9..7366c0dd6c2 100644 --- a/packages/plugin/tests/__snapshots__/no-unreachable-types.spec.ts.snap +++ b/packages/plugin/tests/__snapshots__/no-unreachable-types.spec.ts.snap @@ -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 @@ -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! @@ -81,15 +76,11 @@ 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 | `; @@ -97,7 +88,7 @@ exports[` 4`] = ` 1 | 2 | # ScalarTypeDefinition > 3 | scalar DateTime - | ^^^^^^^ Type "DateTime" is unreachable + | ^^^^^^^^ Type "DateTime" is unreachable 4 | 5 | # EnumTypeDefinition 6 | enum Role { @@ -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 @@ -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 @@ -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 { @@ -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 { @@ -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 { @@ -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 | @@ -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 @@ -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 { @@ -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! @@ -491,6 +467,6 @@ exports[` 15`] = ` 19 | } 20 | > 21 | scalar DateTime - | ^^^^^^^ Type "DateTime" is unreachable + | ^^^^^^^^ Type "DateTime" is unreachable 22 | `; diff --git a/packages/plugin/tests/__snapshots__/no-unused-fields.spec.ts.snap b/packages/plugin/tests/__snapshots__/no-unused-fields.spec.ts.snap index c5d88a3b6c2..cf73c0c80de 100644 --- a/packages/plugin/tests/__snapshots__/no-unused-fields.spec.ts.snap +++ b/packages/plugin/tests/__snapshots__/no-unused-fields.spec.ts.snap @@ -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 | `; @@ -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 | `;