Skip to content

Commit

Permalink
fix(alphabetize): should not fail when selection is aliased (#1114)
Browse files Browse the repository at this point in the history
* fix(alphabetize): should not fail when selection is aliased

* fix(alphabetize): should not fail when selection is aliased
  • Loading branch information
dimaMachina committed Jul 13, 2022
1 parent 97b276d commit 39ab684
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-pugs-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix(alphabetize): should not fail when selection is aliased
13 changes: 4 additions & 9 deletions packages/plugin/src/rules/alphabetize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ const rule: GraphQLESLintRule<[AlphabetizeConfig]> = {
// Starts from 1, ignore nodes.length <= 1
for (let i = 1; i < nodes.length; i += 1) {
const currNode = nodes[i];
const currName = 'name' in currNode && currNode.name?.value;
const currName = ('alias' in currNode && currNode.alias?.value) || ('name' in currNode && currNode.name?.value);
if (!currName) {
// we don't move unnamed current nodes
continue;
}

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

context.report({
node: currNode.name,
node: ('alias' in currNode && currNode.alias) || currNode.name,
messageId: RULE_ID,
data: {
currName,
Expand Down Expand Up @@ -340,12 +340,7 @@ const rule: GraphQLESLintRule<[AlphabetizeConfig]> = {

if (selectionsSelector) {
listeners[`:matches(${selectionsSelector}) SelectionSet`] = (node: GraphQLESTreeNode<SelectionSetNode>) => {
checkNodes(
node.selections.map(selection =>
// sort by alias is field is renamed
'alias' in selection && selection.alias ? ({ name: selection.alias } as any) : selection
)
);
checkNodes(node.selections);
};
}

Expand Down
42 changes: 42 additions & 0 deletions packages/plugin/tests/__snapshots__/alphabetize.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,45 @@ exports[`should sort definitions 1`] = `
58 |
59 | # END
`;

exports[`should sort when selection is aliased 1`] = `
#### ⌨️ Code

1 | { # start
2 | lastName: lastname # lastName comment
3 | fullName: fullname # fullName comment
4 | firsName: firstname # firsName comment
5 | # end
6 | }

#### ⚙️ Options

{
"selections": [
"OperationDefinition"
]
}

#### ❌ Error 1/2

2 | lastName: lastname # lastName comment
> 3 | fullName: fullname # fullName comment
| ^^^^^^^^ \`fullName\` should be before \`lastName\`.
4 | firsName: firstname # firsName comment

#### ❌ Error 2/2

3 | fullName: fullname # fullName comment
> 4 | firsName: firstname # firsName comment
| ^^^^^^^^ \`firsName\` should be before \`fullName\`.
5 | # end

#### 🔧 Autofix output

1 | { # start
2 | firsName: firstname # firsName comment
3 | fullName: fullname # fullName comment
4 | lastName: lastname # lastName comment
5 | # end
6 | }
`;
16 changes: 16 additions & 0 deletions packages/plugin/tests/alphabetize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,21 @@ ruleTester.runGraphQLTests<[AlphabetizeConfig]>('alphabetize', rule, {
{ message: '`auth` should be before `Data`.' },
],
},
{
name: 'should sort when selection is aliased',
options: [{ selections: ['OperationDefinition'] }],
code: /* GraphQL */ `
{ # start
lastName: lastname # lastName comment
fullName: fullname # fullName comment
firsName: firstname # firsName comment
# end
}
`,
errors: [
{ message: '`fullName` should be before `lastName`.' },
{ message: '`firsName` should be before `fullName`.' },
],
},
],
});

0 comments on commit 39ab684

Please sign in to comment.