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

13 changes: 11 additions & 2 deletions docs/rules/filename-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,19 @@ You can set the `cases` option to allow multiple cases:

### ignore

Type: `RegExp[]`\
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.

Sometimes you may have in non-standard file names a project, so using this option allow to ignore them.

For example:

- vendor packages which are not published and was copy-pasted
- ignore some of 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

```js
"unicorn/filename-case": [
"error",
Expand All @@ -81,7 +89,8 @@ Filenames to ignore.
"ignore": [
"FOOBAR\\.js",
"^(B|b)az",
"\\.SOMETHING\\.js"
"\\.SOMETHING\\.js",
/^vendor/i
]
}
]
Expand Down
14 changes: 7 additions & 7 deletions rules/filename-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ function englishishJoinWords(words) {
const create = context => {
const options = context.options[0] || {};
const chosenCases = getChosenCases(options);
const ignore = (options.ignore || []).map(item => new RegExp(item, 'u'));
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 Down Expand Up @@ -203,9 +209,6 @@ const schema = [{
},
ignore: {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true
}
},
Expand All @@ -232,9 +235,6 @@ const schema = [{
},
ignore: {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true
}
},
Expand Down
130 changes: 129 additions & 1 deletion test/filename-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,54 @@ ruleTester.run('filename-case', rule, {
testCaseWithOptions(undefined, undefined, [
{case: 'kebabCase', ignore: ['FOOBAR\\.js']}
Copy link
Owner

Choose a reason for hiding this comment

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

It should allow specifying regex literals. Most people use JS configs, so it's nicer to also be able to specify literals.

Copy link
Author

Choose a reason for hiding this comment

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

@sindresorhus just allow use /foobar\.js/ as regexp, right? When i use string notation foobar\\.js it is also regexp?

Copy link
Owner

@sindresorhus sindresorhus Nov 12, 2019

Choose a reason for hiding this comment

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

Allow both both regex literal and regex in a string.

Copy link
Collaborator

Choose a reason for hiding this comment

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

is regex in a string a good idea? maybe confusing people

]),
testCaseWithOptions(undefined, undefined, [
{case: 'kebabCase', ignore: [/FOOBAR\.js/u]}
]),
testCaseWithOptions('src/foo/index.js', undefined, [
{case: 'kebabCase', ignore: ['FOOBAR\\.js']}
]),
testCaseWithOptions('src/foo/index.js', undefined, [
{case: 'kebabCase', ignore: [/FOOBAR\.js/u]}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{case: 'kebabCase', ignore: ['FOOBAR\\.js']}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{case: 'kebabCase', ignore: [/FOOBAR\.js/u]}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{case: 'camelCase', ignore: ['FOOBAR\\.js']}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{case: 'camelCase', ignore: [/FOOBAR\.js/u]}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{case: 'snakeCase', ignore: ['FOOBAR\\.js']}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{case: 'pascalCase', ignore: ['FOOBAR\\.js']}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{case: 'pascalCase', ignore: [/FOOBAR\.js/u]}
]),
testCaseWithOptions('src/foo/BARBAZ.js', undefined, [
{case: 'kebabCase', ignore: ['FOOBAR\\.js', 'BARBAZ\\.js']}
]),
testCaseWithOptions('src/foo/BARBAZ.js', undefined, [
{case: 'kebabCase', ignore: ['FOOBAR\\.js', /BARBAZ\.js/u]}
]),
testCaseWithOptions('src/foo/[FOOBAR].js', undefined, [
{case: 'camelCase', ignore: ['\\[FOOBAR\\]\\.js']}
]),
testCaseWithOptions('src/foo/[FOOBAR].js', undefined, [
{case: 'camelCase', ignore: [/\[FOOBAR\]\.js/]}
]),
testCaseWithOptions('src/foo/{FOOBAR}.js', undefined, [
{case: 'snakeCase', ignore: ['\\{FOOBAR\\}\\.js']}
]),
testCaseWithOptions('src/foo/{FOOBAR}.js', undefined, [
{case: 'snakeCase', ignore: [/\{FOOBAR\}\.js/]}
]),
testCaseWithOptions('src/foo/foo.js', undefined, [
{case: 'kebabCase', ignore: ['^(F|f)oo']}
]),
Expand All @@ -132,6 +156,12 @@ ruleTester.run('filename-case', rule, {
testCaseWithOptions('src/foo/foo_bar.js', undefined, [
{case: 'kebabCase', ignore: ['^(F|f)oo']}
]),
testCaseWithOptions('src/foo/foo_bar.js', undefined, [
{case: 'kebabCase', ignore: [/foo/iu]}
]),
testCaseWithOptions('src/foo/FOO_bar.js', undefined, [
{case: 'kebabCase', ignore: [/foo/iu]}
]),
testCaseWithOptions('src/foo/foo-bar.js', undefined, [
{case: 'kebabCase', ignore: ['\\.(web|android|ios)\\.js$']}
]),
Expand All @@ -144,15 +174,27 @@ ruleTester.run('filename-case', rule, {
testCaseWithOptions('src/foo/FooBar.ios.js', undefined, [
{case: 'kebabCase', ignore: ['\\.(web|android|ios)\\.js$']}
]),
testCaseWithOptions('src/foo/FooBar.something.js', undefined, [
{case: 'kebabCase', ignore: [/\.(web|android|ios|something)\.js$/u]}
]),
testCaseWithOptions('src/foo/FooBar.js', undefined, [
{case: 'kebabCase', ignore: ['^(F|f)oo']}
]),
testCaseWithOptions('src/foo/FooBar.js', undefined, [
{case: 'kebabCase', ignore: [/^(F|f)oo/u]}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{case: 'kebabCase', ignore: ['^FOO', 'BAZ\\.js$']}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{case: 'kebabCase', ignore: [/^FOO/, /BAZ\.js$/u]}
]),
testCaseWithOptions('src/foo/BARBAZ.js', undefined, [
{case: 'kebabCase', ignore: ['^FOO', 'BAZ\\.js$']}
]),
testCaseWithOptions('src/foo/BARBAZ.js', undefined, [
{case: 'kebabCase', ignore: [/^FOO/, /BAZ\.js$/]}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{
cases: {
Expand All @@ -164,6 +206,17 @@ ruleTester.run('filename-case', rule, {
ignore: ['FOOBAR\\.js']
}
]),
testCaseWithOptions('src/foo/FOOBAR.js', undefined, [
{
cases: {
kebabCase: true,
camelCase: true,
snakeCase: true,
pascalCase: true
},
ignore: [/FOOBAR\.js/u]
}
]),
testCaseWithOptions('src/foo/BaRbAz.js', undefined, [
{
cases: {
Expand All @@ -174,6 +227,17 @@ ruleTester.run('filename-case', rule, {
},
ignore: ['FOOBAR\\.js', 'BaRbAz\\.js']
}
]),
testCaseWithOptions('src/foo/BaRbAz.js', undefined, [
{
cases: {
kebabCase: true,
camelCase: true,
snakeCase: true,
pascalCase: true
},
ignore: [/FOOBAR\.js/u, /BaRbAz\.js/u]
}
])
],
invalid: [
Expand Down Expand Up @@ -330,16 +394,36 @@ ruleTester.run('filename-case', rule, {
'Filename is not in kebab case. Rename it to `bar-baz.js`.',
[{case: 'kebabCase', ignore: ['FOOBAR\\.js']}]
),
testCaseWithOptions(
'src/foo/barBaz.js',
'Filename is not in kebab case. Rename it to `bar-baz.js`.',
[{case: 'kebabCase', ignore: ['/FOOBAR\\.js/']}]
),
testCaseWithOptions(
'src/foo/barBaz.js',
'Filename is not in kebab case. Rename it to `bar-baz.js`.',
[{case: 'kebabCase', ignore: [/FOOBAR\.js/u]}]
),
testCaseWithOptions(
'src/foo/fooBar.js',
'Filename is not in kebab case. Rename it to `foo-bar.js`.',
[{case: 'kebabCase', ignore: ['FOOBAR\\.js']}]
),
testCaseWithOptions(
'src/foo/fooBar.js',
'Filename is not in kebab case. Rename it to `foo-bar.js`.',
[{case: 'kebabCase', ignore: [/FOOBAR\.js/u]}]
),
testCaseWithOptions(
'src/foo/fooBar.js',
'Filename is not in kebab case. Rename it to `foo-bar.js`.',
[{case: 'kebabCase', ignore: ['FOOBAR\\.js', 'foobar\\.js']}]
),
testCaseWithOptions(
'src/foo/fooBar.js',
'Filename is not in kebab case. Rename it to `foo-bar.js`.',
[{case: 'kebabCase', ignore: [/FOOBAR\.js/u, /foobar\.js/u]}]
),
testCaseWithOptions(
'src/foo/FooBar.js',
'Filename is not in camel case or snake case. Rename it to `fooBar.js` or `foo_bar.js`.',
Expand All @@ -351,6 +435,17 @@ ruleTester.run('filename-case', rule, {
ignore: ['FOOBAR\\.js']
}]
),
testCaseWithOptions(
'src/foo/FooBar.js',
'Filename is not in camel case or snake case. Rename it to `fooBar.js` or `foo_bar.js`.',
[{
cases: {
camelCase: true,
snakeCase: true
},
ignore: [/FOOBAR\.js/u]
}]
),
testCaseWithOptions(
'src/foo/FooBar.js',
'Filename is not in camel case or snake case. Rename it to `fooBar.js` or `foo_bar.js`.',
Expand All @@ -370,7 +465,29 @@ ruleTester.run('filename-case', rule, {
camelCase: true,
snakeCase: true
},
ignore: ['/^foo/']
ignore: [/BaRbAz\.js/u]
}]
),
testCaseWithOptions(
'src/foo/FooBar.js',
'Filename is not in camel case or snake case. Rename it to `fooBar.js` or `foo_bar.js`.',
[{
cases: {
camelCase: true,
snakeCase: true
},
ignore: ['^foo']
}]
),
testCaseWithOptions(
'src/foo/FooBar.js',
'Filename is not in camel case or snake case. Rename it to `fooBar.js` or `foo_bar.js`.',
[{
cases: {
camelCase: true,
snakeCase: true
},
ignore: [/^foo/]
}]
),
testCaseWithOptions(
Expand All @@ -383,6 +500,17 @@ ruleTester.run('filename-case', rule, {
},
ignore: ['^foo', '^bar']
}]
),
testCaseWithOptions(
'src/foo/FooBar.js',
'Filename is not in camel case or snake case. Rename it to `fooBar.js` or `foo_bar.js`.',
[{
cases: {
camelCase: true,
snakeCase: true
},
ignore: [/^foo/, /^bar/]
}]
)
]
});