Skip to content

Commit d081fcc

Browse files
author
Dimitri POSTOLOV
authored
fix error report for selection-set-depth rule and for graphql-js rules (#747)
1 parent cf3cc4f commit d081fcc

11 files changed

+28
-58
lines changed

Diff for: .changeset/purple-schools-complain.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 `selection-set-depth` rule and for `graphql-js` rules

Diff for: packages/plugin/src/rules/avoid-typename-prefix.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from 'graphql';
77
import { GraphQLESTreeNode } from '../estree-parser';
88
import { GraphQLESLintRule } from '../types';
9+
import { getLocation } from '../utils';
910

1011
const AVOID_TYPENAME_PREFIX = 'AVOID_TYPENAME_PREFIX';
1112

@@ -55,24 +56,13 @@ const rule: GraphQLESLintRule = {
5556
const fieldName = field.name.value;
5657

5758
if (fieldName.toLowerCase().startsWith(lowerTypeName)) {
58-
const { start } = field.loc;
59-
6059
context.report({
6160
data: {
6261
fieldName,
6362
typeName,
6463
},
6564
messageId: AVOID_TYPENAME_PREFIX,
66-
loc: {
67-
start: {
68-
line: start.line,
69-
column: start.column - 1,
70-
},
71-
end: {
72-
line: start.line,
73-
column: start.column - 1 + lowerTypeName.length,
74-
},
75-
},
65+
loc: getLocation(field.loc, lowerTypeName),
7666
});
7767
}
7868
}

Diff for: packages/plugin/src/rules/graphql-js-validation.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parseImportLine, processImport } from '@graphql-tools/import';
44
import { existsSync } from 'fs';
55
import { join, dirname } from 'path';
66
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
7-
import { requireGraphQLSchemaFromContext, requireSiblingsOperations } from '../utils';
7+
import { getLocation, requireGraphQLSchemaFromContext, requireSiblingsOperations } from '../utils';
88
import { GraphQLESTreeNode } from '../estree-parser';
99

