createPartialStylelintResult.cjs 3.6 KB

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