githubFormatter.js 1.1 KB

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