Skip to content

Commit a285de3

Browse files
author
Dimitri POSTOLOV
authored
fix error report for require-deprecation-reason and require-field-of-type-query-in-mutation-result rule (#745)
1 parent b36a32c commit a285de3

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

.changeset/yellow-phones-drive.md

Lines changed: 5 additions & 0 deletions
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 `require-deprecation-reason` and `require-field-of-type-query-in-mutation-result` rule

packages/plugin/src/rules/require-deprecation-reason.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GraphQLESLintRule } from '../types';
22
import { valueFromNode } from '../estree-parser/utils';
3+
import { getLocation } from '../utils';
34

45
const rule: GraphQLESLintRule = {
56
meta: {
@@ -40,18 +41,16 @@ const rule: GraphQLESLintRule = {
4041
},
4142
create(context) {
4243
return {
43-
Directive(node) {
44-
if (node && node.name && node.name.value === 'deprecated') {
45-
const args = node.arguments || [];
46-
const reasonArg = args.find(arg => arg.name && arg.name.value === 'reason');
47-
const value = reasonArg ? String(valueFromNode(reasonArg.value) || '').trim() : null;
44+
'Directive[name.value=deprecated]'(node) {
45+
const args = node.arguments || [];
46+
const reasonArg = args.find(arg => arg.name && arg.name.value === 'reason');
47+
const value = reasonArg ? String(valueFromNode(reasonArg.value) || '').trim() : null;
4848

49-
if (!value) {
50-
context.report({
51-
node: node.name,
52-
message: 'Directive "@deprecated" must have a reason!',
53-
});
54-
}
49+
if (!value) {
50+
context.report({
51+
loc: getLocation(node.loc, node.name.value, { offsetEnd: 0 }),
52+
message: 'Directive "@deprecated" must have a reason!',
53+
});
5554
}
5655
},
5756
};

packages/plugin/src/rules/require-field-of-type-query-in-mutation-result.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Kind, FieldDefinitionNode, isObjectType } from 'graphql';
2-
import { requireGraphQLSchemaFromContext, getTypeName } from '../utils';
2+
import { requireGraphQLSchemaFromContext, getTypeName, getLocation } from '../utils';
33
import { GraphQLESLintRule } from '../types';
44
import { GraphQLESTreeNode } from '../estree-parser';
55

@@ -54,20 +54,24 @@ const rule: GraphQLESLintRule = {
5454
if (!mutationType || !queryType) {
5555
return {};
5656
}
57-
const selector = `:matches(${Kind.OBJECT_TYPE_DEFINITION}, ${Kind.OBJECT_TYPE_EXTENSION})[name.value=${mutationType.name}] > ${Kind.FIELD_DEFINITION}`;
57+
const selector = [
58+
`:matches(${Kind.OBJECT_TYPE_DEFINITION}, ${Kind.OBJECT_TYPE_EXTENSION})[name.value=${mutationType.name}]`,
59+
'>',
60+
Kind.FIELD_DEFINITION,
61+
Kind.NAMED_TYPE,
62+
].join(' ');
5863

5964
return {
6065
[selector](node: GraphQLESTreeNode<FieldDefinitionNode>) {
61-
const rawNode = node.rawNode();
62-
const typeName = getTypeName(rawNode);
66+
const typeName = node.name.value;
6367
const graphQLType = schema.getType(typeName);
6468

6569
if (isObjectType(graphQLType)) {
6670
const { fields } = graphQLType.astNode;
6771
const hasQueryType = fields.some(field => getTypeName(field) === queryType.name);
6872
if (!hasQueryType) {
6973
context.report({
70-
node,
74+
loc: getLocation(node.loc, typeName),
7175
message: `Mutation result type "${graphQLType.name}" must contain field of type "${queryType.name}".`,
7276
});
7377
}

packages/plugin/tests/__snapshots__/require-deprecation-reason.spec.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[` 1`] = `
44
1 |
55
2 | type A {
66
> 3 | deprecatedWithoutReason: String @deprecated
7-
| ^ Directive "@deprecated" must have a reason!
7+
| ^^^^^^^^^^^ Directive "@deprecated" must have a reason!
88
4 | deprecatedWithReason: String @deprecated(reason: "Reason")
99
5 | notDeprecated: String
1010
6 | }
@@ -34,7 +34,7 @@ exports[` 2`] = `
3434
7 |
3535
8 | enum testEnum {
3636
> 9 | item1 @deprecated
37-
| ^ Directive "@deprecated" must have a reason!
37+
| ^^^^^^^^^^^ Directive "@deprecated" must have a reason!
3838
10 | item2 @deprecated(reason: "Reason")
3939
11 | }
4040
12 |
@@ -63,7 +63,7 @@ exports[` 3`] = `
6363
12 |
6464
13 | interface testInterface {
6565
> 14 | item1: String @deprecated
66-
| ^ Directive "@deprecated" must have a reason!
66+
| ^^^^^^^^^^^ Directive "@deprecated" must have a reason!
6767
15 | item2: Number @deprecated(reason: "Reason")
6868
16 | item3: String
6969
17 | item4: String @deprecated(reason: "")
@@ -90,7 +90,7 @@ exports[` 4`] = `
9090
15 | item2: Number @deprecated(reason: "Reason")
9191
16 | item3: String
9292
> 17 | item4: String @deprecated(reason: "")
93-
| ^ Directive "@deprecated" must have a reason!
93+
| ^^^^^^^^^^^ Directive "@deprecated" must have a reason!
9494
18 | item5: String @deprecated(reason: " ")
9595
19 | }
9696
20 |
@@ -115,7 +115,7 @@ exports[` 5`] = `
115115
16 | item3: String
116116
17 | item4: String @deprecated(reason: "")
117117
> 18 | item5: String @deprecated(reason: " ")
118-
| ^ Directive "@deprecated" must have a reason!
118+
| ^^^^^^^^^^^ Directive "@deprecated" must have a reason!
119119
19 | }
120120
20 |
121121
`;

packages/plugin/tests/__snapshots__/require-field-of-type-query-in-mutation-result.spec.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[` 1`] = `
55
2 | type Query
66
3 | type Mutation {
77
> 4 | createUser: User!
8-
| ^^^^^^^^^^^^^^^^ Mutation result type "User" must contain field of type "Query".
8+
| ^^^^ Mutation result type "User" must contain field of type "Query".
99
5 | }
1010
6 |
1111
`;
@@ -17,7 +17,7 @@ exports[` 2`] = `
1717
4 |
1818
5 | extend type Mutation {
1919
> 6 | createUser: User!
20-
| ^^^^^^^^^^^^^^^^ Mutation result type "User" must contain field of type "Query".
20+
| ^^^^ Mutation result type "User" must contain field of type "Query".
2121
7 | }
2222
8 |
2323
`;
@@ -27,7 +27,7 @@ exports[` 3`] = `
2727
2 | type RootQuery
2828
3 | type RootMutation {
2929
> 4 | createUser: User!
30-
| ^^^^^^^^^^^^^^^^ Mutation result type "User" must contain field of type "RootQuery".
30+
| ^^^^ Mutation result type "User" must contain field of type "RootQuery".
3131
5 | }
3232
6 |
3333
7 | schema {
@@ -43,7 +43,7 @@ exports[` 4`] = `
4343
3 | type RootMutation
4444
4 | extend type RootMutation {
4545
> 5 | createUser: User!
46-
| ^^^^^^^^^^^^^^^^ Mutation result type "User" must contain field of type "RootQuery".
46+
| ^^^^ Mutation result type "User" must contain field of type "RootQuery".
4747
6 | }
4848
7 |
4949
8 | schema {

0 commit comments

Comments
 (0)