preprocessWarnings.cjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /** @import {LintResult} from 'stylelint' */
  6. /** @typedef {LintResult['parseErrors'][0]} ParseError */
  7. /** @typedef {LintResult['warnings'][0]} Warning */
  8. /** @typedef {Warning['severity']} Severity */
  9. /**
  10. * Preprocess warnings in a given lint result.
  11. * Note that this function has a side-effect.
  12. *
  13. * @param {LintResult} result
  14. * @returns {LintResult}
  15. */
  16. function preprocessWarnings(result) {
  17. for (const error of result.parseErrors || []) {
  18. result.warnings.push(parseErrorToWarning(error));
  19. }
  20. for (const warning of result.warnings) {
  21. warning.severity = normalizeSeverity(warning);
  22. }
  23. result.warnings.sort(byLocationOrder);
  24. return result;
  25. }
  26. /**
  27. * @param {ParseError} error
  28. * @returns {Warning}
  29. */
  30. function parseErrorToWarning(error) {
  31. return {
  32. line: error.line,
  33. column: error.column,
  34. rule: error.stylelintType,
  35. severity: constants.SEVERITY_ERROR,
  36. text: `${error.text} (${error.stylelintType})`,
  37. };
  38. }
  39. /**
  40. * @param {Warning} warning
  41. * @returns {Severity}
  42. */
  43. function normalizeSeverity(warning) {
  44. // NOTE: Plugins may add a warning without severity, for example,
  45. // by directly using the PostCSS `Result#warn()` API.
  46. return warning.severity || constants.DEFAULT_SEVERITY;
  47. }
  48. /**
  49. * @param {Warning} a
  50. * @param {Warning} b
  51. * @returns {number}
  52. */
  53. function byLocationOrder(a, b) {
  54. // positionless first
  55. if (!a.line && b.line) return -1;
  56. // positionless first
  57. if (a.line && !b.line) return 1;
  58. if (a.line < b.line) return -1;
  59. if (a.line > b.line) return 1;
  60. if (a.column < b.column) return -1;
  61. if (a.column > b.column) return 1;
  62. return 0;
  63. }
  64. module.exports = preprocessWarnings;