Skip to content

Commit eb1836d

Browse files
AtlasTomdukeluo
andauthored
feat: allow option for non-glob values for filename-blocklist (#34)
* feat: allow option for non-glob values for filename-blocklist * fix: addressed pr comments, updated readme, added tests * refactor: rebase main branch --------- Co-authored-by: Huan Luo <dukeluo@outlook.com>
1 parent b444988 commit eb1836d

File tree

5 files changed

+123
-7
lines changed

5 files changed

+123
-7
lines changed

docs/rules/filename-blocklist.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ module.exports = {
5454
};
5555
```
5656

57+
An optional "nonGlobSuggestion" argument can be passed that allows the blocklist reason to be any string, instead of a strict glob pattern
58+
59+
```js
60+
module.exports = {
61+
plugins: [
62+
'check-file',
63+
],
64+
rules: {
65+
'check-file/filename-blocklist': ['error', {
66+
'**/*.model.ts': 'see the repo rules at http://some/example.com',
67+
'**/*.util.ts': 'for a non glob related reason',
68+
},
69+
{ nonGlobSuggestion: true, }
70+
],
71+
},
72+
};
73+
```
74+
75+
These rules would produce errors that look like:
76+
1. 'The filename "model.ts" matches the blocklisted "**/*.model.ts" pattern, this is not allowed see the repo rules at http://some/example.com'
77+
2. 'The filename "util.ts" matches the blocklisted "**/*.util.ts" pattern, this is not allowed for a non glob related reason'
78+
79+
5780
## Further Reading
5881

5982
- [micromatch](https://github.com/micromatch/micromatch)

lib/constants/message.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export const PREFINED_MATCH_SYNTAX_ERROR_MESSAGE =
1313
'The prefined match "{{ namingPattern }}" is not found in the pattern "{{ filenamePattern }}", please double-check it and try again';
1414

1515
export const FILENAME_BLOCKLIST_ERROR_MESSAGE =
16-
'The filename "{{ filename }}" matches the blocklisted "{{ blockListPattern }}" pattern, use a pattern like "{{ useInsteadPattern }}" instead';
16+
'The filename "{{ filename }}" matches the blocklisted "{{ blockListPattern }}" pattern, use a pattern like "{{ suggestion }}" instead';
17+
18+
export const FILENAME_BLOCKLIST_NON_GLOB_ERROR_MESSAGE =
19+
'The filename "{{ filename }}" matches the blocklisted "{{ blockListPattern }}" pattern, this is not allowed {{ suggestion }}';
1720

1821
export const FILENAME_NAMING_CONVENTION_ERROR_MESSAGE =
1922
'The filename "{{ filename }}" does not match the "{{ originalNamingPattern }}" pattern';

lib/rules/filename-blocklist.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import {
77
FILENAME_BLOCKLIST_ERROR_MESSAGE,
8+
FILENAME_BLOCKLIST_NON_GLOB_ERROR_MESSAGE,
89
NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
910
PATTERN_ERROR_MESSAGE,
1011
} from '../constants/message.js';
@@ -35,22 +36,32 @@ export default {
3536
type: 'string',
3637
},
3738
},
39+
{
40+
type: 'object',
41+
properties: {
42+
nonGlobSuggestion: { type: 'boolean' },
43+
},
44+
},
3845
],
3946
messages: {
4047
invalidObject: NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
4148
invalidPattern: PATTERN_ERROR_MESSAGE,
4249
noMatch: FILENAME_BLOCKLIST_ERROR_MESSAGE,
50+
noMatchWithNonGlob: FILENAME_BLOCKLIST_NON_GLOB_ERROR_MESSAGE,
4351
},
4452
},
4553

4654
create(context) {
4755
return {
4856
Program: (node) => {
4957
const rules = context.options[0];
58+
const { nonGlobSuggestion } = context.options[1] || {
59+
nonGlobSuggestion: false,
60+
};
5061
const error = validateNamingPatternObject(
5162
rules,
5263
globPatternValidator,
53-
globPatternValidator
64+
nonGlobSuggestion ? () => true : globPatternValidator
5465
);
5566

5667
if (error) {
@@ -67,9 +78,10 @@ export default {
6778
const filenameWithPath = getFilePath(context);
6879
const filename = getFilename(filenameWithPath);
6980

70-
for (const [blockListPattern, useInsteadPattern] of Object.entries(
71-
rules
72-
)) {
81+
for (const [
82+
blockListPattern,
83+
useInsteadPatternOrString,
84+
] of Object.entries(rules)) {
7385
const matchResult =
7486
matchRule(filenameWithPath, blockListPattern) ||
7587
// TODO: remove this in next major version
@@ -81,11 +93,11 @@ export default {
8193
if (matchResult) {
8294
context.report({
8395
node,
84-
messageId: 'noMatch',
96+
messageId: nonGlobSuggestion ? 'noMatchWithNonGlob' : 'noMatch',
8597
data: {
8698
filename,
8799
blockListPattern,
88-
useInsteadPattern,
100+
suggestion: useInsteadPatternOrString,
89101
},
90102
});
91103
return;

tests/lib/rules/filename-blocklist.posix.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,43 @@ ruleTester.run('filename-blocklist with option: []', rule, {
236236
},
237237
],
238238
});
239+
240+
ruleTester.run(
241+
"filename-blocklist with option: [{'*.models.ts': 'for some Non Glob related reason'}, { nonGlobSuggestion: true }]",
242+
rule,
243+
{
244+
valid: [
245+
{
246+
code: "var foo = 'bar';",
247+
filename: 'src/foo.apis.ts',
248+
options: [
249+
{
250+
'*.models.ts': 'for some Non Glob related reason',
251+
},
252+
{ nonGlobSuggestion: true },
253+
],
254+
},
255+
],
256+
257+
invalid: [
258+
{
259+
code: "var foo = 'bar';",
260+
filename: 'src/foo.models.ts',
261+
options: [
262+
{
263+
'*.models.ts': 'for some Non Glob related reason',
264+
},
265+
{ nonGlobSuggestion: true },
266+
],
267+
errors: [
268+
{
269+
message:
270+
'The filename "foo.models.ts" matches the blocklisted "*.models.ts" pattern, this is not allowed for some Non Glob related reason',
271+
column: 1,
272+
line: 1,
273+
},
274+
],
275+
},
276+
],
277+
}
278+
);

tests/lib/rules/filename-blocklist.windows.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,41 @@ ruleTester.run('filename-blocklist with option on Windows: []', rule, {
164164
},
165165
],
166166
});
167+
168+
ruleTester.run(
169+
"filename-blocklist with option on Windows: [{'*.models.ts': 'for some Non Glob related reason'}, { nonGlobSuggestion: true }]",
170+
rule,
171+
{
172+
valid: [
173+
{
174+
code: "var foo = 'bar';",
175+
filename: 'src\\foo.apis.ts',
176+
options: [
177+
{
178+
'*.models.ts': 'for some Non Glob related reason',
179+
},
180+
{ nonGlobSuggestion: true },
181+
],
182+
},
183+
],
184+
185+
invalid: [
186+
{
187+
code: "var foo = 'bar';",
188+
filename: 'src\\foo.models.ts',
189+
options: [
190+
{ '*.models.ts': 'for some Non Glob related reason' },
191+
{ nonGlobSuggestion: true },
192+
],
193+
errors: [
194+
{
195+
message:
196+
'The filename "foo.models.ts" matches the blocklisted "*.models.ts" pattern, this is not allowed for some Non Glob related reason',
197+
column: 1,
198+
line: 1,
199+
},
200+
],
201+
},
202+
],
203+
}
204+
);

0 commit comments

Comments
 (0)