prepareReturnValue.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. const descriptionlessDisables = require('./descriptionlessDisables');
  3. const invalidScopeDisables = require('./invalidScopeDisables');
  4. const needlessDisables = require('./needlessDisables');
  5. const reportDisables = require('./reportDisables');
  6. /** @typedef {import('stylelint').Formatter} Formatter */
  7. /** @typedef {import('stylelint').LintResult} StylelintResult */
  8. /** @typedef {import('stylelint').LinterOptions["maxWarnings"]} maxWarnings */
  9. /** @typedef {import('stylelint').LinterResult} LinterResult */
  10. /**
  11. * @param {StylelintResult[]} stylelintResults
  12. * @param {maxWarnings} maxWarnings
  13. * @param {Formatter} formatter
  14. * @param {string} cwd
  15. *
  16. * @returns {LinterResult}
  17. */
  18. module.exports = function prepareReturnValue(stylelintResults, maxWarnings, formatter, cwd) {
  19. reportDisables(stylelintResults);
  20. needlessDisables(stylelintResults);
  21. invalidScopeDisables(stylelintResults);
  22. descriptionlessDisables(stylelintResults);
  23. const errored = stylelintResults.some(
  24. (result) =>
  25. result.errored ||
  26. result.parseErrors.length > 0 ||
  27. result.warnings.some((warning) => warning.severity === 'error'),
  28. );
  29. /** @type {LinterResult} */
  30. const returnValue = {
  31. cwd,
  32. errored,
  33. results: [],
  34. output: '',
  35. reportedDisables: [],
  36. ruleMetadata: getRuleMetadata(stylelintResults),
  37. };
  38. if (maxWarnings !== undefined) {
  39. const foundWarnings = stylelintResults.reduce((count, file) => count + file.warnings.length, 0);
  40. if (foundWarnings > maxWarnings) {
  41. returnValue.maxWarningsExceeded = { maxWarnings, foundWarnings };
  42. }
  43. }
  44. returnValue.output = formatter(stylelintResults, returnValue);
  45. returnValue.results = stylelintResults;
  46. return returnValue;
  47. };
  48. /**
  49. * @param {StylelintResult[]} lintResults
  50. */
  51. function getRuleMetadata(lintResults) {
  52. const [lintResult] = lintResults;
  53. if (lintResult === undefined) return {};
  54. if (lintResult._postcssResult === undefined) return {};
  55. return lintResult._postcssResult.stylelint.ruleMetadata;
  56. }