Skip to content

Commit

Permalink
feat: add errorMessage for no-index
Browse files Browse the repository at this point in the history
  • Loading branch information
dukeluo committed Dec 7, 2024
1 parent d6eea15 commit 7880ddd
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "always",
"embeddedLanguageFormatting": "auto"
"embeddedLanguageFormatting": "auto",
"endOfLine": "auto"
}
49 changes: 39 additions & 10 deletions docs/rules/no-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,44 @@ If `true`, the rule will ignore the middle extensions of the filename.
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`.

```js
module.exports = {
plugins: ['check-file'],
rules: {
'check-file/no-index': [
'error',
{
ignoreMiddleExtensions: true,
},
],
export default [
{
plugins: {
'check-file': checkFile,
},
rules: {
'check-file/no-index': [
'error',
{
ignoreMiddleExtensions: true,
},
],
},
},
};
];
```

##### `errorMessage`

Customizes the error message displayed when a file is being named "index". It offers one placeholder for dynamic content:

- `{{ target }}`: Represents the filename of the blocked file.

```js
export default [
{
plugins: {
'check-file': checkFile,
},
rules: {
'check-file/no-index': [
'error',
{
errorMessage:
'The file "{{ target }}" is not allowed to be named "index", see contribute.md for details',
},
],
},
},
];
```
22 changes: 17 additions & 5 deletions lib/rules/no-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { NO_INDEX_ERROR_MESSAGE } from '../constants/message.js';
import { getDocUrl } from '../utils/doc.js';
import { getBasename, getFilePath, getFilename } from '../utils/filename.js';
import { isNotEmpty } from '../utils/utility.js';

/**
* @type {import('eslint').Rule.RuleModule}
Expand All @@ -25,6 +26,7 @@ export default {
type: 'object',
properties: {
ignoreMiddleExtensions: { type: 'boolean' },
errorMessage: { type: 'string' },
},
},
],
Expand All @@ -36,16 +38,26 @@ export default {
create(context) {
return {
Program: (node) => {
const { ignoreMiddleExtensions } = context.options[0] || {};
const { ignoreMiddleExtensions, errorMessage } =
context.options[0] || {};
const filenameWithPath = getFilePath(context);
const filename = getFilename(filenameWithPath);
const basename = getBasename(filename, ignoreMiddleExtensions);

if (basename === 'index') {
context.report({
node,
messageId: 'noIndex',
});
errorMessage && isNotEmpty(errorMessage)
? context.report({
node,
// eslint-disable-next-line eslint-plugin/prefer-message-ids
message: errorMessage,
data: {
target: filename,
},
})
: context.report({
node,
messageId: 'noIndex',
});
return;
}
},
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/rules/no-index.posix.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,42 @@ ruleTester.run(
],
}
);

ruleTester.run(
'no-index with option: [{ errorMessage: "The file {{ target }} is not allowed to be named index" }]',
rule,
{
valid: [
{
code: "var foo = 'bar';",
filename: 'index.config.ts',
options: [
{
errorMessage:
'The file {{ target }} is not allowed to be named index',
},
],
},
],

invalid: [
{
code: "var foo = 'bar';",
filename: 'src/utils/index.js',
options: [
{
errorMessage:
'The file {{ target }} is not allowed to be named index',
},
],
errors: [
{
message: 'The file index.js is not allowed to be named index',
column: 1,
line: 1,
},
],
},
],
}
);
39 changes: 39 additions & 0 deletions tests/lib/rules/no-index.windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,42 @@ ruleTester.run(
],
}
);

ruleTester.run(
'no-index with option on Windows: [{ errorMessage: "The file {{ target }} is not allowed to be named index" }]',
rule,
{
valid: [
{
code: "var foo = 'bar';",
filename: 'index.config.ts',
options: [
{
errorMessage:
'The file {{ target }} is not allowed to be named index',
},
],
},
],

invalid: [
{
code: "var foo = 'bar';",
filename: 'src\\utils\\index.js',
options: [
{
errorMessage:
'The file {{ target }} is not allowed to be named index',
},
],
errors: [
{
message: 'The file index.js is not allowed to be named index',
column: 1,
line: 1,
},
],
},
],
}
);

0 comments on commit 7880ddd

Please sign in to comment.