Skip to content

Commit 06c3652

Browse files
authored
Better changeset message (#1111)
* better message * better message
1 parent 712a8fe commit 06c3652

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

.changeset/four-trains-glow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@graphql-eslint/eslint-plugin': patch
33
---
44

5-
fix(selection-set-depth): fix `TypeError` when suggestion tried to apply to fragment that located in different file
5+
fix(selection-set-depth): fix `TypeError` from ESLint suggestions, don't provide suggestions for fragment that can be in a separate file

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const RULE_ID = 'selection-set-depth';
1313
const rule: GraphQLESLintRule<[SelectionSetDepthRuleConfig]> = {
1414
meta: {
1515
type: 'suggestion',
16+
// eslint-disable-next-line eslint-plugin/require-meta-has-suggestions -- optional since we can't provide fixes for fragments located in separate files
1617
hasSuggestions: true,
1718
docs: {
1819
category: 'Operations',
@@ -109,30 +110,32 @@ const rule: GraphQLESLintRule<[SelectionSetDepthRuleConfig]> = {
109110
getDocument: () => document,
110111
reportError(error: GraphQLError) {
111112
const { line, column } = error.locations[0];
113+
114+
const ancestors = context.getAncestors();
115+
const token = (ancestors[0] as AST.Program).tokens.find(
116+
token => token.loc.start.line === line && token.loc.start.column === column - 1
117+
);
118+
112119
context.report({
113120
loc: {
114121
line,
115122
column: column - 1,
116123
},
117124
message: error.message,
118-
suggest: [
119-
{
120-
desc: 'Remove selections',
121-
fix(fixer) {
122-
const ancestors = context.getAncestors();
123-
const token = (ancestors[0] as AST.Program).tokens.find(
124-
token => token.loc.start.line === line && token.loc.start.column === column - 1
125-
);
126-
if (!token) {
127-
return null;
128-
}
129-
const sourceCode = context.getSourceCode();
130-
const foundNode = sourceCode.getNodeByRangeIndex(token.range[0]) as any;
131-
const parentNode = foundNode.parent.parent;
132-
return fixer.remove(foundNode.kind === 'Name' ? parentNode.parent : parentNode);
125+
// Don't provide suggestions for fragment that can be in a separate file
126+
...(token && {
127+
suggest: [
128+
{
129+
desc: 'Remove selections',
130+
fix(fixer) {
131+
const sourceCode = context.getSourceCode();
132+
const foundNode = sourceCode.getNodeByRangeIndex(token.range[0]) as any;
133+
const parentNode = foundNode.parent.parent;
134+
return fixer.remove(foundNode.kind === 'Name' ? parentNode.parent : parentNode);
135+
},
133136
},
134-
},
135-
],
137+
],
138+
}),
136139
});
137140
},
138141
});

0 commit comments

Comments
 (0)