createStylelintResult.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const createPartialStylelintResult = require('./createPartialStylelintResult');
  3. const getConfigForFile = require('./getConfigForFile');
  4. /** @typedef {import('stylelint').PostcssResult} PostcssResult */
  5. /** @typedef {import('stylelint').LintResult} StylelintResult */
  6. /**
  7. * @param {import('stylelint').InternalApi} stylelint
  8. * @param {PostcssResult} [postcssResult]
  9. * @param {string} [filePath]
  10. * @param {import('stylelint').CssSyntaxError} [cssSyntaxError]
  11. * @return {Promise<StylelintResult>}
  12. */
  13. module.exports = async function createStylelintResult(
  14. stylelint,
  15. postcssResult,
  16. filePath,
  17. cssSyntaxError,
  18. ) {
  19. let stylelintResult = createPartialStylelintResult(postcssResult, cssSyntaxError);
  20. const configForFile = await getConfigForFile(stylelint, filePath, filePath);
  21. const config = configForFile === null ? {} : configForFile.config;
  22. const file = stylelintResult.source || (cssSyntaxError && cssSyntaxError.file);
  23. if (config.resultProcessors) {
  24. for (const resultProcessor of config.resultProcessors) {
  25. // Result processors might just mutate the result object,
  26. // or might return a new one
  27. const returned = resultProcessor(stylelintResult, file);
  28. if (returned) {
  29. stylelintResult = returned;
  30. }
  31. }
  32. }
  33. return stylelintResult;
  34. };