123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- '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 => {
-
- 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, opts) => {
- report.results = opts.quiet ? eslint.CLIEngine.getErrorResults(report.results) : report.results;
- return report;
- };
- const runEslint = (paths, opts) => {
- const config = optionsManager.buildConfig(opts);
- const engine = new eslint.CLIEngine(config);
- const report = engine.executeOnFiles(paths, config);
- return processReport(report, opts);
- };
- module.exports.lintText = (str, opts) => {
- opts = optionsManager.preprocess(opts);
- if (opts.overrides && opts.overrides.length > 0) {
- const {overrides} = opts;
- delete opts.overrides;
- const filename = path.relative(opts.cwd, opts.filename);
- const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
- opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
- }
- opts = optionsManager.buildConfig(opts);
- const defaultIgnores = optionsManager.getIgnores({}).ignores;
- if (opts.ignores && !isEqual(defaultIgnores, opts.ignores) && typeof opts.filename !== 'string') {
- throw new Error('The `ignores` option requires the `filename` option to be defined.');
- }
- if (opts.filename) {
- const filename = path.relative(opts.cwd, opts.filename);
- if (multimatch(filename, opts.ignores).length > 0 ||
- globby.gitignore.sync({cwd: opts.cwd, ignore: opts.ignores})(opts.filename)) {
- return {
- errorCount: 0,
- warningCount: 0,
- results: [{
- errorCount: 0,
- filePath: filename,
- messages: [],
- warningCount: 0
- }]
- };
- }
- }
- const engine = new eslint.CLIEngine(opts);
- const report = engine.executeOnText(str, opts.filename);
- return processReport(report, opts);
- };
- module.exports.lintFiles = (patterns, opts) => {
- opts = optionsManager.preprocess(opts);
- const isEmptyPatterns = patterns.length === 0;
- const defaultPattern = `**/*.{${opts.extensions.join(',')}}`;
- return globby(
- isEmptyPatterns ? [defaultPattern] : arrify(patterns),
- {ignore: opts.ignores, gitignore: true, cwd: opts.cwd}
- ).then(paths => {
-
-
- if (!isEmptyPatterns) {
- paths = paths.filter(filePath => {
- const ext = path.extname(filePath).replace('.', '');
- return opts.extensions.includes(ext);
- });
- }
- if (!(opts.overrides && opts.overrides.length > 0)) {
- return runEslint(paths, opts);
- }
- const {overrides} = opts;
- delete opts.overrides;
- const grouped = optionsManager.groupConfigs(paths, opts, overrides);
- return mergeReports(grouped.map(data => runEslint(data.paths, data.opts)));
- });
- };
- module.exports.getFormatter = eslint.CLIEngine.getFormatter;
- module.exports.getErrorResults = eslint.CLIEngine.getErrorResults;
- module.exports.outputFixes = eslint.CLIEngine.outputFixes;
|