Skip to content

Commit fa1bfa4

Browse files
committed
feat: migrate to asynchronous API
* Update `rule-finder.js` and its test cases
1 parent b87c9c9 commit fa1bfa4

File tree

2 files changed

+101
-85
lines changed

2 files changed

+101
-85
lines changed

src/lib/rule-finder.js

+35-19
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,38 @@ function _getConfigFile(specifiedFile) {
1818
return require(path.join(process.cwd(), 'package.json')).main;
1919
}
2020

21-
function _getConfigs(configFile, files) {
22-
const cliEngine = eslint.ESLint
23-
? new eslint.ESLint({
21+
async function _getConfigs(configFile, files) {
22+
let isPathIgnored;
23+
let getConfigForFile;
24+
25+
if (eslint.ESLint) {
26+
const esLint = new eslint.ESLint({
2427
// Ignore any config applicable depending on the location on the filesystem
2528
useEslintrc: false,
2629
// Point to the particular config
2730
overrideConfigFile: configFile
28-
})
29-
: new eslint.CLIEngine({
31+
});
32+
isPathIgnored = esLint.isPathIgnored.bind(esLint);
33+
getConfigForFile = esLint.calculateConfigForFile.bind(esLint);
34+
} else {
35+
const cliEngine = new eslint.CLIEngine({
3036
// Ignore any config applicable depending on the location on the filesystem
3137
useEslintrc: false,
3238
// Point to the particular config
3339
configFile
3440
});
41+
isPathIgnored = cliEngine.isPathIgnored.bind(cliEngine);
42+
getConfigForFile = cliEngine.getConfigForFile.bind(cliEngine);
43+
}
3544

36-
return new Set(files
37-
.map(filePath => cliEngine.isPathIgnored(filePath) ? false : cliEngine.getConfigForFile(filePath))
38-
.filter(Boolean));
45+
const configs = files.map(async filePath => (
46+
await isPathIgnored(filePath) ? false : getConfigForFile(filePath)
47+
));
48+
return new Set((await Promise.all(configs)).filter(Boolean));
3949
}
4050

41-
function _getConfig(configFile, files) {
42-
return Array.from(_getConfigs(configFile, files)).reduce((prev, item) => {
51+
async function _getConfig(configFile, files) {
52+
return Array.from(await _getConfigs(configFile, files)).reduce((prev, item) => {
4353
return Object.assign(prev, item, {rules: Object.assign({}, prev.rules, item.rules)});
4454
}, {});
4555
}
@@ -98,14 +108,7 @@ function _createExtensionRegExp(extensions) {
98108
return new RegExp(`.\\.(?:${normalizedExts.join("|")})$`);
99109
}
100110

101-
function RuleFinder(specifiedFile, {omitCore, includeDeprecated, ext = ['.js']}) {
102-
const configFile = _getConfigFile(specifiedFile);
103-
104-
const extensionRegExp = _createExtensionRegExp(ext);
105-
const files = glob.sync(`**/*`, {dot: true, matchBase: true})
106-
.filter(file => extensionRegExp.test(file));
107-
108-
const config = _getConfig(configFile, files);
111+
function RuleFinder(config, {omitCore, includeDeprecated}) {
109112
let currentRuleNames = _getCurrentNamesRules(config);
110113
if (omitCore) {
111114
currentRuleNames = currentRuleNames.filter(_isNotCore);
@@ -143,6 +146,19 @@ function RuleFinder(specifiedFile, {omitCore, includeDeprecated, ext = ['.js']})
143146
this.getDeprecatedRules = () => getSortedRules(deprecatedRuleNames);
144147
}
145148

149+
async function createRuleFinder(specifiedFile, options) {
150+
const configFile = _getConfigFile(specifiedFile);
151+
152+
const {ext = ['.js']} = options;
153+
const extensionRegExp = _createExtensionRegExp(ext);
154+
const files = glob.sync(`**/*`, {dot: true, matchBase: true})
155+
.filter(file => extensionRegExp.test(file));
156+
157+
const config = await _getConfig(configFile, files);
158+
159+
return new RuleFinder(config, options);
160+
}
161+
146162
module.exports = function (specifiedFile, options = {}) {
147-
return new RuleFinder(specifiedFile, options);
163+
return createRuleFinder(specifiedFile, options);
148164
};

0 commit comments

Comments
 (0)