Skip to content

Commit 0cacf02

Browse files
committed
aa
1 parent 77a6d6b commit 0cacf02

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
529529
{
530530
name: 'forbiddenPattern',
531531
code: 'query queryFoo { foo } query getBar { bar }',
532-
options: [{ OperationDefinition: { forbiddenPattern: ['^get', '^query'] } }],
532+
options: [{ OperationDefinition: { forbiddenPattern: [/^(get|query)/] } }],
533533
errors: 2,
534534
},
535535
{
@@ -539,7 +539,7 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
539539
{
540540
'FieldDefinition[gqlType.gqlType.name.value=Boolean]': {
541541
style: 'camelCase',
542-
requiredPattern: ['^is', '^has'],
542+
requiredPattern: [/^(is|has)/],
543543
},
544544
},
545545
],

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,20 @@ const schema = {
6666
style: { enum: ALLOWED_STYLES },
6767
prefix: { type: 'string' },
6868
suffix: { type: 'string' },
69-
forbiddenPattern: ARRAY_DEFAULT_OPTIONS,
70-
requiredPattern: ARRAY_DEFAULT_OPTIONS,
69+
forbiddenPattern: {
70+
...ARRAY_DEFAULT_OPTIONS,
71+
items: {
72+
type: 'object',
73+
description: 'RegEx',
74+
},
75+
},
76+
requiredPattern: {
77+
...ARRAY_DEFAULT_OPTIONS,
78+
items: {
79+
type: 'object',
80+
description: 'RegEx',
81+
},
82+
},
7183
forbiddenPrefixes: {
7284
...ARRAY_DEFAULT_OPTIONS,
7385
description: descriptionPrefixesSuffixes('forbiddenPattern'),
@@ -158,8 +170,8 @@ type PropertySchema = {
158170
style?: AllowedStyle;
159171
suffix?: string;
160172
prefix?: string;
161-
forbiddenPattern?: string[];
162-
requiredPattern?: string[];
173+
forbiddenPattern?: RegExp[];
174+
requiredPattern?: RegExp[];
163175
forbiddenPrefixes?: string[];
164176
forbiddenSuffixes?: string[];
165177
requiredPrefixes?: string[];
@@ -423,18 +435,16 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
423435
renameToNames: [name + suffix],
424436
};
425437
}
426-
const forbidden = forbiddenPattern?.find((pattern: string) =>
427-
new RegExp(pattern).test(name),
428-
);
438+
const forbidden = forbiddenPattern?.find(pattern => pattern.test(name));
429439
if (forbidden) {
430440
return {
431441
errorMessage: `not contain the forbidden pattern "${forbidden}"`,
432-
renameToNames: [name.replace(new RegExp(forbidden), '')],
442+
renameToNames: [name.replace(forbidden, '')],
433443
};
434444
}
435-
if (requiredPattern && !requiredPattern.some(pattern => new RegExp(pattern).test(name))) {
445+
if (requiredPattern && !requiredPattern.some(pattern => pattern.test(name))) {
436446
return {
437-
errorMessage: `contain the required pattern: ${englishJoinWords(requiredPattern)}`,
447+
errorMessage: `contain the required pattern: ${englishJoinWords(requiredPattern.map(re => re.source))}`,
438448
renameToNames: [],
439449
};
440450
}

packages/plugin/src/rules/naming-convention/snapshot.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ exports[`naming-convention > invalid > forbiddenPattern 1`] = `
394394
#### ❌ Error 1/2
395395

396396
> 1 | query queryFoo { foo } query getBar { bar }
397-
| ^^^^^^^^ Query "queryFoo" should not contain the forbidden pattern "^query"
397+
| ^^^^^^^^ Query "queryFoo" should not contain the forbidden pattern "/^(get|query)/"
398398

399399
#### 💡 Suggestion: Rename to \`Foo\`
400400

@@ -403,7 +403,7 @@ exports[`naming-convention > invalid > forbiddenPattern 1`] = `
403403
#### ❌ Error 2/2
404404

405405
> 1 | query queryFoo { foo } query getBar { bar }
406-
| ^^^^^^ Query "getBar" should not contain the forbidden pattern "^get"
406+
| ^^^^^^ Query "getBar" should not contain the forbidden pattern "/^(get|query)/"
407407

408408
#### 💡 Suggestion: Rename to \`Bar\`
409409

@@ -1994,7 +1994,7 @@ exports[`naming-convention > invalid > requiredPattern 1`] = `
19941994
#### ❌ Error
19951995

19961996
> 1 | type Test { enabled: Boolean! }
1997-
| ^^^^^^^ Field "enabled" should contain the required pattern: ^is or ^has
1997+
| ^^^^^^^ Field "enabled" should contain the required pattern: ^(is|has)
19981998
`;
19991999

20002000
exports[`naming-convention > invalid > schema-recommended config 1`] = `

website/content/rules/naming-convention.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ This element must be one of the following enum values:
323323

324324
### `forbiddenPattern` (array)
325325

326-
The object is an array with all elements of the type `string`.
326+
The object is an array with all elements of the type `object`.
327+
328+
The array object has the following properties:
327329

328330
Additional restrictions:
329331

@@ -332,7 +334,9 @@ Additional restrictions:
332334

333335
### `requiredPattern` (array)
334336

335-
The object is an array with all elements of the type `string`.
337+
The object is an array with all elements of the type `object`.
338+
339+
The array object has the following properties:
336340

337341
Additional restrictions:
338342

0 commit comments

Comments
 (0)