Skip to content

Commit ca8b521

Browse files
author
Dimitri POSTOLOV
authored
fix: compare siblings with real filepath instead of virtual in unique-fragment-name and unique-operation-name (#527)
1 parent a008348 commit ca8b521

25 files changed

+287
-206
lines changed

.changeset/hot-items-fry.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: code filepath in unique-fragment-name and unique-operation-name rules

docs/README.md

Lines changed: 52 additions & 53 deletions
Large diffs are not rendered by default.

docs/rules/known-fragment-names.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,29 @@ query {
5555
...UserFields
5656
}
5757
}
58+
```
59+
60+
### False positive case
61+
62+
For extracting documents from code under the hood we use [graphql-tag-pluck](https://graphql-tools.com/docs/graphql-tag-pluck) that [don't support string interpolation](https://stackoverflow.com/questions/62749847/graphql-codegen-dynamic-fields-with-interpolation/62751311#62751311) for this moment.
63+
64+
```js
65+
const USER_FIELDS = gql`
66+
fragment UserFields on User {
67+
id
68+
}
69+
`
70+
71+
const GET_USER = /* GraphQL */ `
72+
# eslint @graphql-eslint/known-fragment-names: 'error'
73+
74+
query User {
75+
user {
76+
...UserFields
77+
}
78+
}
79+
80+
# Will give false positive error 'Unknown fragment "UserFields"'
81+
${USER_FIELDS}
82+
`
5883
```

docs/rules/no-deprecated.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Requires GraphQL Schema: `true` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
66
- Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
77

8-
This rule allow you to enforce that deprecated fields or enum values are not in use by operations.
8+
Enforce that deprecated fields or enum values are not in use by operations.
99

1010
## Usage Examples
1111

docs/rules/no-hashtag-description.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
77

88
Requires to use `"""` or `"` for adding a GraphQL description instead of `#`.
9-
This rule allows you to use hashtag for comments, as long as it's not attached to a AST definition.
9+
Allows to use hashtag for comments, as long as it's not attached to an AST definition.
1010

1111
## Usage Examples
1212

docs/rules/require-id-when-available.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Requires GraphQL Schema: `true` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
66
- Requires GraphQL Operations: `true` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
77

8-
This rule allow you to enforce selecting specific fields when they are available on the GraphQL type.
8+
Enforce selecting specific fields when they are available on the GraphQL type.
99

1010
## Usage Examples
1111

docs/rules/unique-fragment-name.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Requires GraphQL Schema: `false` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
66
- Requires GraphQL Operations: `true` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
77

8-
This rule allow you to enforce unique fragment name across your application.
8+
Enforce unique fragment names across your project.
99

1010
## Usage Examples
1111

docs/rules/unique-operation-name.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Requires GraphQL Schema: `false` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
66
- Requires GraphQL Operations: `true` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
77

8-
This rule allow you to enforce unique operation names across your project.
8+
Enforce unique operation names across your project.
99

1010
## Usage Examples
1111

packages/plugin/src/estree-parser/utils.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function convertRange(gqlLocation: Location): [number, number] {
7070
}
7171

7272
export function extractCommentsFromAst(node: ASTNode): Comment[] {
73-
const loc = node.loc;
73+
const { loc } = node;
7474

7575
if (!loc) {
7676
return [];
@@ -81,19 +81,13 @@ export function extractCommentsFromAst(node: ASTNode): Comment[] {
8181

8282
while (token !== null) {
8383
if (token.kind === TokenKind.COMMENT) {
84-
const value = String(token.value);
84+
const { line, column } = token;
8585
comments.push({
8686
type: 'Block',
87-
value: ' ' + value + ' ',
87+
value: ` ${token.value} `,
8888
loc: {
89-
start: {
90-
line: token.line,
91-
column: token.column,
92-
},
93-
end: {
94-
line: token.line,
95-
column: token.column,
96-
},
89+
start: { line, column },
90+
end: { line, column },
9791
},
9892
range: [token.start, token.end],
9993
});
@@ -122,7 +116,7 @@ export function convertLocation(gqlLocation: Location): SourceLocation {
122116
export function isNodeWithDescription<T extends ASTNode>(
123117
obj: T
124118
): obj is T & { readonly description?: StringValueNode } {
125-
return obj && (obj as any).description;
119+
return (obj as any)?.description;
126120
}
127121

128122
export function convertDescription<T extends ASTNode>(node: T): Comment[] {

packages/plugin/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export { rules } from './rules';
22
export { processors } from './processors';
33
export * from './parser';
4-
export * from './utils';
54
export * from './types';
65
export * from './estree-parser';
76
export * from './testkit';

0 commit comments

Comments
 (0)