Skip to content

Commit 8b6d46b

Browse files
authored
naming-convention rule should not fail when aliasing underscore fields (#2713)
1 parent b15df66 commit 8b6d46b

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

.changeset/unlucky-months-sit.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+
`naming-convention` rule should not fail when aliasing underscore fields

packages/plugin/src/rules/naming-convention/index.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
109109
code: 'query { foo }',
110110
options: [{ OperationDefinition: { style: 'PascalCase' } }],
111111
},
112-
'{ test { __typename ok_ } }',
112+
{
113+
code: '{ test { __typename ok_ } }',
114+
},
113115
{
114116
name: 'should ignore fields',
115117
code: /* GraphQL */ `
@@ -209,6 +211,17 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
209211
},
210212
],
211213
},
214+
{
215+
name: 'should not fail when aliasing underscore fields',
216+
code: /* GraphQL */ `
217+
{
218+
test {
219+
bar: __foo
220+
foo: bar__
221+
}
222+
}
223+
`,
224+
},
212225
],
213226
invalid: [
214227
{

packages/plugin/src/rules/naming-convention/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
430430
};
431431

432432
const checkUnderscore = (isLeading: boolean) => (node: GraphQLESTreeNode<NameNode>) => {
433+
if (node.parent.kind === 'Field' && node.parent.alias !== node) {
434+
return;
435+
}
433436
const suggestedName = node.value.replace(isLeading ? /^_+/ : /_+$/, '');
434437
report(node, `${isLeading ? 'Leading' : 'Trailing'} underscores are not allowed`, [
435438
suggestedName,
@@ -439,14 +442,10 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
439442
const listeners: GraphQLESLintRuleListener = {};
440443

441444
if (!allowLeadingUnderscore) {
442-
listeners[
443-
'Name[value=/^_/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'
444-
] = checkUnderscore(true);
445+
listeners['Name[value=/^_/]'] = checkUnderscore(true);
445446
}
446447
if (!allowTrailingUnderscore) {
447-
listeners[
448-
'Name[value=/_$/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'
449-
] = checkUnderscore(false);
448+
listeners['Name[value=/_$/]'] = checkUnderscore(false);
450449
}
451450

452451
const selectors = new Set(

0 commit comments

Comments
 (0)