DEV: extend `withSilencedDeprecations` to work for ember deprecations (#24153)

This will allow us to globally unsilence deprecations for plugin/theme authors while silencing specific cases in Discourse core.
This commit is contained in:
David Taylor 2023-10-30 12:09:45 +00:00 committed by GitHub
parent b7cafdc07f
commit 3071535a14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 5 deletions

View File

@ -3,6 +3,8 @@ const disabledDeprecations = new Set();
const emberCliDeprecationWorkflows = const emberCliDeprecationWorkflows =
window.deprecationWorkflow?.config?.workflow; window.deprecationWorkflow?.config?.workflow;
let emberDeprecationSilencer;
/** /**
* Display a deprecation warning with the provided message. The warning will be prefixed with the theme/plugin name * Display a deprecation warning with the provided message. The warning will be prefixed with the theme/plugin name
* if it can be automatically determined based on the current stack. * if it can be automatically determined based on the current stack.
@ -72,6 +74,7 @@ export function registerDeprecationHandler(callback) {
* @param {function} callback The function to call while deprecations are silenced. * @param {function} callback The function to call while deprecations are silenced.
*/ */
export function withSilencedDeprecations(deprecationIds, callback) { export function withSilencedDeprecations(deprecationIds, callback) {
ensureEmberDeprecationSilencer();
const idArray = [].concat(deprecationIds); const idArray = [].concat(deprecationIds);
try { try {
idArray.forEach((id) => disabledDeprecations.add(id)); idArray.forEach((id) => disabledDeprecations.add(id));
@ -94,6 +97,7 @@ export function withSilencedDeprecations(deprecationIds, callback) {
* @param {function} callback The asynchronous function to call while deprecations are silenced. * @param {function} callback The asynchronous function to call while deprecations are silenced.
*/ */
export async function withSilencedDeprecationsAsync(deprecationIds, callback) { export async function withSilencedDeprecationsAsync(deprecationIds, callback) {
ensureEmberDeprecationSilencer();
const idArray = [].concat(deprecationIds); const idArray = [].concat(deprecationIds);
try { try {
idArray.forEach((id) => disabledDeprecations.add(id)); idArray.forEach((id) => disabledDeprecations.add(id));
@ -102,3 +106,23 @@ export async function withSilencedDeprecationsAsync(deprecationIds, callback) {
idArray.forEach((id) => disabledDeprecations.delete(id)); idArray.forEach((id) => disabledDeprecations.delete(id));
} }
} }
function ensureEmberDeprecationSilencer() {
if (emberDeprecationSilencer) {
return;
}
emberDeprecationSilencer = (message, options, next) => {
if (options?.id && disabledDeprecations.has(options.id)) {
return;
} else {
next(message, options);
}
};
if (require.has("@ember/debug")) {
require("@ember/debug").registerDeprecationHandler(
emberDeprecationSilencer
);
}
}

View File

@ -6,7 +6,9 @@ import { dasherize } from "@ember/string";
import $ from "jquery"; import $ from "jquery";
import { CLOSE_INITIATED_BY_MODAL_SHOW } from "discourse/components/d-modal"; import { CLOSE_INITIATED_BY_MODAL_SHOW } from "discourse/components/d-modal";
import { disableImplicitInjections } from "discourse/lib/implicit-injections"; import { disableImplicitInjections } from "discourse/lib/implicit-injections";
import deprecated from "discourse-common/lib/deprecated"; import deprecated, {
withSilencedDeprecations,
} from "discourse-common/lib/deprecated";
import I18n from "discourse-i18n"; import I18n from "discourse-i18n";
// Known legacy modals in core. Silence deprecation warnings for these so the messages // Known legacy modals in core. Silence deprecation warnings for these so the messages
@ -177,7 +179,12 @@ export default class ModalServiceWithLegacySupport extends ModalService {
const modalName = `modal/${templateName}`; const modalName = `modal/${templateName}`;
const fullName = opts.admin ? `admin/templates/${modalName}` : modalName; const fullName = opts.admin ? `admin/templates/${modalName}` : modalName;
route.render(fullName, renderArgs);
// Any use of the legacy modal system will trigger Discourse's own deprecation message
// so we can silence Ember's message here.
withSilencedDeprecations("route-render-template", () => {
route.render(fullName, renderArgs);
});
if (opts.panels) { if (opts.panels) {
if (controller.actions.onSelectPanel) { if (controller.actions.onSelectPanel) {
@ -213,9 +220,16 @@ export default class ModalServiceWithLegacySupport extends ModalService {
return; return;
} }
getOwner(this) const applicationRoute = getOwner(this).lookup("route:application");
.lookup("route:application")
.render("hide-modal", { into: "application", outlet: "modalBody" }); // Any use of the legacy modal system will trigger Discourse's own deprecation message
// so we can silence Ember's message here.
withSilencedDeprecations("route-render-template", () => {
applicationRoute.render("hide-modal", {
into: "application",
outlet: "modalBody",
});
});
$(".d-modal.fixed-modal").modal("hide"); $(".d-modal.fixed-modal").modal("hide");
if (controller) { if (controller) {

View File

@ -1,3 +1,4 @@
import { deprecate as emberDeprecate } from "@ember/debug";
import { setupTest } from "ember-qunit"; import { setupTest } from "ember-qunit";
import { module, test } from "qunit"; import { module, test } from "qunit";
import Sinon from "sinon"; import Sinon from "sinon";
@ -216,4 +217,25 @@ module("Unit | Utility | deprecated", function (hooks) {
"counter is incremented outside the silenced function" "counter is incremented outside the silenced function"
); );
}); });
test("can silence Ember deprecations", function (assert) {
withSilencedDeprecations("fake-ember-deprecation", () => {
emberDeprecate("fake ember deprecation message", false, {
id: "fake-ember-deprecation",
for: "not-ember-source",
since: "v0",
until: "v999",
});
});
assert.strictEqual(
this.warnStub.callCount,
0,
"console.warn is not called"
);
assert.strictEqual(
this.counterStub.callCount,
0,
"counter is not incremented"
);
});
}); });