-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (102 loc) · 3.5 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
const path = require('path');
const eslint = require('eslint');
const globby = require('globby');
const isEqual = require('lodash.isequal');
const multimatch = require('multimatch');
const arrify = require('arrify');
const optionsManager = require('./lib/options-manager');
const mergeReports = reports => {
// Merge multiple reports into a single report
let results = [];
let errorCount = 0;
let warningCount = 0;
for (const report of reports) {
results = results.concat(report.results);
errorCount += report.errorCount;
warningCount += report.warningCount;
}
return {
errorCount,
warningCount,
results
};
};
const processReport = (report, options) => {
report.results = options.quiet ? eslint.CLIEngine.getErrorResults(report.results) : report.results;
return report;
};
const runEslint = (paths, options) => {
const config = optionsManager.buildConfig(options);
const engine = new eslint.CLIEngine(config);
const report = engine.executeOnFiles(paths, config);
return processReport(report, options);
};
module.exports.lintText = (str, options) => {
options = optionsManager.preprocess(options);
if (options.overrides && options.overrides.length > 0) {
const {overrides} = options;
delete options.overrides;
const filename = path.relative(options.cwd, options.filename);
const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
options = optionsManager.mergeApplicableOverrides(options, foundOverrides.applicable);
}
options = optionsManager.buildConfig(options);
const defaultIgnores = optionsManager.getIgnores({}).ignores;
if (options.ignores && !isEqual(defaultIgnores, options.ignores) && typeof options.filename !== 'string') {
throw new Error('The `ignores` option requires the `filename` option to be defined.');
}
if (options.filename) {
const filename = path.relative(options.cwd, options.filename);
if (
multimatch(filename, options.ignores).length > 0 ||
globby.gitignore.sync({cwd: options.cwd, ignore: options.ignores})(options.filename)
) {
return {
errorCount: 0,
warningCount: 0,
results: [{
errorCount: 0,
filePath: filename,
messages: [],
warningCount: 0
}]
};
}
}
const engine = new eslint.CLIEngine(options);
const report = engine.executeOnText(str, options.filename);
return processReport(report, options);
};
module.exports.lintFiles = (patterns, options) => {
options = optionsManager.preprocess(options);
const isEmptyPatterns = patterns.length === 0;
const defaultPattern = `**/*.{${options.extensions.join(',')}}`;
return globby(
isEmptyPatterns ? [defaultPattern] : arrify(patterns),
{
ignore: options.ignores,
gitignore: true,
cwd: options.cwd
}
).then(paths => {
// Filter out unwanted file extensions
// For silly users that don't specify an extension in the glob pattern
if (!isEmptyPatterns) {
paths = paths.filter(filePath => {
const ext = path.extname(filePath).replace('.', '');
return options.extensions.includes(ext);
});
}
if (!(options.overrides && options.overrides.length > 0)) {
return runEslint(paths, options);
}
const {overrides} = options;
delete options.overrides;
const grouped = optionsManager.groupConfigs(paths, options, overrides);
return mergeReports(grouped.map(data => runEslint(data.paths, data.options)));
});
};
module.exports.getFormatter = eslint.CLIEngine.getFormatter;
module.exports.getErrorResults = eslint.CLIEngine.getErrorResults;
module.exports.outputFixes = eslint.CLIEngine.outputFixes;