githubFormatter.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import preprocessWarnings from './preprocessWarnings.mjs';
  2. /**
  3. * @see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
  4. *
  5. * @type {import('stylelint').Formatter}
  6. *
  7. * @deprecated See https://stylelint.io/awesome-stylelint#formatters for alternative formatters.
  8. */
  9. export default function githubFormatter(results, returnValue) {
  10. const title = 'Stylelint problem';
  11. const metadata = returnValue.ruleMetadata;
  12. const lines = results.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. lines.push(`::notice title=Stylelint deprecation::The github formatter is deprecated`);
  22. lines.push('');
  23. return lines.join('\n');
  24. }
  25. /**
  26. * @param {string} msg
  27. * @param {Partial<import('stylelint').RuleMeta> | undefined} metadata
  28. * @returns {string}
  29. */
  30. function buildMessage(msg, metadata) {
  31. if (!metadata) return msg;
  32. const url = metadata.url ? ` - ${metadata.url}` : '';
  33. let additional = [
  34. metadata.fixable ? 'maybe fixable' : '',
  35. metadata.deprecated ? 'deprecated' : '',
  36. ]
  37. .filter(Boolean)
  38. .join(', ');
  39. additional = additional ? ` [${additional}]` : '';
  40. return `${msg}${additional}${url}`;
  41. }