Skip to content

Commit d9bdbd3

Browse files
authored
feat: add new style matchDocumentStyle for match-document-filename rule (#929)
* feat: add new style `documentStyle` for `match-document-filename` rule * rename `documentStyle` to `matchDocumentStyle`
1 parent af22d9d commit d9bdbd3

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

.changeset/strong-fireants-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': minor
3+
---
4+
5+
feat: add new style `matchDocumentStyle` for `match-document-filename` rule

docs/rules/match-document-filename.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ The schema defines the following additional types:
131131

132132
## `asString` (enum)
133133

134-
One of: `camelCase`, `PascalCase`, `snake_case`, `UPPER_CASE`, `kebab-case`
134+
One of: `camelCase`, `PascalCase`, `snake_case`, `UPPER_CASE`, `kebab-case`, `matchDocumentStyle`
135135

136136
## `asObject` (object)
137137

@@ -146,6 +146,7 @@ This element must be one of the following enum values:
146146
- `snake_case`
147147
- `UPPER_CASE`
148148
- `kebab-case`
149+
- `matchDocumentStyle`
149150

150151
### `suffix` (string)
151152

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { basename, extname } from 'path';
22
import { existsSync } from 'fs';
33
import { FragmentDefinitionNode, Kind, OperationDefinitionNode } from 'graphql';
4-
import { CaseStyle, convertCase } from '../utils';
4+
import { CaseStyle as _CaseStyle, convertCase } from '../utils';
55
import { GraphQLESLintRule } from '../types';
66
import { GraphQLESTreeNode } from '../estree-parser';
77

8+
type CaseStyle = _CaseStyle | 'matchDocumentStyle';
9+
810
const MATCH_EXTENSION = 'MATCH_EXTENSION';
911
const MATCH_STYLE = 'MATCH_STYLE';
1012

1113
const ACCEPTED_EXTENSIONS: ['.gql', '.graphql'] = ['.gql', '.graphql'];
12-
const CASE_STYLES: CaseStyle[] = ['camelCase', 'PascalCase', 'snake_case', 'UPPER_CASE', 'kebab-case'];
14+
const CASE_STYLES: CaseStyle[] = ['camelCase', 'PascalCase', 'snake_case', 'UPPER_CASE', 'kebab-case', 'matchDocumentStyle'];
1315

1416
type PropertySchema = {
1517
style?: CaseStyle;
@@ -210,8 +212,13 @@ const rule: GraphQLESLintRule<[MatchDocumentFilenameRuleConfig]> = {
210212
option = { style: option } as PropertySchema;
211213
}
212214
const expectedExtension = options.fileExtension || fileExtension;
213-
const expectedFilename =
214-
(option.style ? convertCase(option.style, docName) : filename) + (option.suffix || '') + expectedExtension;
215+
let expectedFilename: string;
216+
if (option.style) {
217+
expectedFilename = option.style === 'matchDocumentStyle' ? docName : convertCase(option.style, docName);
218+
} else {
219+
expectedFilename = filename;
220+
}
221+
expectedFilename += (option.suffix || '') + expectedExtension;
215222
const filenameWithExtension = filename + expectedExtension;
216223

217224
if (expectedFilename !== filenameWithExtension) {

packages/plugin/tests/match-document-filename.spec.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ ruleTester.runGraphQLTests<[MatchDocumentFilenameRuleConfig]>('match-document-fi
1212
},
1313
{
1414
filename: 'src/user-by-id.query.gql',
15-
code: `query USER_BY_ID { user { id } }`,
15+
code: 'query USER_BY_ID { user { id } }',
1616
options: [{ query: { style: 'kebab-case', suffix: '.query' } }],
1717
},
1818
{
1919
filename: 'src/createUserQuery.gql',
20-
code: `mutation CREATE_USER { user { id } }`,
20+
code: 'mutation CREATE_USER { user { id } }',
2121
options: [{ mutation: { style: 'camelCase', suffix: 'Query' } }],
2222
},
2323
{
2424
filename: 'src/NEW_USER.gql',
25-
code: `subscription new_user { user { id } }`,
25+
code: 'subscription new_user { user { id } }',
2626
options: [{ subscription: { style: 'UPPER_CASE' } }],
2727
},
2828
{
@@ -32,9 +32,14 @@ ruleTester.runGraphQLTests<[MatchDocumentFilenameRuleConfig]>('match-document-fi
3232
},
3333
{
3434
filename: 'src/UserById.gql',
35-
code: `query USER_BY_ID { user { id } }`,
35+
code: 'query USER_BY_ID { user { id } }',
3636
options: [{ query: { style: 'PascalCase' } }],
3737
},
38+
{
39+
filename: 'src/SAMEAsOperation.gql',
40+
code: 'query SAMEAsOperation { foo }',
41+
options: [{ query: 'matchDocumentStyle' }],
42+
},
3843
],
3944
invalid: [
4045
{
@@ -45,26 +50,26 @@ ruleTester.runGraphQLTests<[MatchDocumentFilenameRuleConfig]>('match-document-fi
4550
},
4651
{
4752
filename: 'src/user-by-id.gql',
48-
code: `query UserById { user { id } }`,
53+
code: 'query UserById { user { id } }',
4954
options: [{ query: { style: 'PascalCase' } }],
5055
errors: [{ message: `Unexpected filename "user-by-id.gql". Rename it to "UserById.gql"` }],
5156
},
5257
{
5358
filename: 'src/userById.gql',
54-
code: `query UserById { user { id } }`,
59+
code: 'query UserById { user { id } }',
5560
options: [{ query: { style: 'PascalCase', suffix: '.query' } }],
5661
errors: [{ message: `Unexpected filename "userById.gql". Rename it to "UserById.query.gql"` }],
5762
},
5863
{
5964
filename: 'src/user-fields.gql',
60-
code: `fragment UserFields on User { id }`,
65+
code: 'fragment UserFields on User { id }',
6166
options: [{ fragment: { style: 'PascalCase' } }],
6267
errors: [{ message: 'Unexpected filename "user-fields.gql". Rename it to "UserFields.gql"' }],
6368
},
6469
{
65-
// Compare only first operation name
70+
name: 'compare only first operation name',
6671
filename: 'src/getUsersQuery.gql',
67-
code: `query getUsers { users } mutation createPost { createPost }`,
72+
code: 'query getUsers { users } mutation createPost { createPost }',
6873
options: [
6974
{
7075
query: { style: 'PascalCase', suffix: '.query' },
@@ -74,7 +79,7 @@ ruleTester.runGraphQLTests<[MatchDocumentFilenameRuleConfig]>('match-document-fi
7479
errors: [{ message: 'Unexpected filename "getUsersQuery.gql". Rename it to "GetUsers.query.gql"' }],
7580
},
7681
{
77-
// Compare only first operation name if fragment is present
82+
name: 'compare only first operation name if fragment is present',
7883
filename: 'src/getUsersQuery.gql',
7984
code: /* GraphQL */ `
8085
fragment UserFields on User {

0 commit comments

Comments
 (0)