1010
function extractRuleName(stack?: string): string | null {
@@ -26,9 +26,8 @@ export function validateDoc(
2626

2727
for (const error of validationErrors) {
2828
const validateRuleName = ruleName || `[${extractRuleName(error.stack)}]`;
29-
3029
context.report({
31-
loc: error.locations[0],
30+
loc: getLocation({ start: error.locations[0] }),
3231
message: ruleName ? error.message : `${validateRuleName} ${error.message}`,
3332
});
3433
}

Diff for: packages/plugin/src/rules/no-anonymous-operations.ts

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

34
const NO_ANONYMOUS_OPERATIONS = 'NO_ANONYMOUS_OPERATIONS';
45

@@ -37,27 +38,14 @@ const rule: GraphQLESLintRule = {
3738
},
3839
create(context) {
3940
return {
40-
OperationDefinition(node) {
41-
const isAnonymous = (node.name?.value || '').length === 0;
42-
if (isAnonymous) {
43-
const { start } = node.loc;
44-
context.report({
45-
loc: {
46-
start: {
47-
column: start.column - 1,
48-
line: start.line,
49-
},
50-
end: {
51-
column: start.column - 1 + node.operation.length,
52-
line: start.line,
53-
},
54-
},
55-
data: {
56-
operation: node.operation,
57-
},
58-
messageId: NO_ANONYMOUS_OPERATIONS,
59-
});
60-
}
41+
'OperationDefinition[name=undefined]'(node) {
42+
context.report({
43+
loc: getLocation(node.loc, node.operation),
44+
data: {
45+
operation: node.operation,
46+
},
47+
messageId: NO_ANONYMOUS_OPERATIONS,
48+
});
6149
},
6250
};
6351
},

Diff for: packages/plugin/src/rules/require-description.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { GraphQLESLintRule, GraphQLESLintRuleContext, ValueOf } from '../types';
22
import { GraphQLESTreeNode } from '../estree-parser/estree-ast';
33
import { ASTKindToNode, Kind, StringValueNode } from 'graphql';
4+
import { getLocation } from '../utils';
45

56
const REQUIRE_DESCRIPTION_ERROR = 'REQUIRE_DESCRIPTION_ERROR';
67
const DESCRIBABLE_NODES = [
@@ -28,21 +29,8 @@ function verifyRule(
2829
) {
2930
if (node) {
3031
if (!node.description || !node.description.value || node.description.value.trim().length === 0) {
31-
const { start, end } = ('name' in node ? node.name : node).loc;
32-
3332
context.report({
34-
loc: {
35-
start: {
36-
line: start.line,
37-
column: start.column - 1,
38-
},
39-
end: {
40-
line: end.line,
41-
column:
42-
// node.name don't exist on SchemaDefinition
43-
'name' in node ? end.column - 1 + node.name.value.length : end.column,
44-
},
45-
},
33+
loc: getLocation(('name' in node ? node.name : node).loc, 'name' in node ? node.name.value : 'schema'),
4634
messageId: REQUIRE_DESCRIPTION_ERROR,
4735
data: {
4836
nodeType: node.kind,

Diff for: packages/plugin/src/rules/selection-set-depth.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { GraphQLESLintRule } from '../types';
22
import depthLimit from 'graphql-depth-limit';
33
import { DocumentNode, FragmentDefinitionNode, GraphQLError, Kind, OperationDefinitionNode } from 'graphql';
44
import { GraphQLESTreeNode } from '../estree-parser';
5-
import { requireSiblingsOperations } from '../utils';
5+
import { getLocation, requireSiblingsOperations } from '../utils';
66
import { SiblingOperations } from '../sibling-operations';
77

88
type SelectionSetDepthRuleConfig = [{ maxDepth: number; ignore?: string[] }];
@@ -111,7 +111,7 @@ const rule: GraphQLESLintRule<SelectionSetDepthRuleConfig> = {
111111
getDocument: () => document,
112112
reportError: (error: GraphQLError) => {
113113
context.report({
114-
loc: error.locations[0],
114+
loc: getLocation({ start: error.locations[0] }),
115115
message: error.message,
116116
});
117117
},

Diff for: packages/plugin/tests/__snapshots__/executable-definitions.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
exports[` 1`] = `
44
> 1 | type Query { t: String }
5-
| ^ The "Query" definition is not executable.
5+
| ^ The "Query" definition is not executable.
66
`;

Diff for: packages/plugin/tests/__snapshots__/lone-schema-definition.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exports[` 1`] = `
2424
21 | }
2525
22 |
2626
> 23 | schema {
27-
| ^ Must provide only one schema definition.
27+
| ^ Must provide only one schema definition.
2828
24 | query: RootQuery
2929
25 | mutation: RootMutation
3030
26 | }

Diff for: packages/plugin/tests/__snapshots__/possible-type-extension.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exports[` 1`] = `
77
4 | }
88
5 |
99
> 6 | extend type OtherUser {
10-
| ^ Cannot extend type "OtherUser" because it is not defined.
10+
| ^ Cannot extend type "OtherUser" because it is not defined.
1111
7 | name: String!
1212
8 | }
1313
9 |

Diff for: packages/plugin/tests/__snapshots__/selection-set-depth.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports[` 1`] = `
66
3 | viewer {
77
4 | albums {
88
> 5 | title
9-
| ^ 'deep2' exceeds maximum operation depth of 1
9+
| ^ 'deep2' exceeds maximum operation depth of 1
1010
6 | }
1111
7 | }
1212
8 | }
@@ -19,7 +19,7 @@ exports[` 2`] = `
1919
3 | viewer {
2020
4 | albums {
2121
> 5 | ...AlbumFields
22-
| ^ 'deep2' exceeds maximum operation depth of 1
22+
| ^ 'deep2' exceeds maximum operation depth of 1
2323
6 | }
2424
7 | }
2525
8 | }

Diff for: packages/plugin/tests/__snapshots__/unique-type-names.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[` 1`] = `
44
1 |
55
> 2 | type Query {
6-
| ^ There can be only one type named "Query".
6+
| ^ There can be only one type named "Query".
77
3 | foo: String
88
4 | }
99
5 |

0 commit comments

Comments
 (0)