githubFormatter.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. const preprocessWarnings = require('./preprocessWarnings');
  3. /**
  4. * @see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
  5. *
  6. * @type {import('stylelint').Formatter}
  7. */
  8. module.exports = function githubFormatter(results, returnValue) {
  9. const title = 'Stylelint problem';
  10. const metadata = returnValue.ruleMetadata;
  11. return results
  12. .flatMap((result) => {
  13. const { source, warnings } = preprocessWarnings(result);
  14. return warnings.map(({ line, column, endLine, endColumn, text, severity, rule }) => {
  15. const msg = buildMessage(text, metadata[rule]);
  16. return endLine === undefined
  17. ? `::${severity} file=${source},line=${line},col=${column},title=${title}::${msg}`
  18. : `::${severity} file=${source},line=${line},col=${column},endLine=${endLine},endColumn=${endColumn},title=${title}::${msg}`;
  19. });
  20. })
  21. .join('\n');
  22. };
  23. /**
  24. * @param {string} msg
  25. * @param {Partial<import('stylelint').RuleMeta> | undefined} metadata
  26. * @returns {string}
  27. */
  28. function buildMessage(msg, metadata) {
  29. if (!metadata) return msg;
  30. const url = metadata.url ? ` - ${metadata.url}` : '';
  31. let additional = [
  32. metadata.fixable ? 'maybe fixable' : '',
  33. metadata.deprecated ? 'deprecated' : '',
  34. ]
  35. .filter(Boolean)
  36. .join(', ');
  37. additional = additional ? ` [${additional}]` : '';
  38. return `${msg}${additional}${url}`;
  39. }