discourse/app/assets/javascripts/deprecation-silencer/index.js
Godfrey Chan 3c69570b75
DEV: move deprecation silencer to a shared package (#22668)
This babel plugin is intended to supress the deprecation warnings
from building plugins, however, discourse-plugins does not actually
consume this plugin at all. Currently this happens to work due to
how the babel worker processes are shared and the timing/ordering
of the build, but it will stop working with the embroider build.

This commit extracts the plugin the a shared package so that it
can be properly consumed by discourse-plugins as well as core.
2023-07-18 19:07:20 +01:00

60 lines
1.3 KiB
JavaScript

const SILENCED_WARN_PREFIXES = [
"Setting the `jquery-integration` optional feature flag",
"DEPRECATION: Invoking the `<LinkTo>` component with positional arguments is deprecated",
];
class DeprecationSilencer {
constructor() {
this.silenced = new WeakMap();
}
silence(object, method) {
if (this.alreadySilenced(object, method)) {
return;
}
let original = object[method];
object[method] = (message, ...args) => {
if (!this.shouldSilence(message)) {
return original.call(object, message, ...args);
}
};
}
alreadySilenced(object, method) {
let set = this.silenced.get(object);
if (!set) {
set = new Set();
this.silenced.set(object, set);
}
if (set.has(method)) {
return true;
} else {
set.add(method);
return false;
}
}
shouldSilence(message) {
return SILENCED_WARN_PREFIXES.some((prefix) => message.startsWith(prefix));
}
}
const DEPRECATION_SILENCER = new DeprecationSilencer();
/**
* Export a dummy babel plugin which applies the console.warn silences in worker
* processes. Does not actually affect babel output.
*/
module.exports = function () {
DEPRECATION_SILENCER.silence(console, "warn");
return {};
};
module.exports.silence = function silence(...args) {
DEPRECATION_SILENCER.silence(...args);
};