Skip to content

Commit 1bc47a0

Browse files
Add ignore option to the filename-case rule (#431)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 393e8e1 commit 1bc47a0

File tree

3 files changed

+338
-13
lines changed

3 files changed

+338
-13
lines changed

docs/rules/filename-case.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Characters in the filename except `a-z`, `A-Z`, `0-9`, `-`, `_` and `$` are igno
3333

3434
## Options
3535

36+
### case
37+
38+
Type: `string`
39+
3640
You can set the `case` option like this:
3741

3842
```js
@@ -44,7 +48,11 @@ You can set the `case` option like this:
4448
]
4549
```
4650

47-
Or set the `cases` option to allow multiple cases:
51+
### cases
52+
53+
Type: `{[type: string]: string}`
54+
55+
You can set the `cases` option to allow multiple cases:
4856

4957
```js
5058
"unicorn/filename-case": [
@@ -57,3 +65,36 @@ Or set the `cases` option to allow multiple cases:
5765
}
5866
]
5967
```
68+
69+
### ignore
70+
71+
Type: `Array<string | RegExp>`\
72+
Default: `[]`
73+
74+
Filenames to ignore.
75+
76+
When a string is given, it's interpreted as a regular expressions inside a string. Needed for ESLint config in JSON.
77+
78+
Sometimes you may have non-standard filenames in a project. This option lets you ignore those files.
79+
80+
For example:
81+
- Vendor packages that are not published and was copy-pasted.
82+
- Ignore some files when you use [eslint-plugin-markdown](https://github.com/eslint/eslint-plugin-markdown), for example `README.md`.
83+
- Some tools may require special names for some files.
84+
85+
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$'`.
86+
87+
```js
88+
"unicorn/filename-case": [
89+
"error",
90+
{
91+
"case": "kebabCase",
92+
"ignore": [
93+
"^FOOBAR\\.js$",
94+
"^(B|b)az",
95+
"\\.SOMETHING\\.js$",
96+
/^vendor/i
97+
]
98+
}
99+
]
100+
```

rules/filename-case.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,17 @@ const cases = {
5656
/**
5757
Get the cases specified by the option.
5858
59-
@param {unknown} context
59+
@param {object} options
6060
@returns {string[]} The chosen cases.
6161
*/
62-
function getChosenCases(context) {
63-
const option = context.options[0] || {};
64-
65-
if (option.case) {
66-
return [option.case];
62+
function getChosenCases(options) {
63+
if (options.case) {
64+
return [options.case];
6765
}
6866

69-
if (option.cases) {
70-
const cases = Object.keys(option.cases)
71-
.filter(cases => option.cases[cases]);
67+
if (options.cases) {
68+
const cases = Object.keys(options.cases)
69+
.filter(cases => options.cases[cases]);
7270

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

146144
const create = context => {
147-
const chosenCases = getChosenCases(context);
145+
const options = context.options[0] || {};
146+
const chosenCases = getChosenCases(options);
147+
const ignore = (options.ignore || []).map(item => {
148+
if (item instanceof RegExp) {
149+
return item;
150+
}
151+
152+
return new RegExp(item, 'u');
153+
});
148154
const chosenCasesFunctions = chosenCases.map(case_ => ignoreNumbers(cases[case_].fn));
149155
const filenameWithExtension = context.getFilename();
150156

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

160-
if (filename + extension === 'index.js') {
167+
if (base === 'index.js' || ignore.some(regexp => regexp.test(base))) {
161168
return;
162169
}
163170

@@ -199,6 +206,10 @@ const schema = [{
199206
'kebabCase',
200207
'pascalCase'
201208
]
209+
},
210+
ignore: {
211+
type: 'array',
212+
uniqueItems: true
202213
}
203214
},
204215
additionalProperties: false
@@ -221,6 +232,10 @@ const schema = [{
221232
}
222233
},
223234
additionalProperties: false
235+
},
236+
ignore: {
237+
type: 'array',
238+
uniqueItems: true
224239
}
225240
},
226241
additionalProperties: false

0 commit comments

Comments
 (0)