-
-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathindex.js
109 lines (88 loc) · 2.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import path from 'node:path';
import {ESLint} from 'eslint';
import {globby, isGitIgnoredSync} from 'globby';
import {isEqual} from 'lodash-es';
import micromatch from 'micromatch';
import arrify from 'arrify';
import slash from 'slash';
import {
parseOptions,
getIgnores,
mergeWithFileConfig,
} from './lib/options-manager.js';
import {mergeReports, processReport, getIgnoredReport} from './lib/report.js';
const runEslint = async (lint, options) => {
const {filePath, eslintOptions, isQuiet} = options;
const {cwd, baseConfig: {ignorePatterns}} = eslintOptions;
if (
filePath
&& (
micromatch.isMatch(path.relative(cwd, filePath), ignorePatterns)
|| isGitIgnoredSync({cwd, ignore: ignorePatterns})(filePath)
)
) {
return getIgnoredReport(filePath);
}
const eslint = new ESLint({
...eslintOptions,
resolvePluginsRelativeTo: __dirname
});
if (filePath && await eslint.isPathIgnored(filePath)) {
return getIgnoredReport(filePath);
}
const report = await lint(eslint);
return processReport(report, {isQuiet});
};
const globFiles = async (patterns, options) => {
const {ignores, extensions, cwd} = (await mergeWithFileConfig(options)).options;
patterns = patterns.length === 0
? [`**/*.{${extensions.join(',')}}`]
: arrify(patterns).map(pattern => slash(pattern));
const files = await globby(
patterns,
{ignore: ignores, gitignore: true, absolute: true, cwd},
);
return files.filter(file => extensions.includes(path.extname(file).slice(1)));
};
const getConfig = async options => {
const {filePath, eslintOptions} = await parseOptions(options);
const engine = new ESLint(eslintOptions);
return engine.calculateConfigForFile(filePath);
};
const lintText = async (string, options) => {
options = await parseOptions(options);
const {filePath, warnIgnored, eslintOptions} = options;
const {ignorePatterns} = eslintOptions.baseConfig;
if (typeof filePath !== 'string' && !isEqual(getIgnores({}), ignorePatterns)) {
throw new Error('The `ignores` option requires the `filePath` option to be defined.');
}
return runEslint(
eslint => eslint.lintText(string, {filePath, warnIgnored}),
options,
);
};
const lintFile = async (filePath, options) => runEslint(
eslint => eslint.lintFiles([filePath]),
await parseOptions({...options, filePath}),
);
const lintFiles = async (patterns, options) => {
const files = await globFiles(patterns, options);
const reports = await Promise.all(
files.map(filePath => lintFile(filePath, options)),
);
const report = mergeReports(reports.filter(({isIgnored}) => !isIgnored));
return report;
};
const getFormatter = async name => {
const {format} = await new ESLint().loadFormatter(name);
return format;
};
const xo = {
getFormatter,
getErrorResults: ESLint.getErrorResults,
outputFixes: async ({results}) => ESLint.outputFixes(results),
getConfig,
lintText,
lintFiles,
};
export default xo;