Skip to content

Commit 58c644f

Browse files
authored
feat: add new rule filename-blacklist
* feat: new rule filename-blacklist * docs: add filename-blacklist to root README.md * refactor: apply requested changes from pull request review
1 parent 911b252 commit 58c644f

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ Then configure the rules you want to use under the rules section.
6767
- [check-file/filename-naming-convention](docs/rules/filename-naming-convention.md): Enforce a consistent naming pattern for the filename of the specified file
6868
- [check-file/no-index](docs/rules/no-index.md): A file cannot be named "index"
6969
- [check-file/folder-naming-convention](docs/rules/folder-naming-convention.md): Enforce a consistent naming pattern for the name of the specified folder
70+
- [check-file/filename-blacklist](docs/rules/filename-blacklist.md): Blacklist file names by pattern

docs/rules/filename-blacklist.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# The filename should not be blacklisted (filename-blacklist)
2+
3+
Allows you to blacklist certain file name patterns.
4+
5+
## Rule Details
6+
7+
This rule aims to maintain a consistent naming scheme.
8+
9+
If the rule had been set as follows:
10+
```js
11+
...
12+
'check-file/filename-blacklist': ['error', { '**/*.model.ts': '*.models.ts' }],
13+
...
14+
```
15+
16+
Examples of **incorrect** filename with path for this rule:
17+
```sh
18+
src/foo.model.ts
19+
src/bar.model.ts
20+
```
21+
22+
Examples of **correct** filename with path for this rule:
23+
```sh
24+
src/foo.models.ts
25+
src/bar.models.ts
26+
```
27+
28+
29+
### Options
30+
31+
#### blacklist pattern object
32+
33+
You need to specify a different naming pattern for each filename blacklist. The second pattern is used to hint at the correct file name that should be used instead.
34+
35+
```js
36+
module.exports = {
37+
plugins: [
38+
'check-file',
39+
],
40+
rules: {
41+
'check-file/filename-blacklist': ['error', {
42+
'**/*.model.ts': '*.models.ts',
43+
'**/*.util.ts': '*.utils.ts',
44+
}],
45+
},
46+
};
47+
```
48+
49+
## Further Reading
50+
51+
- [micromatch](https://github.com/micromatch/micromatch)
52+
- [glob](https://en.wikipedia.org/wiki/Glob_(programming))
53+
- [testing glob expression online](https://globster.xyz)

lib/rules/filename-blacklist.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @fileoverview The filename should not be blacklisted.
3+
* @author Florian Ehmke
4+
*/
5+
'use strict';
6+
7+
const { getFilename, getFilePath } = require('../utils/filename');
8+
const { checkSettings, globPatternValidator } = require('../utils/settings');
9+
const { getDocUrl } = require('../utils/doc');
10+
const { matchRule } = require('../utils/match');
11+
12+
/**
13+
* @type {import('eslint').Rule.RuleModule}
14+
*/
15+
module.exports = {
16+
meta: {
17+
type: 'layout',
18+
docs: {
19+
description: 'The filename should not be blacklisted',
20+
category: 'Layout & Formatting',
21+
recommended: false,
22+
url: getDocUrl('filename-blacklist'),
23+
},
24+
fixable: null,
25+
schema: [
26+
{
27+
additionalProperties: {
28+
type: 'string',
29+
},
30+
},
31+
],
32+
},
33+
34+
create(context) {
35+
return {
36+
Program: (node) => {
37+
const rules = context.options[0];
38+
39+
const invalidPattern = checkSettings(
40+
rules,
41+
globPatternValidator,
42+
globPatternValidator
43+
);
44+
45+
if (invalidPattern) {
46+
context.report({
47+
node,
48+
message:
49+
'There is an invalid pattern "{{invalidPattern}}", please check it',
50+
data: {
51+
invalidPattern,
52+
},
53+
});
54+
return;
55+
}
56+
57+
const filenameWithPath = getFilePath(context);
58+
const filename = getFilename(filenameWithPath);
59+
60+
for (const [blackListPattern, useInsteadPattern] of Object.entries(
61+
rules
62+
)) {
63+
const matchResult = matchRule(filename, blackListPattern);
64+
65+
if (matchResult) {
66+
const { path } = matchResult;
67+
let message =
68+
'The filename "{{ path }}" matches the blacklisted "{{ blackListPattern }}" pattern. Use a pattern like "{{ useInsteadPattern }}" instead.';
69+
70+
context.report({
71+
node,
72+
message,
73+
data: {
74+
path,
75+
blackListPattern,
76+
useInsteadPattern,
77+
},
78+
});
79+
return;
80+
}
81+
}
82+
},
83+
};
84+
},
85+
};

lib/utils/match.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const matchRule = (
2323
if (!micromatch.isMatch(filePath, targetFilePathPattern)) {
2424
return;
2525
} else if (
26+
targetNaming &&
27+
targetNamingPattern &&
2628
micromatch.isMatch(
2729
targetNaming,
2830
NAMING_CONVENTION[targetNamingPattern] || targetNamingPattern

tests/lib/rules/filename-blacklist.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @fileoverview The filename should follow the filename naming convention
3+
* @author Florian Ehmke
4+
*/
5+
'use strict';
6+
7+
const rule = require('../../../lib/rules/filename-blacklist');
8+
const RuleTester = require('eslint').RuleTester;
9+
10+
const ruleTester = new RuleTester();
11+
12+
ruleTester.run('filename-blacklist', rule, {
13+
valid: [
14+
{
15+
code: "var foo = 'bar';",
16+
filename: 'src/foo.model.ts',
17+
options: [{ '*.models.ts': '*.model.ts' }],
18+
},
19+
{
20+
code: "var foo = 'bar';",
21+
filename: 'src/foo.util‚.ts',
22+
options: [{ '*.utils.ts': '*.util.ts' }],
23+
},
24+
],
25+
invalid: [
26+
{
27+
code: "var foo = 'bar';",
28+
filename: 'src/foo.models.ts',
29+
options: [{ '*.models.ts': '*.model.ts' }],
30+
errors: [
31+
{
32+
message:
33+
'The filename "foo.models.ts" matches the blacklisted "*.models.ts" pattern. Use a pattern like "*.model.ts" instead.',
34+
column: 1,
35+
line: 1,
36+
},
37+
],
38+
},
39+
{
40+
code: "var foo = 'bar';",
41+
filename: 'src/foo.utils.ts',
42+
options: [{ '*.utils.ts': '*.util.ts' }],
43+
errors: [
44+
{
45+
message:
46+
'The filename "foo.utils.ts" matches the blacklisted "*.utils.ts" pattern. Use a pattern like "*.util.ts" instead.',
47+
column: 1,
48+
line: 1,
49+
},
50+
],
51+
},
52+
],
53+
});

0 commit comments

Comments
 (0)