Skip to content

Commit 8b448f1

Browse files
committed
feat: allow option for non-glob values for filename-blocklist
1 parent c5e7e89 commit 8b448f1

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

lib/constants/message.js

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export const FILENAME_BLOCKLIST_ERROR_MESSAGE = template(
2121
'The filename "$0" matches the blocklisted "$1" pattern, use a pattern like "$2" instead'
2222
);
2323

24+
export const FILENAME_BLOCKLIST_NONPATTERN_ERROR_MESSAGE = template(
25+
'The filename "$0" matches the blocklisted "$1" pattern, this is not allowed because "$2"'
26+
);
27+
2428
export const FILENAME_NAMING_CONVENTION_ERROR_MESSAGE = template(
2529
'The filename "$0" does not match the "$1" pattern'
2630
);

lib/rules/filename-blocklist.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
* @author Florian Ehmke, Huan Luo
44
*/
55

6-
import { FILENAME_BLOCKLIST_ERROR_MESSAGE } from '../constants/message.js';
6+
import {
7+
FILENAME_BLOCKLIST_ERROR_MESSAGE,
8+
FILENAME_BLOCKLIST_NONPATTERN_ERROR_MESSAGE,
9+
} from '../constants/message.js';
710
import { getDocUrl } from '../utils/doc.js';
811
import { getFilePath, getFilename } from '../utils/filename.js';
912
import { matchRule } from '../utils/rule.js';
1013
import {
1114
validateNamingPatternObject,
1215
globPatternValidator,
16+
stringResponseValidator,
1317
} from '../utils/validation.js';
1418

1519
/**
@@ -31,17 +35,23 @@ export default {
3135
type: 'string',
3236
},
3337
},
38+
{
39+
stringResponse: {
40+
type: 'boolean',
41+
},
42+
},
3443
],
3544
},
3645

3746
create(context) {
3847
return {
3948
Program: (node) => {
4049
const rules = context.options[0];
50+
const stringResponse = context.options[1];
4151
const message = validateNamingPatternObject(
4252
rules,
4353
globPatternValidator,
44-
globPatternValidator
54+
stringResponse ? stringResponseValidator : globPatternValidator
4555
);
4656

4757
if (message) {
@@ -55,9 +65,10 @@ export default {
5565
const filenameWithPath = getFilePath(context);
5666
const filename = getFilename(filenameWithPath);
5767

58-
for (const [blockListPattern, useInsteadPattern] of Object.entries(
59-
rules
60-
)) {
68+
for (const [
69+
blockListPattern,
70+
useInsteadPatternOrString,
71+
] of Object.entries(rules)) {
6172
const matchResult =
6273
matchRule(filenameWithPath, blockListPattern) ||
6374
// TODO: remove this in next major version
@@ -66,13 +77,17 @@ export default {
6677
// it's a legacy feature, will be removed in the future
6778
matchRule(filename, blockListPattern);
6879

80+
const messageFN = stringResponse
81+
? FILENAME_BLOCKLIST_NONPATTERN_ERROR_MESSAGE
82+
: FILENAME_BLOCKLIST_ERROR_MESSAGE;
83+
6984
if (matchResult) {
7085
context.report({
7186
node,
72-
message: FILENAME_BLOCKLIST_ERROR_MESSAGE(
87+
message: messageFN(
7388
filename,
7489
blockListPattern,
75-
useInsteadPattern
90+
useInsteadPatternOrString
7691
),
7792
});
7893
return;

lib/utils/validation.js

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ const nextJsNamingPatternValidator = (namingPattern) =>
6262
*/
6363
export const globPatternValidator = isGlob;
6464

65+
/**
66+
* @returns {boolean} true if reason is a string
67+
* @param {any} reason an arbitrary reason for banning a file naming pattern that isn't a glob pattern
68+
*/
69+
export const stringResponseValidator = (reason) => typeof reason === 'string';
70+
6571
/**
6672
* @returns {boolean} true if pattern is a valid filename naming pattern
6773
* @param {string} namingPattern pattern string

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

+21
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,24 @@ ruleTester.run('filename-blocklist with option: []', rule, {
236236
},
237237
],
238238
});
239+
240+
ruleTester.run(
241+
"filename-blocklist with option: [{'*.models.ts': 'Some Non Glob reason'}, true]",
242+
rule,
243+
{
244+
valid: [
245+
{
246+
code: "var foo = 'bar';",
247+
filename: 'src/foo.apis.ts',
248+
options: [
249+
{
250+
'*.models.ts': 'Some Non Glob reason',
251+
},
252+
true,
253+
],
254+
},
255+
],
256+
257+
invalid: [],
258+
}
259+
);

0 commit comments

Comments
 (0)