Skip to content

Commit c804992

Browse files
committed
feat: rule filename-naming-convention can specify target file by file path
1 parent 536a956 commit c804992

File tree

3 files changed

+413
-14
lines changed

3 files changed

+413
-14
lines changed

lib/rules/filename-naming-convention.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
*/
55
'use strict';
66

7-
const micromatch = require('micromatch');
87
const { getFilename, getBasename } = require('../utils/filename');
98
const {
109
checkSettings,
1110
namingPatternValidator,
1211
globPatternValidator,
1312
} = require('../utils/settings');
1413
const { getDocUrl } = require('../utils/doc');
15-
const NAMING_CONVENTION = require('../constants/naming-convention');
14+
const { matchRule } = require('../utils/match');
1615

1716
/**
1817
* @type {import('eslint').Rule.RuleModule}
@@ -62,23 +61,32 @@ module.exports = {
6261
const filename = getFilename(filenameWithPath);
6362

6463
for (const [fexPattern, namingPattern] of Object.entries(rules)) {
65-
if (!micromatch.isMatch(filename, fexPattern)) {
66-
continue;
67-
} else if (
68-
micromatch.isMatch(
64+
const matchResult =
65+
matchRule(
66+
filenameWithPath,
67+
fexPattern,
6968
getBasename(filename),
70-
NAMING_CONVENTION[namingPattern] || namingPattern
71-
)
72-
) {
73-
return;
74-
} else {
69+
namingPattern
70+
) ||
71+
// legacy support for version below v1.2.0
72+
// file only can be specified by fex pattern, not by file path pattern
73+
// it's a legacy feature, will be removed in the future
74+
matchRule(
75+
filename,
76+
fexPattern,
77+
getBasename(filename),
78+
namingPattern
79+
);
80+
81+
if (matchResult) {
82+
const { path, pattern } = matchResult;
7583
context.report({
7684
node,
7785
message:
78-
'The filename "{{filename}}" does not match the "{{namingPattern}}" style',
86+
'The filename "{{path}}" does not match the "{{pattern}}" style',
7987
data: {
80-
filename,
81-
namingPattern,
88+
path,
89+
pattern,
8290
},
8391
});
8492
return;

lib/utils/match.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @fileoverview Utils about matching rule
3+
* @author Duke Luo
4+
*/
5+
'use strict';
6+
7+
const micromatch = require('micromatch');
8+
const NAMING_CONVENTION = require('../constants/naming-convention');
9+
10+
/**
11+
* @type {object | undefined} undefined or object with non-matching file path and naming pattern
12+
* @param filePath
13+
* @param targetFilePathPattern
14+
* @param targetNaming
15+
* @param targetNamingPattern
16+
*/
17+
const matchRule = (
18+
filePath,
19+
targetFilePathPattern,
20+
targetNaming,
21+
targetNamingPattern
22+
) => {
23+
if (!micromatch.isMatch(filePath, targetFilePathPattern)) {
24+
return;
25+
} else if (
26+
micromatch.isMatch(
27+
targetNaming,
28+
NAMING_CONVENTION[targetNamingPattern] || targetNamingPattern
29+
)
30+
) {
31+
return;
32+
} else {
33+
return {
34+
path: filePath,
35+
pattern: targetNamingPattern,
36+
};
37+
}
38+
};
39+
40+
module.exports = {
41+
matchRule,
42+
};

0 commit comments

Comments
 (0)