From 7aa815790b712324d608edec6bb33744db12b853 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Sun, 31 Oct 2021 16:44:17 +0100 Subject: [PATCH] fix error report for `input-name` rule (#738) --- .changeset/strong-boxes-kiss.md | 5 +++ packages/plugin/src/rules/input-name.ts | 17 ++++---- .../__snapshots__/input-name.spec.ts.snap | 42 +++++++++---------- 3 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 .changeset/strong-boxes-kiss.md diff --git a/.changeset/strong-boxes-kiss.md b/.changeset/strong-boxes-kiss.md new file mode 100644 index 00000000000..9d681ec4f5a --- /dev/null +++ b/.changeset/strong-boxes-kiss.md @@ -0,0 +1,5 @@ +--- +'@graphql-eslint/eslint-plugin': patch +--- + +fix error report for `input-name` rule diff --git a/packages/plugin/src/rules/input-name.ts b/packages/plugin/src/rules/input-name.ts index a3338feee02..bd8576dffde 100644 --- a/packages/plugin/src/rules/input-name.ts +++ b/packages/plugin/src/rules/input-name.ts @@ -1,5 +1,5 @@ import { GraphQLESLintRule } from '../types'; -import { isMutationType, isQueryType } from '../utils'; +import { getLocation, isMutationType, isQueryType } from '../utils'; type InputNameRuleConfig = { checkInputType?: boolean; @@ -89,10 +89,11 @@ const rule: GraphQLESLintRule = { const listeners = { 'FieldDefinition > InputValueDefinition': node => { - if (node.name.value !== 'input' && shouldCheckType(node.parent.parent)) { + const name = node.name.value; + if (name !== 'input' && shouldCheckType(node.parent.parent)) { context.report({ - node: node.name, - message: `Input "${node.name.value}" should be called "input"`, + loc: getLocation(node.loc, name), + message: `Input "${name}" should be called "input"`, }); } }, @@ -111,14 +112,14 @@ const rule: GraphQLESLintRule = { const inputValueNode = findInputType(node); if (shouldCheckType(inputValueNode.parent.parent)) { const mutationName = `${inputValueNode.parent.name.value}Input`; - + const name = node.name.value; if ( (options.caseSensitiveInputType && node.name.value !== mutationName) || - node.name.value.toLowerCase() !== mutationName.toLowerCase() + name.toLowerCase() !== mutationName.toLowerCase() ) { context.report({ - node, - message: `InputType "${node.name.value}" name should be "${mutationName}"`, + loc: getLocation(node.loc, name), + message: `InputType "${name}" name should be "${mutationName}"`, }); } } diff --git a/packages/plugin/tests/__snapshots__/input-name.spec.ts.snap b/packages/plugin/tests/__snapshots__/input-name.spec.ts.snap index 265cb714ed8..7f904cd63d8 100644 --- a/packages/plugin/tests/__snapshots__/input-name.spec.ts.snap +++ b/packages/plugin/tests/__snapshots__/input-name.spec.ts.snap @@ -2,105 +2,105 @@ exports[` 1`] = ` > 1 | type Mutation { SetMessage(message: String): String } - | ^ Input "message" should be called "input" + | ^^^^^^^ Input "message" should be called "input" `; exports[` 2`] = ` > 1 | type Mutation { SetMessage(message: String): String } - | ^ InputType "String" name should be "SetMessageInput" + | ^^^^^^ InputType "String" name should be "SetMessageInput" `; exports[` 3`] = ` > 1 | type Mutation { SetMessage(input: String): String } - | ^ InputType "String" name should be "SetMessageInput" + | ^^^^^^ InputType "String" name should be "SetMessageInput" `; exports[` 4`] = ` > 1 | type Mutation { SetMessage(hello: SetMessageInput): String } - | ^ Input "hello" should be called "input" + | ^^^^^ Input "hello" should be called "input" `; exports[` 5`] = ` > 1 | type Mutation { userCreate(record: CreateOneUserInput!): CreateOneUserPayload } - | ^ Input "record" should be called "input" + | ^^^^^^ Input "record" should be called "input" `; exports[` 6`] = ` > 1 | type Mutation { userCreate(record: CreateOneUserInput!): CreateOneUserPayload } - | ^ InputType "CreateOneUserInput" name should be "userCreateInput" + | ^^^^^^^^^^^^^^^^^^ InputType "CreateOneUserInput" name should be "userCreateInput" `; exports[` 7`] = ` > 1 | type Mutation { userCreate(record: [CreateOneUserInput]!): CreateOneUserPayload } - | ^ Input "record" should be called "input" + | ^^^^^^ Input "record" should be called "input" `; exports[` 8`] = ` > 1 | type Mutation { userCreate(record: [CreateOneUserInput]!): CreateOneUserPayload } - | ^ InputType "CreateOneUserInput" name should be "userCreateInput" + | ^^^^^^^^^^^^^^^^^^ InputType "CreateOneUserInput" name should be "userCreateInput" `; exports[` 9`] = ` > 1 | type Mutation { userCreate(record: [CreateOneUserInput!]!): CreateOneUserPayload } - | ^ Input "record" should be called "input" + | ^^^^^^ Input "record" should be called "input" `; exports[` 10`] = ` > 1 | type Mutation { userCreate(record: [CreateOneUserInput!]!): CreateOneUserPayload } - | ^ InputType "CreateOneUserInput" name should be "userCreateInput" + | ^^^^^^^^^^^^^^^^^^ InputType "CreateOneUserInput" name should be "userCreateInput" `; exports[` 11`] = ` > 1 | type Mutation { userCreate(record: [CreateOneUserInput!]): CreateOneUserPayload } - | ^ Input "record" should be called "input" + | ^^^^^^ Input "record" should be called "input" `; exports[` 12`] = ` > 1 | type Mutation { userCreate(record: [CreateOneUserInput!]): CreateOneUserPayload } - | ^ InputType "CreateOneUserInput" name should be "userCreateInput" + | ^^^^^^^^^^^^^^^^^^ InputType "CreateOneUserInput" name should be "userCreateInput" `; exports[` 13`] = ` > 1 | type Mutation { userCreate(record: String, test: String): String } - | ^ Input "record" should be called "input" + | ^^^^^^ Input "record" should be called "input" `; exports[` 14`] = ` > 1 | type Mutation { userCreate(record: String, test: String): String } - | ^ InputType "String" name should be "userCreateInput" + | ^^^^^^ InputType "String" name should be "userCreateInput" `; exports[` 15`] = ` > 1 | type Mutation { userCreate(record: String, test: String): String } - | ^ Input "test" should be called "input" + | ^^^^ Input "test" should be called "input" `; exports[` 16`] = ` > 1 | type Mutation { userCreate(record: String, test: String): String } - | ^ InputType "String" name should be "userCreateInput" + | ^^^^^^ InputType "String" name should be "userCreateInput" `; exports[` 17`] = ` > 1 | type Mutation { userCreate(record: String, test: String): String } - | ^ Input "record" should be called "input" + | ^^^^^^ Input "record" should be called "input" `; exports[` 18`] = ` > 1 | type Mutation { userCreate(record: String, test: String): String } - | ^ Input "test" should be called "input" + | ^^^^ Input "test" should be called "input" `; exports[` 19`] = ` > 1 | type Mutation { userCreate(input: String): String } - | ^ InputType "String" name should be "userCreateInput" + | ^^^^^^ InputType "String" name should be "userCreateInput" `; exports[` 20`] = ` > 1 | type Mutation { userCreate(input: UserCreateInput): String } - | ^ InputType "UserCreateInput" name should be "userCreateInput" + | ^^^^^^^^^^^^^^^ InputType "UserCreateInput" name should be "userCreateInput" `; exports[` 21`] = ` > 1 | type Query { getUser(input: GetUserInput): String } - | ^ InputType "GetUserInput" name should be "getUserInput" + | ^^^^^^^^^^^^ InputType "GetUserInput" name should be "getUserInput" `;