Skip to content

Commit 54d7b74

Browse files
committed
feat: add build in pattern support for rule filename-naming-convention
1 parent a98b8cd commit 54d7b74

File tree

3 files changed

+572
-13
lines changed

3 files changed

+572
-13
lines changed

lib/rules/filename-naming-convention.js

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
*/
55
'use strict';
66

7+
const micromatch = require('micromatch');
8+
const { getFilename, getBasename } = require('../utils/filename');
9+
const NAMING_CONVENTION = require('../constants/naming-convention');
10+
11+
/**
12+
* @type {string} invalid naming pattern
13+
* @param {string} rules naming pattern configurated by user
14+
*/
15+
const checkNamingConfig = (rules) => {
16+
const buildInPatterns = Object.keys(NAMING_CONVENTION);
17+
const isValidPattern = (pattern) =>
18+
(typeof pattern === 'string' && buildInPatterns.includes(pattern)) ||
19+
pattern instanceof RegExp;
20+
21+
return Object.values(rules).find((pattern) => !isValidPattern(pattern));
22+
};
23+
724
/**
825
* @type {import('eslint').Rule.RuleModule}
926
*/
@@ -17,10 +34,57 @@ module.exports = {
1734
url: null, // TODO: URL to the documentation page for this rule
1835
},
1936
fixable: null, // Or `code` or `whitespace`
20-
schema: [], // Add a schema if the rule has options
37+
schema: [
38+
{
39+
additionalProperties: {
40+
type: 'string',
41+
},
42+
},
43+
],
2144
},
2245

23-
create() {
24-
return {};
46+
create(context) {
47+
return {
48+
Program: (node) => {
49+
const rules = context.options[0];
50+
const invalidPatter = checkNamingConfig(rules);
51+
52+
if (invalidPatter) {
53+
context.report({
54+
node,
55+
message:
56+
'There is an unsupported naming pattern "{{invalidPatter}}", please check it.',
57+
data: {
58+
invalidPatter,
59+
},
60+
});
61+
return;
62+
}
63+
64+
const filenameWithPath = context.getFilename();
65+
const filename = getFilename(filenameWithPath);
66+
67+
for (const [fexPattern, pattern] of Object.entries(rules)) {
68+
if (!micromatch.isMatch(filename, fexPattern)) {
69+
continue;
70+
} else if (
71+
(NAMING_CONVENTION[pattern] || pattern).test(getBasename(filename))
72+
) {
73+
return;
74+
} else {
75+
context.report({
76+
node,
77+
message:
78+
'The filename "{{filename}}" does not match the "{{pattern}}" style',
79+
data: {
80+
filename,
81+
pattern,
82+
},
83+
});
84+
return;
85+
}
86+
}
87+
},
88+
};
2589
},
2690
};

lib/utils/filename.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@
99
* @param {string} path filename concat with path
1010
*/
1111
const getFilename = (path) => path.substring(path.lastIndexOf('/') + 1);
12+
// TODO: deal with filename in root path
1213

1314
/**
1415
* @type {string} folder
1516
* @param {string} path filename concat with path
1617
*/
1718
const getFolder = (path) => path.substring(0, path.lastIndexOf('/') + 1);
1819

20+
/**
21+
* @type {string} base name
22+
* @param {string} filename filename without path
23+
*/
24+
const getBasename = (filename) =>
25+
filename.substring(0, filename.lastIndexOf('.'));
26+
1927
module.exports = {
2028
getFolder,
2129
getFilename,
30+
getBasename,
2231
};

0 commit comments

Comments
 (0)