Skip to content

Commit ebb423b

Browse files
committed
feat(#35): add new errorMessage params for rule filename-naming-convention
1 parent e0b1e5f commit ebb423b

5 files changed

+105
-12
lines changed

docs/rules/filename-naming-convention.md

+23
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ module.exports = {
130130
};
131131
```
132132

133+
##### `errorMessage`
134+
135+
Customizes the error message displayed when a file's filename doesn't match the declared naming pattern. It offers two placeholders for dynamic content:
136+
137+
- `{{ target }}`: Represents the filename of the non-matching file.
138+
- `{{ pattern }}`: Represents the naming pattern.
139+
140+
```js
141+
module.exports = {
142+
plugins: ['check-file'],
143+
rules: {
144+
'check-file/filename-naming-convention': [
145+
'error',
146+
{ '**/*/!(index).*': '<1>' },
147+
{
148+
errorMessage:
149+
'The file "{{ target }}" does not match file naming convention defined("{{ pattern }}") for this project, see contribute.md for details',
150+
},
151+
],
152+
},
153+
};
154+
```
155+
133156
## Further Reading
134157

135158
- [micromatch](https://github.com/micromatch/micromatch)

lib/rules/filename-blocklist.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ export default {
5454
return {
5555
Program: (node) => {
5656
const rules = context.options[0];
57-
const { errorMessage } = context.options[1] || {
58-
errorMessage: '',
59-
};
57+
const errorMessage = context.options[1]?.errorMessage ?? '';
6058
const error = validateNamingPatternObject(
6159
rules,
6260
globPatternValidator,

lib/rules/filename-naming-convention.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
matchRule,
1616
transformRuleWithPrefinedMatchSyntax,
1717
} from '../utils/rule.js';
18+
import { isNotEmpty } from '../utils/utility.js';
1819
import {
1920
filenameNamingPatternValidator,
2021
globPatternValidator,
@@ -44,6 +45,7 @@ export default {
4445
type: 'object',
4546
properties: {
4647
ignoreMiddleExtensions: { type: 'boolean' },
48+
errorMessage: { type: 'string' },
4749
},
4850
},
4951
],
@@ -78,7 +80,9 @@ export default {
7880

7981
const filenameWithPath = getFilePath(context);
8082
const filename = getFilename(filenameWithPath);
81-
const { ignoreMiddleExtensions } = context.options[1] || {};
83+
const ignoreMiddleExtensions =
84+
context.options[1]?.ignoreMiddleExtensions ?? false;
85+
const errorMessage = context.options[1]?.errorMessage ?? '';
8286

8387
for (const [
8488
originalFilenamePattern,
@@ -108,11 +112,21 @@ export default {
108112
};
109113
}
110114
} catch (error) {
111-
context.report({
112-
node,
113-
messageId: error.type,
114-
data: error.payload,
115-
});
115+
isNotEmpty(errorMessage) && error.type === 'noMatch'
116+
? context.report({
117+
node,
118+
// eslint-disable-next-line eslint-plugin/prefer-message-ids
119+
message: errorMessage,
120+
data: {
121+
target: error.payload.filename,
122+
pattern: error.payload.originalNamingPattern,
123+
},
124+
})
125+
: context.report({
126+
node,
127+
messageId: error.type,
128+
data: error.payload,
129+
});
116130
}
117131
}
118132
},

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

+29
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,35 @@ ruleTester.run(
13921392
}
13931393
);
13941394

1395+
ruleTester.run(
1396+
"filename-naming-convention with option: [{ '**/*/!(index).*': '<1>' }, { errorMessage: 'The file \"{{ target }}\" does not match file naming convention defined(\"{{ pattern }}\") for this project, see contribute.md for details'}]",
1397+
rule,
1398+
{
1399+
valid: [],
1400+
invalid: [
1401+
{
1402+
code: "var foo = 'bar';",
1403+
filename: 'src/components/featureA/featureB.jsx',
1404+
options: [
1405+
{ '**/*/!(index).*': '<1>' },
1406+
{
1407+
errorMessage:
1408+
'The file "{{ target }}" does not match file naming convention defined("{{ pattern }}") for this project, see contribute.md for details',
1409+
},
1410+
],
1411+
errors: [
1412+
{
1413+
message:
1414+
'The file "featureB.jsx" does not match file naming convention defined("<1>") for this project, see contribute.md for details',
1415+
column: 1,
1416+
line: 1,
1417+
},
1418+
],
1419+
},
1420+
],
1421+
}
1422+
);
1423+
13951424
ruleTester.run('filename-naming-convention with option: []', rule, {
13961425
valid: [],
13971426
invalid: [

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

+32-3
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ ruleTester.run(
861861
);
862862

863863
ruleTester.run(
864-
"filename-naming-convention with option: [{ '**/*/!(index).*': '<1>' }, { ignoreMiddleExtensions: true }]",
864+
"filename-naming-convention with option on Windows: [{ '**/*/!(index).*': '<1>' }, { ignoreMiddleExtensions: true }]",
865865
rule,
866866
{
867867
valid: [
@@ -929,7 +929,7 @@ ruleTester.run(
929929
);
930930

931931
ruleTester.run(
932-
"filename-naming-convention with option: [{ '**/*/!(index).*': '<9>' }]",
932+
"filename-naming-convention with option on Windows: [{ '**/*/!(index).*': '<9>' }]",
933933
rule,
934934
{
935935
valid: [],
@@ -951,7 +951,36 @@ ruleTester.run(
951951
}
952952
);
953953

954-
ruleTester.run('filename-naming-convention with option: []', rule, {
954+
ruleTester.run(
955+
"filename-naming-convention with option on Windows: [{ '**/*/!(index).*': '<1>' }, { errorMessage: 'The file \"{{ target }}\" does not match file naming convention defined(\"{{ pattern }}\") for this project, see contribute.md for details'}]",
956+
rule,
957+
{
958+
valid: [],
959+
invalid: [
960+
{
961+
code: "var foo = 'bar';",
962+
filename: 'src\\components\\featureA\\featureB.jsx',
963+
options: [
964+
{ '**/*/!(index).*': '<1>' },
965+
{
966+
errorMessage:
967+
'The file "{{ target }}" does not match file naming convention defined("{{ pattern }}") for this project, see contribute.md for details',
968+
},
969+
],
970+
errors: [
971+
{
972+
message:
973+
'The file "featureB.jsx" does not match file naming convention defined("<1>") for this project, see contribute.md for details',
974+
column: 1,
975+
line: 1,
976+
},
977+
],
978+
},
979+
],
980+
}
981+
);
982+
983+
ruleTester.run('filename-naming-convention with option on Windows: []', rule, {
955984
valid: [],
956985
invalid: [
957986
{

0 commit comments

Comments
 (0)