Skip to content

Commit e0b1e5f

Browse files
committed
refactor(#35): replace nonGlobSuggestion with errorMessage
1 parent f25a913 commit e0b1e5f

File tree

5 files changed

+53
-44
lines changed

5 files changed

+53
-44
lines changed

docs/rules/filename-blocklist.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,29 @@ module.exports = {
5959

6060
#### rule configuration object
6161

62-
##### `nonGlobSuggestion`
62+
##### `errorMessage`
6363

64-
If `true`, it allows the blocklist reason to be any string, instead of a strict glob pattern.
64+
Customizes the error message displayed when a file is blocked due to matching a blocklisted filename pattern. It offers two placeholders for dynamic content:
65+
66+
- `{{ target }}`: Represents the filename of the blocked file.
67+
- `{{ pattern }}`: Represents the blocklisted filename pattern.
6568

6669
```js
6770
module.exports = {
6871
plugins: ['check-file'],
6972
rules: {
7073
'check-file/filename-blocklist': [
7174
'error',
75+
{ '*.models.ts': '*.model.ts' },
7276
{
73-
'**/*.model.ts': 'see the repo rules at http://some/example.com',
74-
'**/*.util.ts': 'for a non glob related reason',
77+
errorMessage:
78+
'The file "{{ target }}" is blocked since it since it matches the blocklisted pattern "{{ pattern }}", see contribute.md for details',
7579
},
76-
{ nonGlobSuggestion: true },
7780
],
7881
},
7982
};
8083
```
8184

82-
These rules would produce errors that look like:
83-
84-
- `The filename "model.ts" matches the blocklisted "**/*.model.ts" pattern, this is not allowed see the repo rules at http://some/example.com`
85-
- `The filename "util.ts" matches the blocklisted "**/*.util.ts" pattern, this is not allowed for a non glob related reason`
86-
8785
## Further Reading
8886

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

lib/constants/message.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ export const PREFINED_MATCH_SYNTAX_ERROR_MESSAGE =
1515
export const FILENAME_BLOCKLIST_ERROR_MESSAGE =
1616
'The filename "{{ filename }}" matches the blocklisted "{{ blockListPattern }}" pattern, use a pattern like "{{ suggestion }}" instead';
1717

18-
export const FILENAME_BLOCKLIST_NON_GLOB_ERROR_MESSAGE =
19-
'The filename "{{ filename }}" matches the blocklisted "{{ blockListPattern }}" pattern, this is not allowed {{ suggestion }}';
20-
2118
export const FILENAME_NAMING_CONVENTION_ERROR_MESSAGE =
2219
'The filename "{{ filename }}" does not match the "{{ originalNamingPattern }}" pattern';
2320

lib/rules/filename-blocklist.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
import {
77
FILENAME_BLOCKLIST_ERROR_MESSAGE,
8-
FILENAME_BLOCKLIST_NON_GLOB_ERROR_MESSAGE,
98
NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
109
PATTERN_ERROR_MESSAGE,
1110
} from '../constants/message.js';
1211
import { getDocUrl } from '../utils/doc.js';
1312
import { getFilePath, getFilename } from '../utils/filename.js';
1413
import { matchRule } from '../utils/rule.js';
14+
import { isNotEmpty } from '../utils/utility.js';
1515
import {
1616
validateNamingPatternObject,
1717
globPatternValidator,
@@ -39,29 +39,28 @@ export default {
3939
{
4040
type: 'object',
4141
properties: {
42-
nonGlobSuggestion: { type: 'boolean' },
42+
errorMessage: { type: 'string' },
4343
},
4444
},
4545
],
4646
messages: {
4747
invalidObject: NAMING_PATTERN_OBJECT_ERROR_MESSAGE,
4848
invalidPattern: PATTERN_ERROR_MESSAGE,
4949
noMatch: FILENAME_BLOCKLIST_ERROR_MESSAGE,
50-
noMatchWithNonGlob: FILENAME_BLOCKLIST_NON_GLOB_ERROR_MESSAGE,
5150
},
5251
},
5352

5453
create(context) {
5554
return {
5655
Program: (node) => {
5756
const rules = context.options[0];
58-
const { nonGlobSuggestion } = context.options[1] || {
59-
nonGlobSuggestion: false,
57+
const { errorMessage } = context.options[1] || {
58+
errorMessage: '',
6059
};
6160
const error = validateNamingPatternObject(
6261
rules,
6362
globPatternValidator,
64-
nonGlobSuggestion ? () => true : globPatternValidator
63+
globPatternValidator
6564
);
6665

6766
if (error) {
@@ -78,10 +77,9 @@ export default {
7877
const filenameWithPath = getFilePath(context);
7978
const filename = getFilename(filenameWithPath);
8079

81-
for (const [
82-
blockListPattern,
83-
useInsteadPatternOrString,
84-
] of Object.entries(rules)) {
80+
for (const [blockListPattern, useInsteadPattern] of Object.entries(
81+
rules
82+
)) {
8583
const matchResult =
8684
matchRule(filenameWithPath, blockListPattern) ||
8785
// TODO: remove this in next major version
@@ -91,15 +89,25 @@ export default {
9189
matchRule(filename, blockListPattern);
9290

9391
if (matchResult) {
94-
context.report({
95-
node,
96-
messageId: nonGlobSuggestion ? 'noMatchWithNonGlob' : 'noMatch',
97-
data: {
98-
filename,
99-
blockListPattern,
100-
suggestion: useInsteadPatternOrString,
101-
},
102-
});
92+
isNotEmpty(errorMessage)
93+
? context.report({
94+
node,
95+
// eslint-disable-next-line eslint-plugin/prefer-message-ids
96+
message: errorMessage,
97+
data: {
98+
target: filename,
99+
pattern: blockListPattern,
100+
},
101+
})
102+
: context.report({
103+
node,
104+
messageId: 'noMatch',
105+
data: {
106+
filename,
107+
blockListPattern,
108+
suggestion: useInsteadPattern,
109+
},
110+
});
103111
return;
104112
}
105113
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,19 @@ ruleTester.run('filename-blocklist with option: []', rule, {
238238
});
239239

240240
ruleTester.run(
241-
"filename-blocklist with option: [{'*.models.ts': 'for some Non Glob related reason'}, { nonGlobSuggestion: true }]",
241+
"filename-blocklist with option: [{'*.models.ts': '*.model.ts'}, { errorMessage: 'The file \"{{ target }}\" is blocked since it since it matches the blocklisted pattern \"{{ pattern }}\", see contribute.md for details' }]",
242242
rule,
243243
{
244244
valid: [
245245
{
246246
code: "var foo = 'bar';",
247247
filename: 'src/foo.apis.ts',
248248
options: [
249+
{ '*.models.ts': '*.model.ts' },
249250
{
250-
'*.models.ts': 'for some Non Glob related reason',
251+
errorMessage:
252+
'The file "{{ target }}" is blocked since it since it matches the blocklisted pattern "{{ pattern }}", see contribute.md for details',
251253
},
252-
{ nonGlobSuggestion: true },
253254
],
254255
},
255256
],
@@ -259,15 +260,16 @@ ruleTester.run(
259260
code: "var foo = 'bar';",
260261
filename: 'src/foo.models.ts',
261262
options: [
263+
{ '*.models.ts': '*.model.ts' },
262264
{
263-
'*.models.ts': 'for some Non Glob related reason',
265+
errorMessage:
266+
'The file "{{ target }}" is blocked since it since it matches the blocklisted pattern "{{ pattern }}", see contribute.md for details',
264267
},
265-
{ nonGlobSuggestion: true },
266268
],
267269
errors: [
268270
{
269271
message:
270-
'The filename "foo.models.ts" matches the blocklisted "*.models.ts" pattern, this is not allowed for some Non Glob related reason',
272+
'The file "foo.models.ts" is blocked since it since it matches the blocklisted pattern "*.models.ts", see contribute.md for details',
271273
column: 1,
272274
line: 1,
273275
},

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,19 @@ ruleTester.run('filename-blocklist with option on Windows: []', rule, {
166166
});
167167

168168
ruleTester.run(
169-
"filename-blocklist with option on Windows: [{'*.models.ts': 'for some Non Glob related reason'}, { nonGlobSuggestion: true }]",
169+
"filename-blocklist with option on Windows: [{'*.models.ts': '*.model.ts'}, { errorMessage: 'The file \"{{ target }}\" is blocked since it since it matches the blocklisted pattern \"{{ pattern }}\", see contribute.md for details' }]",
170170
rule,
171171
{
172172
valid: [
173173
{
174174
code: "var foo = 'bar';",
175175
filename: 'src\\foo.apis.ts',
176176
options: [
177+
{ '*.models.ts': '*.model.ts' },
177178
{
178-
'*.models.ts': 'for some Non Glob related reason',
179+
errorMessage:
180+
'The file "{{ target }}" is blocked since it since it matches the blocklisted pattern "{{ pattern }}", see contribute.md for details',
179181
},
180-
{ nonGlobSuggestion: true },
181182
],
182183
},
183184
],
@@ -187,13 +188,16 @@ ruleTester.run(
187188
code: "var foo = 'bar';",
188189
filename: 'src\\foo.models.ts',
189190
options: [
190-
{ '*.models.ts': 'for some Non Glob related reason' },
191-
{ nonGlobSuggestion: true },
191+
{ '*.models.ts': '*.model.ts' },
192+
{
193+
errorMessage:
194+
'The file "{{ target }}" is blocked since it since it matches the blocklisted pattern "{{ pattern }}", see contribute.md for details',
195+
},
192196
],
193197
errors: [
194198
{
195199
message:
196-
'The filename "foo.models.ts" matches the blocklisted "*.models.ts" pattern, this is not allowed for some Non Glob related reason',
200+
'The file "foo.models.ts" is blocked since it since it matches the blocklisted pattern "*.models.ts", see contribute.md for details',
197201
column: 1,
198202
line: 1,
199203
},

0 commit comments

Comments
 (0)