UNPKG

3.4 kBJavaScriptView Raw
1'use strict';
2const path = require('path');
3const eslint = require('eslint');
4const globby = require('globby');
5const isEqual = require('lodash.isequal');
6const multimatch = require('multimatch');
7const arrify = require('arrify');
8const optionsManager = require('./options-manager');
9
10exports.lintText = (str, opts) => {
11 opts = optionsManager.preprocess(opts);
12
13 if (opts.overrides && opts.overrides.length > 0) {
14 const overrides = opts.overrides;
15 delete opts.overrides;
16
17 const filename = path.relative(opts.cwd, opts.filename);
18
19 const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
20 opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
21 }
22
23 opts = optionsManager.buildConfig(opts);
24 const defaultIgnores = optionsManager.getIgnores({}).ignores;
25
26 if (opts.ignores && !isEqual(defaultIgnores, opts.ignores) && typeof opts.filename !== 'string') {
27 throw new Error('The `ignores` option requires the `filename` option to be defined.');
28 }
29
30 if (opts.filename) {
31 const filename = path.relative(opts.cwd, opts.filename);
32 const gitIgnores = optionsManager.getGitIgnores(opts);
33 const glob = [filename].concat(gitIgnores);
34
35 if (multimatch(glob, opts.ignores).length > 0) {
36 return {
37 errorCount: 0,
38 warningCount: 0,
39 results: [{
40 errorCount: 0,
41 filePath: filename,
42 messages: [],
43 warningCount: 0
44 }]
45 };
46 }
47 }
48
49 const engine = new eslint.CLIEngine(opts);
50 const report = engine.executeOnText(str, opts.filename);
51
52 return processReport(report, opts);
53};
54
55exports.lintFiles = (patterns, opts) => {
56 opts = optionsManager.preprocess(opts);
57 patterns = patterns.length === 0 ? ['**/*'] : arrify(patterns);
58
59 const gitIgnores = optionsManager.getGitIgnores(opts);
60 const glob = patterns.concat(gitIgnores);
61
62 return globby(glob, {ignore: opts.ignores, nodir: true}).then(paths => {
63 // Filter out unwanted file extensions
64 // for silly users that don't specify an extension in the glob pattern
65 paths = paths.filter(x => {
66 // Remove dot before the actual extension
67 const ext = path.extname(x).replace('.', '');
68 return opts.extensions.indexOf(ext) !== -1;
69 });
70
71 if (!(opts.overrides && opts.overrides.length > 0)) {
72 return runEslint(paths, opts);
73 }
74
75 const overrides = opts.overrides;
76 delete opts.overrides;
77
78 const grouped = optionsManager.groupConfigs(paths, opts, overrides);
79
80 return mergeReports(grouped.map(data => runEslint(data.paths, data.opts)));
81 });
82};
83
84function mergeReports(reports) {
85 // Merge multiple reports into a single report
86 let results = [];
87 let errorCount = 0;
88 let warningCount = 0;
89
90 reports.forEach(report => {
91 results = results.concat(report.results);
92 errorCount += report.errorCount;
93 warningCount += report.warningCount;
94 });
95
96 return {
97 errorCount,
98 warningCount,
99 results
100 };
101}
102
103function runEslint(paths, opts) {
104 const config = optionsManager.buildConfig(opts);
105 const engine = new eslint.CLIEngine(config);
106 const report = engine.executeOnFiles(paths, config);
107
108 return processReport(report, opts);
109}
110
111function processReport(report, opts) {
112 report.results = opts.quiet ? eslint.CLIEngine.getErrorResults(report.results) : report.results;
113 return report;
114}
115
116exports.getFormatter = eslint.CLIEngine.getFormatter;
117exports.getErrorResults = eslint.CLIEngine.getErrorResults;
118exports.outputFixes = eslint.CLIEngine.outputFixes;