Skip to content

Commit c00aa8b

Browse files
committed
feat: add errorMessage for folder-match-with-fex
1 parent 6acc7ef commit c00aa8b

File tree

5 files changed

+161
-22
lines changed

5 files changed

+161
-22
lines changed

docs/rules/folder-match-with-fex.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,54 @@ bar/__tests__/foo.test.js
3333
The key is used to select target files, while the value is used to declare the naming pattern for their folder names. You can specify a different folder naming pattern for different target files. The plugin will only check files you explicitly provided:
3434

3535
```js
36-
module.exports = {
37-
plugins: ['check-file'],
38-
rules: {
39-
'check-file/folder-match-with-fex': [
40-
'error',
41-
{
42-
'*.test.{js,jsx,ts,tsx}': '**/__tests__/',
43-
'*.styled.{jsx,tsx}': '**/pages/',
44-
},
45-
],
36+
export default [
37+
{
38+
plugins: {
39+
'check-file': checkFile,
40+
},
41+
rules: {
42+
'check-file/folder-match-with-fex': [
43+
'error',
44+
{
45+
'*.test.{js,jsx,ts,tsx}': '**/__tests__/',
46+
'*.styled.{jsx,tsx}': '**/pages/',
47+
},
48+
],
49+
},
4650
},
47-
};
51+
];
52+
```
53+
54+
#### rule configuration object
55+
56+
##### `errorMessage`
57+
58+
Customizes the error message displayed when a file's folder does not match the naming pattern. It offers two placeholders for dynamic content:
59+
60+
- `{{ target }}`: Represents the target file.
61+
- `{{ pattern }}`: Represents the naming pattern for the target file's folder name.
62+
63+
```js
64+
export default [
65+
{
66+
plugins: {
67+
'check-file': checkFile,
68+
},
69+
rules: {
70+
'check-file/folder-match-with-fex': [
71+
'error',
72+
{
73+
'*.test.{js,jsx,ts,tsx}': '**/__tests__/',
74+
'*.styled.{jsx,tsx}': '**/pages/',
75+
},
76+
{
77+
errorMessage:
78+
'The folder of the file "{{ target }}" does not match the "{{ pattern }}" pattern, see contribute.md for details',
79+
},
80+
],
81+
},
82+
},
83+
];
4884
```
4985

5086
## Further Reading

