emitWarning.cjs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 process = require('node:process');
  5. // Each code must be unique across all warning types.
  6. const DEPRECATION_CODES = {
  7. COMMONJS_PLUGINS: '001',
  8. COMMONJS_NODEJS_API: '002',
  9. RESULT_OUTPUT_PROPERTY: '003',
  10. GITHUB_FORMATTER: '004',
  11. CONTEXT_FIX: '005',
  12. RULE: '006', // either removal or renaming
  13. REPORT_AMBIGUOUS_POSITION: '007',
  14. };
  15. const EXPERIMENTAL_CODES = {
  16. SUPPRESSIONS: '101',
  17. };
  18. const emittedWarnings = new Set();
  19. /**
  20. * @typedef {DEPRECATION_CODES[keyof DEPRECATION_CODES] |
  21. * EXPERIMENTAL_CODES[keyof EXPERIMENTAL_CODES]} WarningCode
  22. */
  23. /**
  24. * Emit a warning with the specified type.
  25. *
  26. * @param {string} message
  27. * @param {'DeprecationWarning' | 'ExperimentalWarning'} type - See https://nodejs.org/api/process.html#nodejs-warning-names
  28. * @param {WarningCode} code
  29. * @param {string} detail
  30. * @returns {void}
  31. */
  32. function emitWarning(message, type, code, detail) {
  33. const key = JSON.stringify({
  34. message,
  35. options: { type, code: `stylelint:${code}`, detail },
  36. });
  37. if (emittedWarnings.has(key)) return;
  38. emittedWarnings.add(key);
  39. process.emitWarning(message, { type, code: `stylelint:${code}`, detail });
  40. }
  41. /**
  42. * Use this function for deprecation warnings, instead of `process.emitWarning()`.
  43. *
  44. * @param {string} message
  45. * @param {keyof DEPRECATION_CODES} codeKey
  46. * @param {string} detail
  47. * @returns {void}
  48. */
  49. function emitDeprecationWarning(message, codeKey, detail) {
  50. emitWarning(message, 'DeprecationWarning', DEPRECATION_CODES[codeKey], detail);
  51. }
  52. /**
  53. * Use this function for experimental warnings, instead of `process.emitWarning()`.
  54. *
  55. * @param {string} message
  56. * @param {keyof EXPERIMENTAL_CODES} codeKey
  57. * @param {string} detail
  58. * @returns {void}
  59. */
  60. function emitExperimentalWarning(message, codeKey, detail) {
  61. emitWarning(message, 'ExperimentalWarning', EXPERIMENTAL_CODES[codeKey], detail);
  62. }
  63. /**
  64. * Clear the already emitted warnings.
  65. * Only useful in tests.
  66. *
  67. * @private
  68. */
  69. function clearEmittedWarnings() {
  70. if (process.env.NODE_ENV !== 'test') {
  71. throw new Error('This function can only be called in test environments!');
  72. }
  73. emittedWarnings.clear();
  74. }
  75. exports.clearEmittedWarnings = clearEmittedWarnings;
  76. exports.emitDeprecationWarning = emitDeprecationWarning;
  77. exports.emitExperimentalWarning = emitExperimentalWarning;