Skip to content

Commit f712780

Browse files
author
Dimitri POSTOLOV
authored
fix error report for unique-fragment-name and unique-operation-name rule (#735)
* fix error report for `unique-fragment-name` and `unique-operation-name` rule
1 parent a8c7b43 commit f712780

File tree

6 files changed

+38
-22
lines changed

6 files changed

+38
-22
lines changed

.changeset/odd-islands-run.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 `unique-fragment-name` and `unique-operation-name` rule

packages/plugin/src/rules/unique-fragment-name.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { relative } from 'path';
22
import { FragmentDefinitionNode, Kind, OperationDefinitionNode } from 'graphql';
33
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
44
import { GraphQLESTreeNode } from '../estree-parser';
5-
import { normalizePath, requireSiblingsOperations, getOnDiskFilepath } from '../utils';
5+
import { normalizePath, requireSiblingsOperations, getOnDiskFilepath, getLocation } from '../utils';
66
import { FragmentSource, OperationSource } from '../sibling-operations';
77

88
const RULE_NAME = 'unique-fragment-name';
@@ -14,11 +14,7 @@ export const checkNode = (
1414
ruleName: string,
1515
messageId: string
1616
): void => {
17-
const documentName = node.name?.value;
18-
if (!documentName) {
19-
return;
20-
}
21-
17+
const documentName = node.name.value;
2218
const siblings = requireSiblingsOperations(ruleName, context);
2319
const siblingDocuments: (FragmentSource | OperationSource)[] =
2420
node.kind === Kind.FRAGMENT_DEFINITION ? siblings.getFragment(documentName) : siblings.getOperation(documentName);
@@ -31,7 +27,6 @@ export const checkNode = (
3127
});
3228

3329
if (conflictingDocuments.length > 0) {
34-
const { start, end } = node.name.loc;
3530
context.report({
3631
messageId,
3732
data: {
@@ -40,16 +35,7 @@ export const checkNode = (
4035
.map(f => `\t${relative(process.cwd(), getOnDiskFilepath(f.filePath))}`)
4136
.join('\n'),
4237
},
43-
loc: {
44-
start: {
45-
line: start.line,
46-
column: start.column - 1,
47-
},
48-
end: {
49-
line: end.line,
50-
column: end.column - 1,
51-
},
52-
},
38+
loc: getLocation(node.name.loc, documentName),
5339
});
5440
}
5541
};

packages/plugin/src/rules/unique-operation-name.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const rule: GraphQLESLintRule = {
5858
},
5959
create(context) {
6060
return {
61-
OperationDefinition(node) {
61+
'OperationDefinition[name!=undefined]'(node) {
6262
checkNode(context, node, RULE_NAME, UNIQUE_OPERATION_NAME);
6363
},
6464
};

packages/plugin/src/utils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,28 @@ export const convertCase = (style: CaseStyle, str: string): string => {
189189
return lowerCase(str).replace(/ /g, '-');
190190
}
191191
};
192+
193+
export function getLocation(
194+
loc: Partial<AST.SourceLocation>,
195+
fieldName = '',
196+
offset?: { offsetStart?: number; offsetEnd?: number }
197+
): AST.SourceLocation {
198+
const { start } = loc;
199+
200+
/*
201+
* ESLint has 0-based column number
202+
* https://eslint.org/docs/developer-guide/working-with-rules#contextreport
203+
*/
204+
const { offsetStart = 1, offsetEnd = 1 } = offset ?? {};
205+
206+
return {
207+
start: {
208+
line: start.line,
209+
column: start.column - offsetStart,
210+
},
211+
end: {
212+
line: start.line,
213+
column: start.column - offsetEnd + fieldName.length,
214+
},
215+
};
216+
}

packages/plugin/tests/__snapshots__/unique-fragment-name.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
exports[` 1`] = `
44
> 1 | fragment HasIdFields on U { a b c }
5-
| ^ Fragment named "HasIdFields" already defined in:
5+
| ^^^^^^^^^^^ Fragment named "HasIdFields" already defined in:
66
-1866344359.graphql
77
`;
88

99
exports[` 2`] = `
1010
> 1 | fragment HasIdFields on U { a b c }
11-
| ^ Fragment named "HasIdFields" already defined in:
11+
| ^^^^^^^^^^^ Fragment named "HasIdFields" already defined in:
1212
-1559743294.graphql
1313
-1866344359.graphql
1414
`;

packages/plugin/tests/__snapshots__/unique-operation-name.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
exports[` 1`] = `
44
> 1 | query test { bar }
5-
| ^ Operation named "test" already defined in:
5+
| ^^^^ Operation named "test" already defined in:
66
6844040.graphql
77
`;
88

99
exports[` 2`] = `
1010
> 1 | query test { bar }
11-
| ^ Operation named "test" already defined in:
11+
| ^^^^ Operation named "test" already defined in:
1212
6844040.graphql
1313
84823255.graphql
1414
`;

0 commit comments

Comments
 (0)