Skip to content

Commit f1b3c0e

Browse files
committed
refactor: move matchRule method and rename file
1 parent 0fae3f3 commit f1b3c0e

File tree

5 files changed

+42
-51
lines changed

5 files changed

+42
-51
lines changed

lib/rules/filename-blocklist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const {
1010
globPatternValidator,
1111
} = require('../utils/settings');
1212
const { getDocUrl } = require('../utils/doc');
13-
const { matchRule } = require('../utils/match');
1413
const { FILENAME_BLOCKLIST_ERROR_MESSAGE } = require('../constants/message');
14+
const { matchRule } = require('../utils/rule');
1515

1616
/** @typedef {module:eslint} ESLint */
1717
/**

lib/rules/filename-naming-convention.js

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

7-
const { transformRuleWithPrefinedMatchSyntax } = require('../utils/transform');
7+
const {
8+
transformRuleWithPrefinedMatchSyntax,
9+
matchRule,
10+
} = require('../utils/rule');
811
const { getFilename, getBasename, getFilePath } = require('../utils/filename');
912
const {
1013
validateNamingPatternObject,
1114
filenameNamingPatternValidator,
1215
globPatternValidator,
1316
} = require('../utils/settings');
1417
const { getDocUrl } = require('../utils/doc');
15-
const { matchRule } = require('../utils/match');
1618
const {
1719
FILENAME_NAMING_CONVENTION_ERROR_MESSAGE,
1820
} = require('../constants/message');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const {
1414
globPatternValidator,
1515
} = require('../utils/settings');
1616
const { getDocUrl } = require('../utils/doc');
17-
const { matchRule } = require('../utils/match');
1817
const { FOLDER_MATCH_WITH_FEX_ERROR_MESSAGE } = require('../constants/message');
18+
const { matchRule } = require('../utils/rule');
1919

2020
/** @typedef {module:eslint} ESLint */
2121
/**

lib/utils/match.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

lib/utils/transform.js renamed to lib/utils/rule.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
/* eslint-disable jsdoc/valid-types */
21
/**
3-
* @file Utils about prefined match syntax
2+
* @file Utils about rule
43
* @author David Ratier, Duke Luo
54
*/
65
'use strict';
76

87
const micromatch = require('micromatch');
9-
const { isNil, isEmpty } = require('../utils/utility');
8+
const { isNil, isEmpty } = require('./utility');
109
const { PREFINED_MATCH_SYNTAX_REGEXP } = require('../constants/regex');
1110
const { PREFINED_MATCH_SYNTAX_ERROR_MESSAGE } = require('../constants/message');
11+
const NAMING_CONVENTION = require('../constants/naming-convention');
1212

1313
/**
1414
* Takes in a rule and transforms it if it contains prefined match syntax
@@ -57,6 +57,39 @@ const transformRuleWithPrefinedMatchSyntax = (
5757
return [filenamePattern, newNamingPattern];
5858
};
5959

60+
/**
61+
* @returns {object | undefined} undefined or object with non-matching file path and naming pattern
62+
* @param {string} filePath file path
63+
* @param {string} targetFilePathPattern path pattern of the target file
64+
* @param {string} [targetNaming] target naming
65+
* @param {string} [targetNamingPattern] naming pattern of the target naming
66+
*/
67+
const matchRule = (
68+
filePath,
69+
targetFilePathPattern,
70+
targetNaming,
71+
targetNamingPattern
72+
) => {
73+
if (!micromatch.isMatch(filePath, targetFilePathPattern)) {
74+
return;
75+
} else if (
76+
targetNaming &&
77+
targetNamingPattern &&
78+
micromatch.isMatch(
79+
targetNaming,
80+
NAMING_CONVENTION[targetNamingPattern] || targetNamingPattern
81+
)
82+
) {
83+
return;
84+
} else {
85+
return {
86+
path: filePath,
87+
pattern: targetNamingPattern,
88+
};
89+
}
90+
};
91+
6092
module.exports = {
6193
transformRuleWithPrefinedMatchSyntax,
94+
matchRule,
6295
};

0 commit comments

Comments
 (0)