Skip to content

Commit 7151ac1

Browse files
committed
fix: addressed pr comments, updated readme, added tests
1 parent 8b448f1 commit 7151ac1

File tree

5 files changed

+112
-10
lines changed

5 files changed

+112
-10
lines changed

docs/rules/filename-blocklist.md

+23
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const FILENAME_BLOCKLIST_ERROR_MESSAGE = template(
2222
);
2323

2424
export const FILENAME_BLOCKLIST_NONPATTERN_ERROR_MESSAGE = template(
25-
'The filename "$0" matches the blocklisted "$1" pattern, this is not allowed because "$2"'
25+
'The filename "$0" matches the blocklisted "$1" pattern, this is not allowed $2'
2626
);
2727

2828
export const FILENAME_NAMING_CONVENTION_ERROR_MESSAGE = template(

lib/rules/filename-blocklist.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { matchRule } from '../utils/rule.js';
1313
import {
1414
validateNamingPatternObject,
1515
globPatternValidator,
16-
stringResponseValidator,
1716
} from '../utils/validation.js';
1817

1918
/**
@@ -36,8 +35,9 @@ export default {
3635
},
3736
},
3837
{
39-
stringResponse: {
40-
type: 'boolean',
38+
type: 'object',
39+
properties: {
40+
nonGlobSuggestion: { type: 'boolean' },
4141
},
4242
},
4343
],
@@ -47,11 +47,13 @@ export default {
4747
return {
4848
Program: (node) => {
4949
const rules = context.options[0];
50-
const stringResponse = context.options[1];
50+
const { nonGlobSuggestion } = context.options[1] || {
51+
nonGlobSuggestion: false,
52+
};
5153
const message = validateNamingPatternObject(
5254
rules,
5355
globPatternValidator,
54-
stringResponse ? stringResponseValidator : globPatternValidator
56+
nonGlobSuggestion ? () => true : globPatternValidator
5557
);
5658

5759
if (message) {
@@ -77,7 +79,7 @@ export default {
7779
// it's a legacy feature, will be removed in the future
7880
matchRule(filename, blockListPattern);
7981

80-
const messageFN = stringResponse
82+
const messageFN = nonGlobSuggestion
8183
? FILENAME_BLOCKLIST_NONPATTERN_ERROR_MESSAGE
8284
: FILENAME_BLOCKLIST_ERROR_MESSAGE;
8385

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

+32-3
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ ruleTester.run('filename-blocklist with option: []', rule, {
238238
});
239239

240240
ruleTester.run(
241-
"filename-blocklist with option: [{'*.models.ts': 'Some Non Glob reason'}, true]",
241+
"filename-blocklist with option: [{'*.models.ts': 'for some Non Glob related reason'}, true]",
242242
rule,
243243
{
244244
valid: [
@@ -247,13 +247,42 @@ ruleTester.run(
247247
filename: 'src/foo.apis.ts',
248248
options: [
249249
{
250-
'*.models.ts': 'Some Non Glob reason',
250+
'*.models.ts': 'for some Non Glob related reason',
251251
},
252-
true,
252+
{ nonGlobSuggestion: true },
253253
],
254254
},
255255
],
256256

257257
invalid: [],
258258
}
259259
);
260+
261+
ruleTester.run(
262+
"filename-blocklist with option: [{'*.models.ts': 'for some Non Glob related reason'}, { nonGlobSuggestion: true, }]",
263+
rule,
264+
{
265+
valid: [],
266+
267+
invalid: [
268+
{
269+
code: "var foo = 'bar';",
270+
filename: 'src/foo.models.ts',
271+
options: [
272+
{
273+
'*.models.ts': 'for some Non Glob related reason',
274+
},
275+
{ nonGlobSuggestion: true },
276+
],
277+
errors: [
278+
{
279+
message:
280+
'The filename "foo.models.ts" matches the blocklisted "*.models.ts" pattern, this is not allowed for some Non Glob related reason',
281+
column: 1,
282+
line: 1,
283+
},
284+
],
285+
},
286+
],
287+
}
288+
);

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

+48
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,51 @@ ruleTester.run('filename-blocklist with option on Windows: []', rule, {
164164
},
165165
],
166166
});
167+
168+
ruleTester.run(
169+
"filename-blocklist with option: [{'*.models.ts': 'for some Non Glob related reason'}, { nonGlobSuggestion: true, }] on Windows: []",
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+
);
188+
189+
ruleTester.run(
190+
"filename-blocklist with option: [{'*.models.ts': 'for some Non Glob related reason'}, { nonGlobSuggestion: true, }] on Windows: []",
191+
rule,
192+
{
193+
valid: [],
194+
195+
invalid: [
196+
{
197+
code: "var foo = 'bar';",
198+
filename: 'src\\foo.models.ts',
199+
options: [
200+
{ '*.models.ts': 'for some Non Glob related reason' },
201+
{ nonGlobSuggestion: true },
202+
],
203+
errors: [
204+
{
205+
message:
206+
'The filename "foo.models.ts" matches the blocklisted "*.models.ts" pattern, this is not allowed for some Non Glob related reason',
207+
column: 1,
208+
line: 1,
209+
},
210+
],
211+
},
212+
],
213+
}
214+
);

0 commit comments

Comments
 (0)