Skip to content

Commit

Permalink
naming-convention rule should not fail when aliasing underscore fie…
Browse files Browse the repository at this point in the history
…lds (#2713)
  • Loading branch information
dimaMachina authored Nov 17, 2024
1 parent b15df66 commit 8b6d46b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-months-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

`naming-convention` rule should not fail when aliasing underscore fields
15 changes: 14 additions & 1 deletion packages/plugin/src/rules/naming-convention/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
code: 'query { foo }',
options: [{ OperationDefinition: { style: 'PascalCase' } }],
},
'{ test { __typename ok_ } }',
{
code: '{ test { __typename ok_ } }',
},
{
name: 'should ignore fields',
code: /* GraphQL */ `
Expand Down Expand Up @@ -209,6 +211,17 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
},
],
},
{
name: 'should not fail when aliasing underscore fields',
code: /* GraphQL */ `
{
test {
bar: __foo
foo: bar__
}
}
`,
},
],
invalid: [
{
Expand Down
11 changes: 5 additions & 6 deletions packages/plugin/src/rules/naming-convention/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
};

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

if (!allowLeadingUnderscore) {
listeners[
'Name[value=/^_/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'
] = checkUnderscore(true);
listeners['Name[value=/^_/]'] = checkUnderscore(true);
}
if (!allowTrailingUnderscore) {
listeners[
'Name[value=/_$/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'
] = checkUnderscore(false);
listeners['Name[value=/_$/]'] = checkUnderscore(false);
}

const selectors = new Set(
Expand Down

0 comments on commit 8b6d46b

Please sign in to comment.