Skip to content

Commit b71948d

Browse files
committed
feat: add rule settings validation for rule folder-match-with-fex
1 parent f47188d commit b71948d

File tree

4 files changed

+123
-22
lines changed

4 files changed

+123
-22
lines changed

lib/rules/filename-naming-convention.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,15 @@
55
'use strict';
66

77
const micromatch = require('micromatch');
8-
const isGlob = require('is-glob');
98
const { getFilename, getBasename } = require('../utils/filename');
9+
const {
10+
checkSettings,
11+
namingPatternValidator,
12+
globPatternValidator,
13+
} = require('../utils/settings');
1014
const { getDocUrl } = require('../utils/doc');
1115
const NAMING_CONVENTION = require('../constants/naming-convention');
1216

13-
/**
14-
* @type {string} invalid naming pattern
15-
* @param rules naming pattern configurated by user
16-
*/
17-
const checkNamingConfig = (rules) => {
18-
const buildInPatterns = Object.keys(NAMING_CONVENTION);
19-
20-
for (const [fexPattern, namingPattern] of Object.entries(rules)) {
21-
if (!isGlob(fexPattern)) {
22-
return fexPattern;
23-
} else if (
24-
!isGlob(namingPattern) &&
25-
!buildInPatterns.includes(namingPattern)
26-
) {
27-
return namingPattern;
28-
}
29-
}
30-
};
31-
3217
/**
3318
* @type {import('eslint').Rule.RuleModule}
3419
*/
@@ -55,7 +40,11 @@ module.exports = {
5540
return {
5641
Program: (node) => {
5742
const rules = context.options[0];
58-
const invalidPattern = checkNamingConfig(rules);
43+
const invalidPattern = checkSettings(
44+
rules,
45+
globPatternValidator,
46+
namingPatternValidator
47+
);
5948

6049
if (invalidPattern) {
6150
context.report({

lib/rules/folder-match-with-fex.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const micromatch = require('micromatch');
88
const { getFolder, getFilename } = require('../utils/filename');
9+
const { checkSettings, globPatternValidator } = require('../utils/settings');
910
const { getDocUrl } = require('../utils/doc');
1011

1112
/**
@@ -34,10 +35,29 @@ module.exports = {
3435
create(context) {
3536
return {
3637
Program: (node) => {
38+
const rules = context.options[0];
39+
40+
const invalidPattern = checkSettings(
41+
rules,
42+
globPatternValidator,
43+
globPatternValidator
44+
);
45+
46+
if (invalidPattern) {
47+
context.report({
48+
node,
49+
message:
50+
'There is an invalid pattern "{{invalidPattern}}", please check it',
51+
data: {
52+
invalidPattern,
53+
},
54+
});
55+
return;
56+
}
57+
3758
const filenameWithPath = context.getFilename();
3859
const filename = getFilename(filenameWithPath);
3960
const folder = getFolder(filenameWithPath);
40-
const rules = context.options[0];
4161

4262
for (const [fexPattern, folderPattern] of Object.entries(rules)) {
4363
if (!micromatch.isMatch(filename, fexPattern)) {

lib/utils/settings.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @fileoverview Utils about rule settings
3+
* @author Duke Luo
4+
*/
5+
'use strict';
6+
7+
const isGlob = require('is-glob');
8+
const NAMING_CONVENTION = require('../constants/naming-convention');
9+
10+
/**
11+
* @type {string} invalid key or value
12+
* @param settings rule settings configurated by user
13+
* @param keyValidator settings key validator
14+
* @param valueValidator settings value validator
15+
*/
16+
const checkSettings = (settings, keyValidator, valueValidator) => {
17+
for (const [key, value] of Object.entries(settings)) {
18+
if (!keyValidator(key)) {
19+
return key;
20+
} else if (!valueValidator(value)) {
21+
return value;
22+
}
23+
}
24+
};
25+
26+
/**
27+
* @type {boolean}
28+
* @param namingPattern pattern string
29+
*/
30+
const namingPatternValidator = (namingPattern) => {
31+
const buildInPatterns = Object.keys(NAMING_CONVENTION);
32+
33+
return isGlob(namingPattern) || buildInPatterns.includes(namingPattern);
34+
};
35+
36+
/**
37+
* @type {boolean}
38+
* @param pattern pattern string
39+
*/
40+
const globPatternValidator = isGlob;
41+
42+
module.exports = {
43+
checkSettings,
44+
namingPatternValidator,
45+
globPatternValidator,
46+
};

tests/lib/rules/folder-match-with-fex.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,49 @@ ruleTester.run('folder-match-with-fex with fex that has not been set', rule, {
258258

259259
invalid: [],
260260
});
261+
262+
ruleTester.run(
263+
"folder-match-with-fex with option: [{ '*.test.js': 'FOO', '*.test.ts': '*/__tests__/' }]",
264+
rule,
265+
{
266+
valid: [],
267+
268+
invalid: [
269+
{
270+
code: "var foo = 'bar';",
271+
filename: 'bar/__tests__/foo.test.js',
272+
options: [{ '*.test.js': 'FOO', '*.test.ts': '*/__tests__/' }],
273+
errors: [
274+
{
275+
message: 'There is an invalid pattern "FOO", please check it',
276+
column: 1,
277+
line: 1,
278+
},
279+
],
280+
},
281+
],
282+
}
283+
);
284+
285+
ruleTester.run(
286+
"folder-match-with-fex with option: [{ '*.test.js': '*/__tests__/', '.test.ts': '*/__tests__/' }]",
287+
rule,
288+
{
289+
valid: [],
290+
291+
invalid: [
292+
{
293+
code: "var foo = 'bar';",
294+
filename: 'bar/__tests__/foo.test.js',
295+
options: [{ '*.test.js': '*/__tests__/', '.test.ts': '*/__tests__/' }],
296+
errors: [
297+
{
298+
message: 'There is an invalid pattern ".test.ts", please check it',
299+
column: 1,
300+
line: 1,
301+
},
302+
],
303+
},
304+
],
305+
}
306+
);

0 commit comments

Comments
 (0)