webpack/test/helpers/deprecationTracking.js

60 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const util = require("util");
2025-05-01 22:36:51 +08:00
/** @type {undefined | Map<string, { code: string, message: string, stack: string }>} */
2024-07-31 06:15:03 +08:00
let interception;
const originalDeprecate = util.deprecate;
2025-04-16 22:04:11 +08:00
/**
* @template {EXPECTED_FUNCTION} T
* @param {T} fn fn
* @param {string} message message
2025-05-01 22:36:51 +08:00
* @param {string=} _code code
2025-04-16 22:04:11 +08:00
* @returns {T} result
*/
2025-05-01 22:36:51 +08:00
util.deprecate = (fn, message, _code) => {
const original = originalDeprecate(fn, message, _code);
2025-04-16 22:04:11 +08:00
// @ts-expect-error expected
2025-07-03 17:06:45 +08:00
return function deprecate(...args) {
if (interception) {
2025-05-01 22:36:51 +08:00
interception.set(`${_code}: ${message}`, {
code: /** @type {string} */ (_code),
message,
2025-05-01 22:36:51 +08:00
stack: /** @type {string} */ (new Error(message).stack)
});
2025-04-16 22:04:11 +08:00
// @ts-expect-error expected
return fn.apply(this, args);
}
2024-07-31 04:21:27 +08:00
2025-04-16 22:04:11 +08:00
// @ts-expect-error expected
2024-07-31 04:21:27 +08:00
return original.apply(this, args);
};
};
2025-05-01 22:36:51 +08:00
/**
* @returns {() => EXPECTED_ANY} result
*/
2025-07-08 22:46:17 +08:00
module.exports.start = () => {
interception = new Map();
return () => {
const map = interception;
interception = undefined;
2025-07-03 17:06:45 +08:00
return [...(map || [])]
.sort(([a], [b]) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
})
2025-07-08 22:46:17 +08:00
.map(([_key, data]) => data);
};
};