Skip to content

Commit 39ab684

Browse files
authored
fix(alphabetize): should not fail when selection is aliased (#1114)
* fix(alphabetize): should not fail when selection is aliased * fix(alphabetize): should not fail when selection is aliased
1 parent 97b276d commit 39ab684

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

.changeset/perfect-pugs-carry.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(alphabetize): should not fail when selection is aliased

packages/plugin/src/rules/alphabetize.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,14 @@ const rule: GraphQLESLintRule<[AlphabetizeConfig]> = {
257257
// Starts from 1, ignore nodes.length <= 1
258258
for (let i = 1; i < nodes.length; i += 1) {
259259
const currNode = nodes[i];
260-
const currName = 'name' in currNode && currNode.name?.value;
260+
const currName = ('alias' in currNode && currNode.alias?.value) || ('name' in currNode && currNode.name?.value);
261261
if (!currName) {
262262
// we don't move unnamed current nodes
263263
continue;
264264
}
265265

266266
const prevNode = nodes[i - 1];
267-
const prevName = 'name' in prevNode && prevNode.name?.value;
267+
const prevName = ('alias' in prevNode && prevNode.alias?.value) || ('name' in prevNode && prevNode.name?.value);
268268
if (prevName) {
269269
// Compare with lexicographic order
270270
const compareResult = prevName.localeCompare(currName);
@@ -278,7 +278,7 @@ const rule: GraphQLESLintRule<[AlphabetizeConfig]> = {
278278
}
279279

280280
context.report({
281-
node: currNode.name,
281+
node: ('alias' in currNode && currNode.alias) || currNode.name,
282282
messageId: RULE_ID,
283283
data: {
284284
currName,
@@ -340,12 +340,7 @@ const rule: GraphQLESLintRule<[AlphabetizeConfig]> = {
340340

341341
if (selectionsSelector) {
342342
listeners[`:matches(${selectionsSelector}) SelectionSet`] = (node: GraphQLESTreeNode<SelectionSetNode>) => {
343-
checkNodes(
344-
node.selections.map(selection =>
345-
// sort by alias is field is renamed
346-
'alias' in selection && selection.alias ? ({ name: selection.alias } as any) : selection
347-
)
348-
);
343+
checkNodes(node.selections);
349344
};
350345
}
351346

packages/plugin/tests/__snapshots__/alphabetize.spec.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,3 +816,45 @@ exports[`should sort definitions 1`] = `
816816
58 |
817817
59 | # END
818818
`;
819+
820+
exports[`should sort when selection is aliased 1`] = `
821+
#### ⌨️ Code
822+
823+
1 | { # start
824+
2 | lastName: lastname # lastName comment
825+
3 | fullName: fullname # fullName comment
826+
4 | firsName: firstname # firsName comment
827+
5 | # end
828+
6 | }
829+
830+
#### ⚙️ Options
831+
832+
{
833+
"selections": [
834+
"OperationDefinition"
835+
]
836+
}
837+
838+
#### ❌ Error 1/2
839+
840+
2 | lastName: lastname # lastName comment
841+
> 3 | fullName: fullname # fullName comment
842+
| ^^^^^^^^ \`fullName\` should be before \`lastName\`.
843+
4 | firsName: firstname # firsName comment
844+
845+
#### ❌ Error 2/2
846+
847+
3 | fullName: fullname # fullName comment
848+
> 4 | firsName: firstname # firsName comment
849+
| ^^^^^^^^ \`firsName\` should be before \`fullName\`.
850+
5 | # end
851+
852+
#### 🔧 Autofix output
853+
854+
1 | { # start
855+
2 | firsName: firstname # firsName comment
856+
3 | fullName: fullname # fullName comment
857+
4 | lastName: lastname # lastName comment
858+
5 | # end
859+
6 | }
860+
`;

packages/plugin/tests/alphabetize.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,5 +332,21 @@ ruleTester.runGraphQLTests<[AlphabetizeConfig]>('alphabetize', rule, {
332332
{ message: '`auth` should be before `Data`.' },
333333
],
334334
},
335+
{
336+
name: 'should sort when selection is aliased',
337+
options: [{ selections: ['OperationDefinition'] }],
338+
code: /* GraphQL */ `
339+
{ # start
340+
lastName: lastname # lastName comment
341+
fullName: fullname # fullName comment
342+
firsName: firstname # firsName comment
343+
# end
344+
}
345+
`,
346+
errors: [
347+
{ message: '`fullName` should be before `lastName`.' },
348+
{ message: '`firsName` should be before `fullName`.' },
349+
],
350+
},
335351
],
336352
});

0 commit comments

Comments
 (0)