Skip to content

Commit b1f2730

Browse files
New option prefix for match-document-filename rule (#1141)
* prefix add spec valid invalid invalid changeset minor changeset fix changeset * fix Co-authored-by: Dimitri POSTOLOV <[email protected]>
1 parent 6cc3717 commit b1f2730

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

.changeset/light-lizards-know.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+
add new option `prefix` for `match-document-filename` rule

docs/rules/match-document-filename.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ query UserById {
8585
}
8686
```
8787

88+
### Correct
89+
90+
```graphql
91+
# eslint @graphql-eslint/match-document-filename: ['error', { fragment: { style: 'kebab-case', prefix: 'mutation.' } }]
92+
93+
# mutation.add-alert.graphql
94+
mutation addAlert {
95+
foo
96+
}
97+
```
98+
99+
### Correct
100+
101+
```graphql
102+
# eslint @graphql-eslint/match-document-filename: ['error', { fragment: { prefix: 'query.' } }]
103+
104+
# query.me.graphql
105+
query me {
106+
foo
107+
}
108+
```
109+
88110
## Config Schema
89111

90112
The schema defines the following properties:
@@ -151,6 +173,8 @@ This element must be one of the following enum values:
151173

152174
### `suffix` (string)
153175

176+
### `prefix` (string)
177+
154178
## Resources
155179

156180
- [Rule source](../../packages/plugin/src/rules/match-document-filename.ts)

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const schema = {
3737
properties: {
3838
style: { enum: CASE_STYLES },
3939
suffix: { type: 'string' },
40+
prefix: { type: 'string' },
4041
},
4142
},
4243
},
@@ -137,6 +138,26 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
137138
}
138139
`,
139140
},
141+
{
142+
title: 'Correct',
143+
usage: [{ fragment: { style: 'kebab-case', prefix: 'mutation.' } }],
144+
code: /* GraphQL */ `
145+
# mutation.add-alert.graphql
146+
mutation addAlert {
147+
foo
148+
}
149+
`,
150+
},
151+
{
152+
title: 'Correct',
153+
usage: [{ fragment: { prefix: 'query.' } }],
154+
code: /* GraphQL */ `
155+
# query.me.graphql
156+
query me {
157+
foo
158+
}
159+
`,
160+
},
140161
],
141162
configOptions: [
142163
{
@@ -211,12 +232,13 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
211232
option = { style: option };
212233
}
213234
const expectedExtension = options.fileExtension || fileExtension;
214-
let expectedFilename: string;
235+
let expectedFilename = option.prefix || '';
236+
215237
if (option.style) {
216-
expectedFilename =
238+
expectedFilename +=
217239
option.style === 'matchDocumentStyle' ? docName : convertCase(option.style, docName);
218240
} else {
219-
expectedFilename = filename;
241+
expectedFilename += filename;
220242
}
221243
expectedFilename += (option.suffix || '') + expectedExtension;
222244
const filenameWithExtension = filename + expectedExtension;

packages/plugin/tests/__snapshots__/match-document-filename.spec.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ exports[`Invalid #4 1`] = `
7575
| ^ Unexpected filename "user-fields.gql". Rename it to "UserFields.gql"
7676
`;
7777

78+
exports[`Invalid #7 1`] = `
79+
#### ⌨️ Code
80+
81+
1 | mutation addAlertChannel {
82+
2 | foo
83+
3 | }
84+
85+
#### ⚙️ Options
86+
87+
{
88+
"mutation": {
89+
"prefix": "mutation."
90+
}
91+
}
92+
93+
#### ❌ Error
94+
95+
> 1 | mutation addAlertChannel {
96+
| ^ Unexpected filename "add-alert-channel.graphql". Rename it to "mutation.add-alert-channel.graphql"
97+
2 | foo
98+
`;
99+
78100
exports[`compare only first operation name 1`] = `
79101
#### ⌨️ Code
80102

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,20 @@ ruleTester.runGraphQLTests<RuleOptions>('match-document-filename', rule, {
105105
{ message: 'Unexpected filename "getUsersQuery.gql". Rename it to "GetUsers.query.gql"' },
106106
],
107107
},
108+
{
109+
filename: 'add-alert-channel.graphql',
110+
code: /* GraphQL */ `
111+
mutation addAlertChannel {
112+
foo
113+
}
114+
`,
115+
options: [{ mutation: { prefix: 'mutation.' } }],
116+
errors: [
117+
{
118+
message:
119+
'Unexpected filename "add-alert-channel.graphql". Rename it to "mutation.add-alert-channel.graphql"',
120+
},
121+
],
122+
},
108123
],
109124
});

0 commit comments

Comments
 (0)