Skip to content

Commit 7880ddd

Browse files
committed
feat: add errorMessage for no-index
1 parent d6eea15 commit 7880ddd

File tree

5 files changed

+136
-16
lines changed

5 files changed

+136
-16
lines changed

.prettierrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"trailingComma": "es5",
77
"bracketSpacing": true,
88
"arrowParens": "always",
9-
"embeddedLanguageFormatting": "auto"
9+
"embeddedLanguageFormatting": "auto",
10+
"endOfLine": "auto"
1011
}

docs/rules/no-index.md

+39-10
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,44 @@ If `true`, the rule will ignore the middle extensions of the filename.
3131
In some cases, you may want to ignore the middle extensions of the filename. For example, you want to lint the base name of the config files, e.g. `index.config.js`, you can do so by setting the `ignoreMiddleExtensions` option to `true`, and the rule will only validate its base name, in this case the base name will be `index`.
3232

3333
```js
34-
module.exports = {
35-
plugins: ['check-file'],
36-
rules: {
37-
'check-file/no-index': [
38-
'error',
39-
{
40-
ignoreMiddleExtensions: true,
41-
},
42-
],
34+
export default [
35+
{
36+
plugins: {
37+
'check-file': checkFile,
38+
},
39+
rules: {
40+
'check-file/no-index': [
41+
'error',
42+
{
43+
ignoreMiddleExtensions: true,
44+
},
45+
],
46+
},
4347
},
44-
};
48+
];
49+
```
50+
51+
##### `errorMessage`
52+
53+
Customizes the error message displayed when a file is being named "index". It offers one placeholder for dynamic content:
54+
55+
- `{{ target }}`: Represents the filename of the blocked file.
56+
57+
```js
58+
export default [
59+
{
60+
plugins: {
61+
'check-file': checkFile,
62+
},
63+
rules: {
64+
'check-file/no-index': [
65+
'error',
66+
{
67+
errorMessage:
68+
'The file "{{ target }}" is not allowed to be named "index", see contribute.md for details',
69+
},
70+
],
71+
},
72+
},
73+
];
4574
```

lib/rules/no-index.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { NO_INDEX_ERROR_MESSAGE } from '../constants/message.js';
77
import { getDocUrl } from '../utils/doc.js';
88
import { getBasename, getFilePath, getFilename } from '../utils/filename.js';
9+
import { isNotEmpty } from '../utils/utility.js';
910

1011
/**
1112
* @type {import('eslint').Rule.RuleModule}
@@ -25,6 +26,7 @@ export default {
2526
type: 'object',
2627
properties: {
2728
ignoreMiddleExtensions: { type: 'boolean' },
29+
errorMessage: { type: 'string' },
2830
},
2931
},
3032
],
@@ -36,16 +38,26 @@ export default {
3638
create(context) {
3739
return {
3840
Program: (node) => {
39-
const { ignoreMiddleExtensions } = context.options[0] || {};
41+
const { ignoreMiddleExtensions, errorMessage } =
42+
context.options[0] || {};
4043
const filenameWithPath = getFilePath(context);
4144
const filename = getFilename(filenameWithPath);
4245
const basename = getBasename(filename, ignoreMiddleExtensions);
4346

4447
if (basename === 'index') {
45-
context.report({
46-
node,
47-
messageId: 'noIndex',
48-
});
48+
errorMessage && isNotEmpty(errorMessage)
49+
? context.report({
50+
node,
51+
// eslint-disable-next-line eslint-plugin/prefer-message-ids
52+
message: errorMessage,
53+
data: {
54+
target: filename,
55+
},
56+
})
57+
: context.report({
58+
node,
59+
messageId: 'noIndex',
60+
});
4961
return;
5062
}
5163
},

tests/lib/rules/no-index.posix.js

+39
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,42 @@ ruleTester.run(
131131
],
132132
}
133133
);
134+
135+
ruleTester.run(
136+
'no-index with option: [{ errorMessage: "The file {{ target }} is not allowed to be named index" }]',
137+
rule,
138+
{
139+
valid: [
140+
{
141+
code: "var foo = 'bar';",
142+
filename: 'index.config.ts',
143+
options: [
144+
{
145+
errorMessage:
146+
'The file {{ target }} is not allowed to be named index',
147+
},
148+
],
149+
},
150+
],
151+
152+
invalid: [
153+
{
154+
code: "var foo = 'bar';",
155+
filename: 'src/utils/index.js',
156+
options: [
157+
{
158+
errorMessage:
159+
'The file {{ target }} is not allowed to be named index',
160+
},
161+
],
162+
errors: [
163+
{
164+
message: 'The file index.js is not allowed to be named index',
165+
column: 1,
166+
line: 1,
167+
},
168+
],
169+
},
170+
],
171+
}
172+
);

tests/lib/rules/no-index.windows.js

+39
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,42 @@ ruleTester.run(
131131
],
132132
}
133133
);
134+
135+
ruleTester.run(
136+
'no-index with option on Windows: [{ errorMessage: "The file {{ target }} is not allowed to be named index" }]',
137+
rule,
138+
{
139+
valid: [
140+
{
141+
code: "var foo = 'bar';",
142+
filename: 'index.config.ts',
143+
options: [
144+
{
145+
errorMessage:
146+
'The file {{ target }} is not allowed to be named index',
147+
},
148+
],
149+
},
150+
],
151+
152+
invalid: [
153+
{
154+
code: "var foo = 'bar';",
155+
filename: 'src\\utils\\index.js',
156+
options: [
157+
{
158+
errorMessage:
159+
'The file {{ target }} is not allowed to be named index',
160+
},
161+
],
162+
errors: [
163+
{
164+
message: 'The file index.js is not allowed to be named index',
165+
column: 1,
166+
line: 1,
167+
},
168+
],
169+
},
170+
],
171+
}
172+
);

0 commit comments

Comments
 (0)