diff --git a/.changeset/unlucky-months-sit.md b/.changeset/unlucky-months-sit.md new file mode 100644 index 00000000000..f80419ff942 --- /dev/null +++ b/.changeset/unlucky-months-sit.md @@ -0,0 +1,5 @@ +--- +'@graphql-eslint/eslint-plugin': patch +--- + +`naming-convention` rule should not fail when aliasing underscore fields diff --git a/packages/plugin/src/rules/naming-convention/index.test.ts b/packages/plugin/src/rules/naming-convention/index.test.ts index 3bdceff48b1..d7ea9eb403d 100644 --- a/packages/plugin/src/rules/naming-convention/index.test.ts +++ b/packages/plugin/src/rules/naming-convention/index.test.ts @@ -109,7 +109,9 @@ ruleTester.run('naming-convention', rule, { code: 'query { foo }', options: [{ OperationDefinition: { style: 'PascalCase' } }], }, - '{ test { __typename ok_ } }', + { + code: '{ test { __typename ok_ } }', + }, { name: 'should ignore fields', code: /* GraphQL */ ` @@ -209,6 +211,17 @@ ruleTester.run('naming-convention', rule, { }, ], }, + { + name: 'should not fail when aliasing underscore fields', + code: /* GraphQL */ ` + { + test { + bar: __foo + foo: bar__ + } + } + `, + }, ], invalid: [ { diff --git a/packages/plugin/src/rules/naming-convention/index.ts b/packages/plugin/src/rules/naming-convention/index.ts index 09911d3718a..8917e23dfee 100644 --- a/packages/plugin/src/rules/naming-convention/index.ts +++ b/packages/plugin/src/rules/naming-convention/index.ts @@ -430,6 +430,9 @@ export const rule: GraphQLESLintRule = { }; const checkUnderscore = (isLeading: boolean) => (node: GraphQLESTreeNode) => { + 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, @@ -439,14 +442,10 @@ export const rule: GraphQLESLintRule = { 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(