Skip to content

Commit 712a8fe

Browse files
authored
fix(selection-set-depth): fix TypeError when suggestion tried to apply to fragment that located in different file (#1110)
* fix(selection-set-depth): fix `TypeError` when suggestion tried to apply to fragment that located in different file * Update packages/plugin/tests/selection-set-depth.spec.ts
1 parent 01421ae commit 712a8fe

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

.changeset/four-trains-glow.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(selection-set-depth): fix `TypeError` when suggestion tried to apply to fragment that located in different file

packages/plugin/src/rules/selection-set-depth.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ const rule: GraphQLESLintRule<[SelectionSetDepthRuleConfig]> = {
123123
const token = (ancestors[0] as AST.Program).tokens.find(
124124
token => token.loc.start.line === line && token.loc.start.column === column - 1
125125
);
126+
if (!token) {
127+
return null;
128+
}
126129
const sourceCode = context.getSourceCode();
127130
const foundNode = sourceCode.getNodeByRangeIndex(token.range[0]) as any;
128131
const parentNode = foundNode.parent.parent;

packages/plugin/tests/__snapshots__/selection-set-depth.spec.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,35 @@ exports[`Invalid #2 1`] = `
6666
5 | }
6767
`;
6868

69+
exports[`suggestions should not throw error when fragment is located in different file 1`] = `
70+
#### ⌨️ Code
71+
72+
1 | {
73+
2 | viewer {
74+
3 | albums {
75+
4 | ...AlbumFields
76+
5 | }
77+
6 | }
78+
7 | }
79+
80+
#### ⚙️ Options
81+
82+
{
83+
"maxDepth": 2
84+
}
85+
86+
#### ❌ Error
87+
88+
4 | ...AlbumFields
89+
> 5 | }
90+
| ^ '' exceeds maximum operation depth of 2
91+
6 | }
92+
`;
93+
6994
exports[`suggestions should work with inline fragments 1`] = `
7095
#### ⌨️ Code
7196

72-
1 | query {
97+
1 | {
7398
2 | viewer {
7499
3 | albums {
75100
4 | ... on Album {
@@ -94,7 +119,7 @@ exports[`suggestions should work with inline fragments 1`] = `
94119

95120
#### 💡 Suggestion: Remove selections
96121

97-
1 | query {
122+
1 | {
98123
2 | viewer {
99124
3 |
100125
4 | }

packages/plugin/tests/selection-set-depth.spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ruleTester.runGraphQLTests<[SelectionSetDepthRuleConfig]>('selection-set-depth',
1414
{
1515
options: [{ maxDepth: 2 }],
1616
code: `
17-
query {
17+
{
1818
viewer { # Level 0
1919
albums { # Level 1
2020
title # Level 2
@@ -84,7 +84,7 @@ ruleTester.runGraphQLTests<[SelectionSetDepthRuleConfig]>('selection-set-depth',
8484
options: [{ maxDepth: 1 }],
8585
errors: [{ message: "'' exceeds maximum operation depth of 1" }],
8686
code: /* GraphQL */ `
87-
query {
87+
{
8888
viewer {
8989
albums {
9090
... on Album {
@@ -95,5 +95,29 @@ ruleTester.runGraphQLTests<[SelectionSetDepthRuleConfig]>('selection-set-depth',
9595
}
9696
`,
9797
},
98+
{
99+
name: 'suggestions should not throw error when fragment is located in different file',
100+
parserOptions: {
101+
operations: /* GraphQL */ `
102+
fragment AlbumFields on Album {
103+
id
104+
modifier {
105+
date
106+
}
107+
}
108+
`,
109+
},
110+
options: [{ maxDepth: 2 }],
111+
errors: [{ message: "'' exceeds maximum operation depth of 2" }],
112+
code: /* GraphQL */ `
113+
{
114+
viewer {
115+
albums {
116+
...AlbumFields
117+
}
118+
}
119+
}
120+
`,
121+
},
98122
],
99123
});

0 commit comments

Comments
 (0)