createPartialStylelintResult.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { SEVERITY_ERROR } from './constants.mjs';
  2. import process from 'node:process';
  3. /** @import {LintResult as StylelintResult, PostcssResult} from 'stylelint' */
  4. /**
  5. * @param {PostcssResult} [postcssResult]
  6. * @param {import('stylelint').CssSyntaxError} [cssSyntaxError]
  7. * @returns {StylelintResult}
  8. */
  9. export default function createPartialStylelintResult(postcssResult, cssSyntaxError) {
  10. /** @type {StylelintResult} */
  11. let stylelintResult;
  12. /** @type {string | undefined} */
  13. let source;
  14. if (postcssResult && postcssResult.root) {
  15. if (postcssResult.root.source) {
  16. source = postcssResult.root.source.input.file;
  17. if (!source && 'id' in postcssResult.root.source.input) {
  18. source = postcssResult.root.source.input.id;
  19. }
  20. }
  21. // @ts-expect-error -- TS2339: Property 'noDeprecation' does not exist on type 'Process'.
  22. // https://github.com/nodejs/node/blob/main/doc/api/process.md#processnodeprecation
  23. const includeDeprecations = !process.noDeprecation;
  24. const deprecationMessages = includeDeprecations
  25. ? postcssResult.messages.filter((message) => message.stylelintType === 'deprecation')
  26. : [];
  27. const deprecations = deprecationMessages.map((deprecationMessage) => {
  28. return {
  29. text: deprecationMessage.text,
  30. reference: deprecationMessage.stylelintReference,
  31. };
  32. });
  33. const invalidOptionMessages = postcssResult.messages.filter(
  34. (message) => message.stylelintType === 'invalidOption',
  35. );
  36. const invalidOptionWarnings = invalidOptionMessages.map((invalidOptionMessage) => {
  37. return {
  38. text: invalidOptionMessage.text,
  39. };
  40. });
  41. const parseErrors = postcssResult.messages.filter(
  42. (message) => message.stylelintType === 'parseError',
  43. );
  44. // Remove deprecation warnings, invalid options, and parse errors from the messages
  45. postcssResult.messages = postcssResult.messages.filter(
  46. (message) =>
  47. message.stylelintType !== 'deprecation' &&
  48. message.stylelintType !== 'invalidOption' &&
  49. message.stylelintType !== 'parseError',
  50. );
  51. // This defines the stylelint result object that formatters receive
  52. stylelintResult = {
  53. source,
  54. deprecations,
  55. invalidOptionWarnings,
  56. // @ts-expect-error -- TS2322: Type 'Message[]' is not assignable to type '(Warning & { stylelintType: string; })[]'.
  57. parseErrors,
  58. errored: postcssResult.stylelint.stylelintError,
  59. warnings: postcssResult.messages.map((message) => {
  60. return {
  61. line: message.line,
  62. column: message.column,
  63. endLine: message.endLine,
  64. endColumn: message.endColumn,
  65. fix: message.fix,
  66. rule: message.rule,
  67. severity: message.severity,
  68. text: message.text,
  69. url: message.url,
  70. };
  71. }),
  72. ignored: postcssResult.stylelint.ignored,
  73. _postcssResult: postcssResult,
  74. };
  75. } else if (cssSyntaxError) {
  76. if (cssSyntaxError.name !== 'CssSyntaxError') {
  77. throw cssSyntaxError;
  78. }
  79. stylelintResult = {
  80. source: cssSyntaxError.file || '<input css 1>',
  81. deprecations: [],
  82. invalidOptionWarnings: [],
  83. parseErrors: [],
  84. errored: true,
  85. warnings: [
  86. {
  87. line: cssSyntaxError.line,
  88. column: cssSyntaxError.column,
  89. endLine: cssSyntaxError.endLine,
  90. endColumn: cssSyntaxError.endColumn,
  91. rule: cssSyntaxError.name,
  92. severity: SEVERITY_ERROR,
  93. text: `${cssSyntaxError.reason} (${cssSyntaxError.name})`,
  94. },
  95. ],
  96. };
  97. } else {
  98. throw new Error(
  99. 'createPartialStylelintResult must be called with either postcssResult or CssSyntaxError',
  100. );
  101. }
  102. return stylelintResult;
  103. }