Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

naming-convention rule should not fail when aliasing underscore fields #2713

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading