| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import process from 'node:process';
- // Each code must be unique across all warning types.
- const DEPRECATION_CODES = {
- COMMONJS_PLUGINS: '001',
- COMMONJS_NODEJS_API: '002',
- RESULT_OUTPUT_PROPERTY: '003',
- GITHUB_FORMATTER: '004',
- CONTEXT_FIX: '005',
- RULE: '006', // either removal or renaming
- REPORT_AMBIGUOUS_POSITION: '007',
- };
- const EXPERIMENTAL_CODES = {
- SUPPRESSIONS: '101',
- };
- const emittedWarnings = new Set();
- /**
- * @typedef {DEPRECATION_CODES[keyof DEPRECATION_CODES] |
- * EXPERIMENTAL_CODES[keyof EXPERIMENTAL_CODES]} WarningCode
- */
- /**
- * Emit a warning with the specified type.
- *
- * @param {string} message
- * @param {'DeprecationWarning' | 'ExperimentalWarning'} type - See https://nodejs.org/api/process.html#nodejs-warning-names
- * @param {WarningCode} code
- * @param {string} detail
- * @returns {void}
- */
- function emitWarning(message, type, code, detail) {
- const key = JSON.stringify({
- message,
- options: { type, code: `stylelint:${code}`, detail },
- });
- if (emittedWarnings.has(key)) return;
- emittedWarnings.add(key);
- process.emitWarning(message, { type, code: `stylelint:${code}`, detail });
- }
- /**
- * Use this function for deprecation warnings, instead of `process.emitWarning()`.
- *
- * @param {string} message
- * @param {keyof DEPRECATION_CODES} codeKey
- * @param {string} detail
- * @returns {void}
- */
- export function emitDeprecationWarning(message, codeKey, detail) {
- emitWarning(message, 'DeprecationWarning', DEPRECATION_CODES[codeKey], detail);
- }
- /**
- * Use this function for experimental warnings, instead of `process.emitWarning()`.
- *
- * @param {string} message
- * @param {keyof EXPERIMENTAL_CODES} codeKey
- * @param {string} detail
- * @returns {void}
- */
- export function emitExperimentalWarning(message, codeKey, detail) {
- emitWarning(message, 'ExperimentalWarning', EXPERIMENTAL_CODES[codeKey], detail);
- }
- /**
- * Clear the already emitted warnings.
- * Only useful in tests.
- *
- * @private
- */
- export function clearEmittedWarnings() {
- if (process.env.NODE_ENV !== 'test') {
- throw new Error('This function can only be called in test environments!');
- }
- emittedWarnings.clear();
- }
|