Skip to content

Commit 6f6e9b9

Browse files
committed
feat: add errorMessage for folder-naming-convention
1 parent 9257993 commit 6f6e9b9

File tree

4 files changed

+156
-19
lines changed

4 files changed

+156
-19
lines changed

docs/rules/folder-naming-convention.md

+47-11
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,54 @@ While `NEXT_JS_APP_ROUTER_CASE` covers many naming cases, it's possible that som
9494
The key is used to select target folders, while the value is used to declare the naming pattern for the folder name. You can specify a different naming pattern for different target folders. The plugin will only check folders you explicitly provided:
9595

9696
```js
97-
module.exports = {
98-
plugins: ['check-file'],
99-
rules: {
100-
'check-file/folder-naming-convention': [
101-
'error',
102-
{
103-
'src/**/': 'CAMEL_CASE',
104-
'mocks/*/': 'KEBAB_CASE',
105-
},
106-
],
97+
export default [
98+
{
99+
plugins: {
100+
'check-file': checkFile,
101+
},
102+
rules: {
103+
'check-file/folder-naming-convention': [
104+
'error',
105+
{
106+
'src/**/': 'CAMEL_CASE',
107+
'mocks/*/': 'KEBAB_CASE',
108+
},
109+
],
110+
},
107111
},
108-
};
112+
];
113+
```
114+
115+
#### rule configuration object
116+
117+
##### `errorMessage`
118+
119+
Customizes the error message displayed when a folder does not match the naming pattern. It offers two placeholders for dynamic content:
120+
121+
- `{{ target }}`: Represents the target folder.
122+
- `{{ pattern }}`: Represents the naming pattern for the target folder.
123+
124+
```js
125+
export default [
126+
{
127+
plugins: {
128+
'check-file': checkFile,
129+
},
130+
rules: {
131+
'check-file/folder-naming-convention': [
132+
'error',
133+
{
134+
'src/**/': 'CAMEL_CASE',
135+
'mocks/*/': 'KEBAB_CASE',
136+
},
137+
{
138+
errorMessage:
139+
'The folder "{{ target }}" does not match the "{{ pattern }}" pattern, see contribute.md for details',
140+
},
141+
],
142+
},
143+
},
144+
];
109145
```
110146

111147
## Further Reading

lib/rules/folder-naming-convention.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export default {
4444
type: 'string',
4545
},
4646
},
47+
{
48+
type: 'object',
49+
properties: {
50+
errorMessage: { type: 'string' },
51+
},
52+
},
4753
],
4854
messages: {
4955
invalidObject: NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
@@ -75,6 +81,7 @@ export default {
7581

7682
const filenameWithPath = getFilePath(context);
7783
const folderPath = getFolderPath(filenameWithPath);
84+
const errorMessage = context.options[1]?.errorMessage ?? '';
7885

7986
for (const [folderPattern, namingPattern] of Object.entries(rules)) {
8087
if (
@@ -102,14 +109,24 @@ export default {
102109
namingPattern
103110
)
104111
) {
105-
context.report({
106-
node,
107-
messageId: 'noMatch',
108-
data: {
109-
folder,
110-
namingPattern,
111-
},
112-
});
112+
isNotEmpty(errorMessage)
113+
? context.report({
114+
node,
115+
// eslint-disable-next-line eslint-plugin/prefer-message-ids
116+
message: errorMessage,
117+
data: {
118+
target: folder,
119+
pattern: namingPattern,
120+
},
121+
})
122+
: context.report({
123+
node,
124+
messageId: 'noMatch',
125+
data: {
126+
folder,
127+
namingPattern,
128+
},
129+
});
113130
return;
114131
}
115132
}

tests/lib/rules/folder-naming-convention.posix.js

+42
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,45 @@ ruleTester.run(
562562
],
563563
}
564564
);
565+
566+
ruleTester.run(
567+
"folder-naming-convention with option: [{ 'src/**/': 'CAMEL_CASE' }, { errorMessage: 'The folder {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details' }]",
568+
rule,
569+
{
570+
valid: [
571+
{
572+
code: "var foo = 'bar';",
573+
filename: 'src/components/displayLabel/displayLabel.js',
574+
options: [
575+
{ 'src/**/': 'CAMEL_CASE' },
576+
{
577+
errorMessage:
578+
'The folder {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details',
579+
},
580+
],
581+
},
582+
],
583+
584+
invalid: [
585+
{
586+
code: "var foo = 'bar';",
587+
filename: 'src/components/DisplayLabel/displayLabel.js',
588+
options: [
589+
{ 'src/**/': 'CAMEL_CASE' },
590+
{
591+
errorMessage:
592+
'The folder {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details',
593+
},
594+
],
595+
errors: [
596+
{
597+
message:
598+
'The folder DisplayLabel does not match the CAMEL_CASE pattern, see contribute.md for details',
599+
column: 1,
600+
line: 1,
601+
},
602+
],
603+
},
604+
],
605+
}
606+
);

tests/lib/rules/folder-naming-convention.windows.js

+42
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,45 @@ ruleTester.run(
565565
],
566566
}
567567
);
568+
569+
ruleTester.run(
570+
"folder-naming-convention with option on Windows: [{ 'src/**/': 'CAMEL_CASE' }, { errorMessage: 'The folder {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details' }]",
571+
rule,
572+
{
573+
valid: [
574+
{
575+
code: "var foo = 'bar';",
576+
filename: 'src\\components\\displayLabel\\displayLabel.js',
577+
options: [
578+
{ 'src/**/': 'CAMEL_CASE' },
579+
{
580+
errorMessage:
581+
'The folder {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details',
582+
},
583+
],
584+
},
585+
],
586+
587+
invalid: [
588+
{
589+
code: "var foo = 'bar';",
590+
filename: 'src\\components\\DisplayLabel\\displayLabel.js',
591+
options: [
592+
{ 'src/**/': 'CAMEL_CASE' },
593+
{
594+
errorMessage:
595+
'The folder {{ target }} does not match the {{ pattern }} pattern, see contribute.md for details',
596+
},
597+
],
598+
errors: [
599+
{
600+
message:
601+
'The folder DisplayLabel does not match the CAMEL_CASE pattern, see contribute.md for details',
602+
column: 1,
603+
line: 1,
604+
},
605+
],
606+
},
607+
],
608+
}
609+
);

0 commit comments

Comments
 (0)