DEV: Refactor deprecation silencer (#22526)

- Unify the silencing methods, use a WeakMap to remember the seen objects
- Export a proper plugin and use the absolute path in the config, instead
  of the proprietary config from `broccoli-babel-transpiler`

The latter causes problems in Embroider which doesn't use the broccoli
based babel pipeline.
This commit is contained in:
Godfrey Chan 2023-07-11 09:01:51 -07:00 committed by GitHub
parent bdb9ee8507
commit cfa2f1fea8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 41 deletions

View File

@ -17,9 +17,8 @@ module.exports = function (defaults) {
const vendorJs = discourseRoot + "/vendor/assets/javascripts/";
// Silence deprecations which we are aware of - see `lib/deprecation-silencer.js`
const ui = defaults.project.ui;
DeprecationSilencer.silenceUiWarn(ui);
DeprecationSilencer.silenceConsoleWarn();
DeprecationSilencer.silence(console, "warn");
DeprecationSilencer.silence(defaults.project.ui, "writeWarnLine");
const isProduction = EmberApp.env().includes("production");
const isTest = EmberApp.env().includes("test");
@ -95,7 +94,7 @@ module.exports = function (defaults) {
},
babel: {
plugins: [DeprecationSilencer.generateBabelPlugin()],
plugins: [require.resolve("./lib/deprecation-silencer")],
},
// We need to build tests in prod for theme tests

View File

@ -5,52 +5,57 @@ const SILENCED_WARN_PREFIXES = [
"DEPRECATION: Invoking the `<LinkTo>` component with positional arguments is deprecated",
];
let consoleWarnSilenced = false;
module.exports = class DeprecationSilencer {
static silenceUiWarn(ui) {
const oldWriteWarning = ui.writeWarnLine.bind(ui);
ui.writeWarnLine = (message, ...args) => {
if (
!SILENCED_WARN_PREFIXES.some((prefix) => message.startsWith(prefix))
) {
return oldWriteWarning(message, ...args);
}
};
class DeprecationSilencer {
constructor() {
this.silenced = new WeakMap();
}
static silenceConsoleWarn() {
if (consoleWarnSilenced) {
silence(object, method) {
if (this.alreadySilenced(object, method)) {
return;
}
/* eslint-disable no-console */
const oldConsoleWarn = console.warn.bind(console);
console.warn = (message, ...args) => {
if (
!SILENCED_WARN_PREFIXES.some((prefix) => message.startsWith(prefix))
) {
return oldConsoleWarn(message, ...args);
let original = object[method];
object[method] = (message, ...args) => {
if (!this.shouldSilence(message)) {
return original.call(object, message, ...args);
}
};
/* eslint-enable no-console */
consoleWarnSilenced = true;
}
/**
* Generates a dummy babel plugin which applies the console.warn silences in worker
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.
*/
static generateBabelPlugin() {
return {
_parallelBabel: {
requireFile: require.resolve("./deprecation-silencer"),
buildUsing: "babelShim",
},
};
}
static babelShim() {
DeprecationSilencer.silenceConsoleWarn();
module.exports = function () {
DEPRECATION_SILENCER.silence(console, "warn");
return {};
}
};
module.exports.silence = function silence(...args) {
DEPRECATION_SILENCER.silence(...args);
};