index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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('./lib/options-manager');
  9. const mergeReports = reports => {
  10. // Merge multiple reports into a single report
  11. let results = [];
  12. let errorCount = 0;
  13. let warningCount = 0;
  14. for (const report of reports) {
  15. results = results.concat(report.results);
  16. errorCount += report.errorCount;
  17. warningCount += report.warningCount;
  18. }
  19. return {
  20. errorCount,
  21. warningCount,
  22. results
  23. };
  24. };
  25. const processReport = (report, opts) => {
  26. report.results = opts.quiet ? eslint.CLIEngine.getErrorResults(report.results) : report.results;
  27. return report;
  28. };
  29. const runEslint = (paths, opts) => {
  30. const config = optionsManager.buildConfig(opts);
  31. const engine = new eslint.CLIEngine(config);
  32. const report = engine.executeOnFiles(paths, config);
  33. return processReport(report, opts);
  34. };
  35. module.exports.lintText = (str, opts) => {
  36. opts = optionsManager.preprocess(opts);
  37. if (opts.overrides && opts.overrides.length > 0) {
  38. const {overrides} = opts;
  39. delete opts.overrides;
  40. const filename = path.relative(opts.cwd, opts.filename);
  41. const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
  42. opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
  43. }
  44. opts = optionsManager.buildConfig(opts);
  45. const defaultIgnores = optionsManager.getIgnores({}).ignores;
  46. if (opts.ignores && !isEqual(defaultIgnores, opts.ignores) && typeof opts.filename !== 'string') {
  47. throw new Error('The `ignores` option requires the `filename` option to be defined.');
  48. }
  49. if (opts.filename) {
  50. const filename = path.relative(opts.cwd, opts.filename);
  51. if (multimatch(filename, opts.ignores).length > 0 ||
  52. globby.gitignore.sync({cwd: opts.cwd, ignore: opts.ignores})(opts.filename)) {
  53. return {
  54. errorCount: 0,
  55. warningCount: 0,
  56. results: [{
  57. errorCount: 0,
  58. filePath: filename,
  59. messages: [],
  60. warningCount: 0
  61. }]
  62. };
  63. }
  64. }
  65. const engine = new eslint.CLIEngine(opts);
  66. const report = engine.executeOnText(str, opts.filename);
  67. return processReport(report, opts);
  68. };
  69. module.exports.lintFiles = (patterns, opts) => {
  70. opts = optionsManager.preprocess(opts);
  71. const isEmptyPatterns = patterns.length === 0;
  72. const defaultPattern = `**/*.{${opts.extensions.join(',')}}`;
  73. return globby(
  74. isEmptyPatterns ? [defaultPattern] : arrify(patterns),
  75. {ignore: opts.ignores, gitignore: true, cwd: opts.cwd}
  76. ).then(paths => {
  77. // Filter out unwanted file extensions
  78. // For silly users that don't specify an extension in the glob pattern
  79. if (!isEmptyPatterns) {
  80. paths = paths.filter(filePath => {
  81. const ext = path.extname(filePath).replace('.', '');
  82. return opts.extensions.includes(ext);
  83. });
  84. }
  85. if (!(opts.overrides && opts.overrides.length > 0)) {
  86. return runEslint(paths, opts);
  87. }
  88. const {overrides} = opts;
  89. delete opts.overrides;
  90. const grouped = optionsManager.groupConfigs(paths, opts, overrides);
  91. return mergeReports(grouped.map(data => runEslint(data.paths, data.opts)));
  92. });
  93. };
  94. module.exports.getFormatter = eslint.CLIEngine.getFormatter;
  95. module.exports.getErrorResults = eslint.CLIEngine.getErrorResults;
  96. module.exports.outputFixes = eslint.CLIEngine.outputFixes;