Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore option to the filename-case rule #431

43 changes: 42 additions & 1 deletion docs/rules/filename-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Characters in the filename except `a-z`, `A-Z`, `0-9`, `-`, `_` and `$` are igno

## Options

### case

Type: `string`

You can set the `case` option like this:

```js
Expand All @@ -44,7 +48,11 @@ You can set the `case` option like this:
]
```

Or set the `cases` option to allow multiple cases:
### cases

Type: `{[type: string]: string}`

You can set the `cases` option to allow multiple cases:

```js
"unicorn/filename-case": [
Expand All @@ -57,3 +65,36 @@ Or set the `cases` option to allow multiple cases:
}
]
```

### ignore

Type: `Array<string | RegExp>`\
Default: `[]`

Filenames to ignore.

sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explicitly say that if a string is given, it's interpreted as a regex inside a string.

When a string is given, it's interpreted as a regular expressions inside a string. Needed for ESLint config in JSON.

Sometimes you may have non-standard filenames in a project. This option lets you ignore those files.

For example:
- Vendor packages that are not published and was copy-pasted.
- Ignore some files when you use [eslint-plugin-markdown](https://github.com/eslint/eslint-plugin-markdown), for example `README.md`.
- Some tools may require special names for some files.

Don't forget that you must escape special characters that you don't want to be interpreted as part of the regex, for example, if you have `[` in the actual filename. For example, to match `[id].js`, use `/^\[id\]\.js$/"` or `'^\\[id\\]\\.js$'`.

```js
"unicorn/filename-case": [
"error",
{
"case": "kebabCase",
"ignore": [
"^FOOBAR\\.js$",
"^(B|b)az",
"\\.SOMETHING\\.js$",
/^vendor/i
]
}
]
```
37 changes: 26 additions & 11 deletions rules/filename-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,17 @@ const cases = {
/**
Get the cases specified by the option.

@param {unknown} context
@param {object} options
@returns {string[]} The chosen cases.
*/
function getChosenCases(context) {
const option = context.options[0] || {};

if (option.case) {
return [option.case];
function getChosenCases(options) {
if (options.case) {
return [options.case];
}

if (option.cases) {
const cases = Object.keys(option.cases)
.filter(cases => option.cases[cases]);
if (options.cases) {
const cases = Object.keys(options.cases)
.filter(cases => options.cases[cases]);

return cases.length > 0 ? cases : ['kebabCase'];
}
Expand Down Expand Up @@ -144,7 +142,15 @@ function englishishJoinWords(words) {
}

const create = context => {
const chosenCases = getChosenCases(context);
const options = context.options[0] || {};
const chosenCases = getChosenCases(options);
const ignore = (options.ignore || []).map(item => {
if (item instanceof RegExp) {
return item;
}

return new RegExp(item, 'u');
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
});
const chosenCasesFunctions = chosenCases.map(case_ => ignoreNumbers(cases[case_].fn));
const filenameWithExtension = context.getFilename();

Expand All @@ -156,8 +162,9 @@ const create = context => {
Program: node => {
const extension = path.extname(filenameWithExtension);
const filename = path.basename(filenameWithExtension, extension);
const base = filename + extension;

if (filename + extension === 'index.js') {
if (base === 'index.js' || ignore.some(regexp => regexp.test(base))) {
return;
}

Expand Down Expand Up @@ -199,6 +206,10 @@ const schema = [{
'kebabCase',
'pascalCase'
]
},
ignore: {
type: 'array',
uniqueItems: true
}
},
additionalProperties: false
Expand All @@ -221,6 +232,10 @@ const schema = [{
}
},
additionalProperties: false
},
ignore: {
type: 'array',
uniqueItems: true
}
},
additionalProperties: false
Expand Down
Loading