Skip to content

Commit bcd6664

Browse files
committed
refactor: extract constant, rename variable and fix jsdoc
1 parent f7e2e63 commit bcd6664

File tree

4 files changed

+67
-31
lines changed

4 files changed

+67
-31
lines changed

lib/constants/regex.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @file Regex pattern constants
3+
* @author Duke Luo
4+
*/
5+
'use strict';
6+
7+
/**
8+
* @example <1>
9+
*/
10+
const PREFINED_MATCH_SYNTAX_REGEXP = /^<(\d+)>$/;
11+
12+
module.exports = {
13+
PREFINED_MATCH_SYNTAX_REGEXP,
14+
};

lib/rules/filename-naming-convention.js

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

7-
const { transformRuleWithGroupCapture } = require('../utils/transform');
7+
const { transformRuleWithPrefinedMatchSyntax } = require('../utils/transform');
88
const { getFilename, getBasename, getFilePath } = require('../utils/filename');
99
const {
1010
checkSettings,
11-
fileNamingPatternValidator,
11+
filenameNamingPatternValidator,
1212
globPatternValidator,
1313
} = require('../utils/settings');
1414
const { getDocUrl } = require('../utils/doc');
@@ -55,7 +55,7 @@ module.exports = {
5555
const invalidPattern = checkSettings(
5656
rules,
5757
globPatternValidator,
58-
fileNamingPatternValidator
58+
filenameNamingPatternValidator
5959
);
6060

6161
if (invalidPattern) {
@@ -71,18 +71,19 @@ module.exports = {
7171
}
7272

7373
for (const [
74-
originalFexPattern,
74+
originalFilenamePattern,
7575
originalNamingPattern,
7676
] of Object.entries(rules)) {
7777
try {
78-
const [fexPattern, namingPattern] = transformRuleWithGroupCapture(
79-
[originalFexPattern, originalNamingPattern],
80-
filenameWithPath
81-
);
78+
const [filenamePattern, namingPattern] =
79+
transformRuleWithPrefinedMatchSyntax(
80+
[originalFilenamePattern, originalNamingPattern],
81+
filenameWithPath
82+
);
8283

8384
const matchResult = matchRule(
8485
filenameWithPath,
85-
fexPattern,
86+
filenamePattern,
8687
getBasename(filename, ignoreMiddleExtensions),
8788
namingPattern
8889
);

lib/utils/settings.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const isGlob = require('is-glob');
88
const NAMING_CONVENTION = require('../constants/naming-convention');
9+
const { PREFINED_MATCH_SYNTAX_REGEXP } = require('../constants/regex');
910

1011
/**
1112
* Validator
@@ -40,12 +41,13 @@ const namingPatternValidator = (namingPattern) => {
4041
};
4142

4243
/**
43-
* @returns {boolean} true if pattern is a valid naming pattern
44+
* @returns {boolean} true if pattern is a valid filename naming pattern
4445
* @param {string} namingPattern pattern string
4546
*/
46-
const fileNamingPatternValidator = (namingPattern) => {
47+
const filenameNamingPatternValidator = (namingPattern) => {
4748
return (
48-
namingPatternValidator(namingPattern) || !!/^<\d+>$/.test(namingPattern)
49+
namingPatternValidator(namingPattern) ||
50+
PREFINED_MATCH_SYNTAX_REGEXP.test(namingPattern)
4951
);
5052
};
5153

@@ -58,6 +60,6 @@ const globPatternValidator = isGlob;
5860
module.exports = {
5961
checkSettings,
6062
namingPatternValidator,
61-
fileNamingPatternValidator,
63+
filenameNamingPatternValidator,
6264
globPatternValidator,
6365
};

lib/utils/transform.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,60 @@
1+
/* eslint-disable jsdoc/valid-types */
2+
/**
3+
* @file Utils about prefined match syntax
4+
* @author David Ratier, Duke Luo
5+
*/
6+
'use strict';
7+
18
const micromatch = require('micromatch');
9+
const { PREFINED_MATCH_SYNTAX_REGEXP } = require('../constants/regex');
210

311
/**
4-
* Takes in a ruleset and transforms it if it contains capture groups
12+
* Takes in a rule and transforms it if it contains prefined match syntax
513
*
6-
* @param {Array} ruleset ruleset
7-
* @param {Array} ruleset.0 glob
8-
* @param {Array} ruleset.1 rule to transform
14+
* @typedef {string} filenamePattern original filename pattern
15+
* @typedef {string} namingPattern original naming pattern
16+
* @typedef {[filenamePattern, namingPattern]} rule
17+
* @param {rule} rule original rule
918
* @param {string} filenameWithPath filename with path
10-
* @returns {Array} [glob, rule]
19+
* @returns {rule} new rule
20+
* @throws {Error} if a cprefined match syntax referenced in the naming pattern is not found in the filename pattern
1121
*/
12-
function transformRuleWithGroupCapture([glob, rule], filenameWithPath) {
13-
const keyCaptureGroups = micromatch.capture(glob, filenameWithPath);
22+
const transformRuleWithPrefinedMatchSyntax = (
23+
[filenamePattern, namingPattern],
24+
filenameWithPath
25+
) => {
26+
const keyCaptureGroups = micromatch.capture(
27+
filenamePattern,
28+
filenameWithPath
29+
);
1430

1531
if (!keyCaptureGroups) {
16-
return [glob, rule];
32+
return [filenamePattern, namingPattern];
1733
}
1834

19-
const valueCaptureGroupRegex = /<(\d+)>/g;
20-
const valueCaptureGroups = [...rule.matchAll(valueCaptureGroupRegex)];
35+
const valueCaptureGroups = [
36+
...namingPattern.matchAll(new RegExp(PREFINED_MATCH_SYNTAX_REGEXP, 'g')),
37+
];
2138

2239
if (!valueCaptureGroups || !valueCaptureGroups.length) {
23-
return [glob, rule];
40+
return [filenamePattern, namingPattern];
2441
}
2542

26-
const newRule = valueCaptureGroups.reduce((value, group) => {
43+
const newNamingPattern = valueCaptureGroups.reduce((value, group) => {
2744
const groupIndex = +group[1];
45+
2846
if (!keyCaptureGroups || keyCaptureGroups[groupIndex] === undefined) {
2947
throw new Error(
30-
`The capture group "${rule}" is not found in the glob "${glob}"`
48+
`The capture group "${namingPattern}" is not found in the glob "${filenamePattern}"`
3149
);
3250
}
33-
return value.replace(group[0], keyCaptureGroups[+group[1]]);
34-
}, rule);
3551

36-
return [glob, newRule];
37-
}
52+
return value.replace(group[0], keyCaptureGroups[groupIndex]);
53+
}, namingPattern);
54+
55+
return [filenamePattern, newNamingPattern];
56+
};
3857

3958
module.exports = {
40-
transformRuleWithGroupCapture,
59+
transformRuleWithPrefinedMatchSyntax,
4160
};

0 commit comments

Comments
 (0)