1 | 'use strict';
|
2 | const path = require('path');
|
3 | const eslint = require('eslint');
|
4 | const globby = require('globby');
|
5 | const isEqual = require('lodash.isequal');
|
6 | const multimatch = require('multimatch');
|
7 | const arrify = require('arrify');
|
8 | const optionsManager = require('./options-manager');
|
9 |
|
10 | exports.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 |
|
55 | exports.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 |
|
64 |
|
65 | paths = paths.filter(x => {
|
66 |
|
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 |
|
84 | function mergeReports(reports) {
|
85 |
|
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 |
|
103 | function 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 |
|
111 | function processReport(report, opts) {
|
112 | report.results = opts.quiet ? eslint.CLIEngine.getErrorResults(report.results) : report.results;
|
113 | return report;
|
114 | }
|
115 |
|
116 | exports.getFormatter = eslint.CLIEngine.getFormatter;
|
117 | exports.getErrorResults = eslint.CLIEngine.getErrorResults;
|
118 | exports.outputFixes = eslint.CLIEngine.outputFixes;
|