Skip to content

Commit 10b9975

Browse files
authored
allow to config naming-convention for Relay fragment convention <module_name>_<property_name> via requiredPattern option (#2838)
* aa * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Merge branch 'master' into use-search-params * Update .changeset/polite-impalas-float.md * Apply suggestions from code review
1 parent 0b5bf61 commit 10b9975

File tree

12 files changed

+351
-197
lines changed

12 files changed

+351
-197
lines changed

.changeset/polite-impalas-float.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': minor
3+
---
4+
5+
- allow to config `naming-convention` for Relay fragment convention `<module_name>_<property_name>`
6+
via `requiredPattern` option
7+
8+
- replace `requiredPatterns: RegEx[]` by `requiredPattern: RegEx` option

packages/plugin/src/rules/match-document-filename/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ const schemaOption = {
2828
oneOf: [{ $ref: '#/definitions/asString' }, { $ref: '#/definitions/asObject' }],
2929
} as const;
3030

31+
const caseSchema = {
32+
enum: CASE_STYLES,
33+
description: `One of: ${CASE_STYLES.map(t => `\`${t}\``).join(', ')}`,
34+
};
35+
3136
const schema = {
3237
definitions: {
33-
asString: {
34-
enum: CASE_STYLES,
35-
description: `One of: ${CASE_STYLES.map(t => `\`${t}\``).join(', ')}`,
36-
},
38+
asString: caseSchema,
3739
asObject: {
3840
type: 'object',
3941
additionalProperties: false,
4042
minProperties: 1,
4143
properties: {
42-
style: { enum: CASE_STYLES },
44+
style: caseSchema,
4345
suffix: { type: 'string' },
4446
prefix: { type: 'string' },
4547
},

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

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,48 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
237237
},
238238
],
239239
},
240+
{
241+
name: 'requiredPattern with case style in prefix',
242+
options: [
243+
{
244+
FragmentDefinition: {
245+
style: 'PascalCase',
246+
requiredPattern: /^(?<camelCase>.+?)_/,
247+
},
248+
},
249+
],
250+
code: /* GraphQL */ `
251+
fragment myUser_UserProfileFields on User {
252+
id
253+
}
254+
`,
255+
parserOptions: {
256+
graphQLConfig: {
257+
schema: 'type User',
258+
},
259+
},
260+
},
261+
{
262+
name: 'requiredPattern with case style in suffix',
263+
options: [
264+
{
265+
FragmentDefinition: {
266+
style: 'PascalCase',
267+
requiredPattern: /_(?<snake_case>.+?)$/,
268+
},
269+
},
270+
],
271+
code: /* GraphQL */ `
272+
fragment UserProfileFields_my_user on User {
273+
id
274+
}
275+
`,
276+
parserOptions: {
277+
graphQLConfig: {
278+
schema: 'type User',
279+
},
280+
},
281+
},
240282
],
241283
invalid: [
242284
{
@@ -446,15 +488,42 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
446488
`,
447489
options: (rule.meta.docs!.configOptions as any).operations,
448490
errors: [
449-
{ message: 'Query "TestQuery" should not have "Query" suffix' },
450-
{ message: 'Query "QueryTest" should not have "Query" prefix' },
451-
{ message: 'Query "GetQuery" should not have "Get" prefix' },
452-
{ message: 'Mutation "TestMutation" should not have "Mutation" suffix' },
453-
{ message: 'Mutation "MutationTest" should not have "Mutation" prefix' },
454-
{ message: 'Subscription "TestSubscription" should not have "Subscription" suffix' },
455-
{ message: 'Subscription "SubscriptionTest" should not have "Subscription" prefix' },
456-
{ message: 'Fragment "TestFragment" should not have "Fragment" suffix' },
457-
{ message: 'Fragment "FragmentTest" should not have "Fragment" prefix' },
491+
{
492+
message:
493+
'Query "TestQuery" should not contain the forbidden pattern "/(query|mutation|subscription)$/i"',
494+
},
495+
{
496+
message:
497+
'Query "QueryTest" should not contain the forbidden pattern "/^(query|mutation|subscription|get)/i"',
498+
},
499+
{
500+
message:
501+
'Query "GetQuery" should not contain the forbidden pattern "/^(query|mutation|subscription|get)/i"',
502+
},
503+
{
504+
message:
505+
'Mutation "TestMutation" should not contain the forbidden pattern "/(query|mutation|subscription)$/i"',
506+
},
507+
{
508+
message:
509+
'Mutation "MutationTest" should not contain the forbidden pattern "/^(query|mutation|subscription|get)/i"',
510+
},
511+
{
512+
message:
513+
'Subscription "TestSubscription" should not contain the forbidden pattern "/(query|mutation|subscription)$/i"',
514+
},
515+
{
516+
message:
517+
'Subscription "SubscriptionTest" should not contain the forbidden pattern "/^(query|mutation|subscription|get)/i"',
518+
},
519+
{
520+
message:
521+
'Fragment "TestFragment" should not contain the forbidden pattern "/(^fragment)|(fragment$)/i"',
522+
},
523+
{
524+
message:
525+
'Fragment "FragmentTest" should not contain the forbidden pattern "/(^fragment)|(fragment$)/i"',
526+
},
458527
],
459528
},
460529
{
@@ -536,17 +605,39 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
536605
errors: 2,
537606
},
538607
{
539-
name: 'requiredPatterns',
608+
name: 'requiredPattern',
540609
code: 'type Test { enabled: Boolean! }',
541610
options: [
542611
{
543612
'FieldDefinition[gqlType.gqlType.name.value=Boolean]': {
544613
style: 'camelCase',
545-
requiredPatterns: [/^(is|has)/],
614+
requiredPattern: /^(is|has)/,
546615
},
547616
},
548617
],
549618
errors: 1,
550619
},
620+
{
621+
name: 'requiredPattern with case style in suffix',
622+
options: [
623+
{
624+
FragmentDefinition: {
625+
style: 'PascalCase',
626+
requiredPattern: /_(?<camelCase>.+?)$/,
627+
},
628+
},
629+
],
630+
code: /* GraphQL */ `
631+
fragment UserProfileFields on User {
632+
id
633+
}
634+
`,
635+
parserOptions: {
636+
graphQLConfig: {
637+
schema: 'type User',
638+
},
639+
},
640+
errors: 1,
641+
},
551642
],
552643
});

0 commit comments

Comments
 (0)