lib/rules/folder-match-with-fex.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { getDocUrl } from '../utils/doc.js';
1212
import { getFilePath, getFilename, getFolderPath } from '../utils/filename.js';
1313
import { matchRule } from '../utils/rule.js';
14+
import { isNotEmpty } from '../utils/utility.js';
1415
import {
1516
globPatternValidator,
1617
validateNamingPatternObject,
@@ -36,6 +37,12 @@ export default {
3637
type: 'string',
3738
},
3839
},
40+
{
41+
type: 'object',
42+
properties: {
43+
errorMessage: { type: 'string' },
44+
},
45+
},
3946
],
4047
messages: {
4148
invalidObject: NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
@@ -68,6 +75,7 @@ export default {
6875
const filenameWithPath = getFilePath(context);
6976
const filename = getFilename(filenameWithPath);
7077
const folderPath = getFolderPath(filenameWithPath);
78+
const errorMessage = context.options[1]?.errorMessage ?? '';
7179

7280
for (const [fexPattern, folderPattern] of Object.entries(rules)) {
7381
const matchResult = matchRule(
@@ -78,14 +86,24 @@ export default {
7886
);
7987

8088
if (matchResult) {
81-
context.report({
82-
node,
83-
messageId: 'noMatch',
84-
data: {
85-
filenameWithPath,
86-
folderPattern,
87-
},
88-
});
89+
isNotEmpty(errorMessage)
90+
? context.report({
91+
node,
92+
// eslint-disable-next-line eslint-plugin/prefer-message-ids
93+
message: errorMessage,
94+
data: {
95+
target: filename,
96+
pattern: folderPattern,
97+
},
98+
})
99+
: context.report({
100+
node,
101+
messageId: 'noMatch',
102+
data: {
103+
filenameWithPath,
104+
folderPattern,
105+
},
106+
});
89107
return;
90108
}
91109
}

lib/rules/no-index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ export default {
3838
create(context) {
3939
return {
4040
Program: (node) => {
41-
const { ignoreMiddleExtensions, errorMessage } =
42-
context.options[0] || {};
41+
const ignoreMiddleExtensions =
42+
context.options[0]?.ignoreMiddleExtensions ?? false;
43+
const errorMessage = context.options[0]?.errorMessage ?? '';
4344
const filenameWithPath = getFilePath(context);
4445
const filename = getFilename(filenameWithPath);
4546
const basename = getBasename(filename, ignoreMiddleExtensions);
4647

4748
if (basename === 'index') {
48-
errorMessage && isNotEmpty(errorMessage)
49+
isNotEmpty(errorMessage)
4950
? context.report({
5051
node,
5152
// eslint-disable-next-line eslint-plugin/prefer-message-ids

tests/lib/rules/folder-match-with-fex.posix.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,45 @@ ruleTester.run('folder-match-with-fex with option: []', rule, {
332332
},
333333
],
334334
});
335+
336+
ruleTester.run(
337+
"folder-match-with-fex with option: [{ '*.test.js': '*/__tests__/' }, { errorMessage: 'The folder of the file {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details' }]",
338+
rule,
339+
{
340+
valid: [
341+
{
342+
code: "var foo = 'bar';",
343+
filename: 'bar/__tests__/foo.test.js',
344+
options: [
345+
{ '*.test.js': '*/__tests__/' },
346+
{
347+
errorMessage:
348+
'The folder of the file {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details',
349+
},
350+
],
351+
},
352+
],
353+
354+
invalid: [
355+
{
356+
code: "var foo = 'bar';",
357+
filename: 'bar/_tests_/foo.test.js',
358+
options: [
359+
{ '*.test.js': '*/__tests__/' },
360+
{
361+
errorMessage:
362+
'The folder of the file {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details',
363+
},
364+
],
365+
errors: [
366+
{
367+
message:
368+
'The folder of the file foo.test.js does not match the */__tests__/ pattern, see contribute.md for details',
369+
column: 1,
370+
line: 1,
371+
},
372+
],
373+
},
374+
],
375+
}
376+
);

tests/lib/rules/folder-match-with-fex.windows.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,45 @@ ruleTester.run('folder-match-with-fex with option on Windows: []', rule, {
331331
},
332332
],
333333
});
334+
335+
ruleTester.run(
336+
"folder-match-with-fex with option on Windows: [{ '*.test.js': '*/__tests__/' }, { errorMessage: 'The folder of the file {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details' }]",
337+
rule,
338+
{
339+
valid: [
340+
{
341+
code: "var foo = 'bar';",
342+
filename: 'bar\\__tests__\\foo.test.js',
343+
options: [
344+
{ '*.test.js': '*/__tests__/' },
345+
{
346+
errorMessage:
347+
'The folder of the file {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details',
348+
},
349+
],
350+
},
351+
],
352+
353+
invalid: [
354+
{
355+
code: "var foo = 'bar';",
356+
filename: 'bar\\_tests_\\foo.test.js',
357+
options: [
358+
{ '*.test.js': '*/__tests__/' },
359+
{
360+
errorMessage:
361+
'The folder of the file {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details',
362+
},
363+
],
364+
errors: [
365+
{
366+
message:
367+
'The folder of the file foo.test.js does not match the */__tests__/ pattern, see contribute.md for details',
368+
column: 1,
369+
line: 1,
370+
},
371+
],
372+
},
373+
],
374+
}
375+
);

0 commit comments

Comments
 (0)