discourse/app/assets/javascripts/discourse-common/addon/lib/deprecated.js
Jarek Radosz 064332ef6e
DEV: Update ember-cli-deprecation-workflow from 2.2.0 to 3.0.1 (#28226)
From dependabot PR:

<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/ember-cli/ember-cli-deprecation-workflow/releases">ember-cli-deprecation-workflow's releases</a>.</em></p>
<blockquote>
<h2>v3.0.1-ember-cli-deprecation-workflow</h2>
<h2>Release (2024-07-11)</h2>
<p>ember-cli-deprecation-workflow 3.0.1 (patch)</p>
<h4>🏠 Internal</h4>
<ul>
<li><code>ember-cli-deprecation-workflow</code>
<ul>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/192">#192</a> fix repository link in package.json (<a href="https://github.com/mansona"><code>@​mansona</code></a>)</li>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/191">#191</a> update release plan workflow (<a href="https://github.com/mansona"><code>@​mansona</code></a>)</li>
</ul>
</li>
</ul>
<h4>Committers: 1</h4>
<ul>
<li>Chris Manson (<a href="https://github.com/mansona"><code>@​mansona</code></a>)</li>
</ul>
<h2>v3.0.0</h2>
<h2>Release (2024-06-25)</h2>
<p>ember-cli-deprecation-workflow 3.0.0 (major)</p>
<h4>💥 Breaking Change</h4>
<ul>
<li><code>ember-cli-deprecation-workflow</code>
<ul>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/159">#159</a> [BREAKING] Convert to a module. Drops support for Ember &lt; 3.28, requires manual initialization (<a href="https://github.com/lolmaus"><code>@​lolmaus</code></a>)</li>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/175">#175</a> Node 16 is the minimum supported version (<a href="https://github.com/mixonic"><code>@​mixonic</code></a>)</li>
</ul>
</li>
</ul>
<h4>🐛 Bug Fix</h4>
<ul>
<li><code>ember-cli-deprecation-workflow</code>
<ul>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/181">#181</a> Remove unused broccoli magic (<a href="https://github.com/simonihmig"><code>@​simonihmig</code></a>)</li>
</ul>
</li>
</ul>
<h4>📝 Documentation</h4>
<ul>
<li><code>ember-cli-deprecation-workflow</code>
<ul>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/184">#184</a> Update configuration paths in documentation (<a href="https://github.com/backspace"><code>@​backspace</code></a>)</li>
</ul>
</li>
</ul>
<h4>🏠 Internal</h4>
<ul>
<li><code>ember-cli-deprecation-workflow</code>
<ul>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/189">#189</a> start using release-plan (<a href="https://github.com/mansona"><code>@​mansona</code></a>)</li>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/188">#188</a> start using pnpm (<a href="https://github.com/mansona"><code>@​mansona</code></a>)</li>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/178">#178</a> Upgrade Ember CLI to 5.4 (<a href="https://github.com/lolmaus"><code>@​lolmaus</code></a>)</li>
<li><a href="https://redirect.github.com/ember-cli/ember-cli-deprecation-workflow/pull/170">#170</a> Bump Node, swap to npm, update CI pipeline (<a href="https://github.com/mixonic"><code>@​mixonic</code></a>)</li>
</ul>
</li>
</ul>
<h4>Committers: 5</h4>
<ul>
<li>Andrey Mikhaylov (lolmaus) (<a href="https://github.com/lolmaus"><code>@​lolmaus</code></a>)</li>
<li>Buck Doyle (<a href="https://github.com/backspace"><code>@​backspace</code></a>)</li>
<li>Chris Manson (<a href="https://github.com/mansona"><code>@​mansona</code></a>)</li>
<li>Matthew Beale (<a href="https://github.com/mixonic"><code>@​mixonic</code></a>)</li>
<li>Simon Ihmig (<a href="https://github.com/simonihmig"><code>@​simonihmig</code></a>)</li>
</ul>
</blockquote>
</details>
2024-08-07 17:59:42 +02:00

131 lines
4.2 KiB
JavaScript

import DEPRECATION_WORKFLOW from "../deprecation-workflow";
const handlers = [];
const disabledDeprecations = new Set();
let emberDeprecationSilencer;
/**
* 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.
* @param {String} msg The deprecation message
* @param {Object} [options] Deprecation options
* @param {String} [options.id] A unique identifier for this deprecation. This should be namespaced by dots (e.g. discourse.my_deprecation)
* @param {String} [options.since] The Discourse version this deprecation was introduced in
* @param {String} [options.dropFrom] The Discourse version this deprecation will be dropped in. Typically one major version after `since`
* @param {String} [options.url] A URL which provides more detail about the deprecation
* @param {boolean} [options.raiseError] Raise an error when this deprecation is triggered. Defaults to `false`
*/
export default function deprecated(msg, options = {}) {
const { id, since, dropFrom, url, raiseError } = options;
if (id && disabledDeprecations.has(id)) {
return;
}
msg = ["Deprecation notice:", msg];
if (since) {
msg.push(`[deprecated since Discourse ${since}]`);
}
if (dropFrom) {
msg.push(`[removal in Discourse ${dropFrom}]`);
}
if (id) {
msg.push(`[deprecation id: ${id}]`);
}
if (url) {
msg.push(`[info: ${url}]`);
}
msg = msg.join(" ");
let consolePrefix = "";
if (require.has("discourse/lib/source-identifier")) {
// This module doesn't exist in pretty-text/wizard/etc.
consolePrefix =
require("discourse/lib/source-identifier").consolePrefix() || "";
}
handlers.forEach((h) => h(msg, options));
const matchedWorkflow = DEPRECATION_WORKFLOW.find((w) => w.matchId === id);
if (
raiseError ||
matchedWorkflow?.handler === "throw" ||
(!matchedWorkflow && globalThis.EmberENV?.RAISE_ON_DEPRECATION)
) {
throw msg;
}
if (matchedWorkflow?.handler !== "silence") {
console.warn(...[consolePrefix, msg].filter(Boolean)); //eslint-disable-line no-console
}
}
/**
* Register a function which will be called whenever a deprecation is triggered
* @param {function} callback The callback function. Arguments will match those of `deprecated()`.
*/
export function registerDeprecationHandler(callback) {
handlers.push(callback);
}
/**
* Silence one or more deprecations while running `callback`
* @param {(string|string[])} deprecationIds A single id, or an array of ids, of deprecations to silence
* @param {function} callback The function to call while deprecations are silenced.
*/
export function withSilencedDeprecations(deprecationIds, callback) {
ensureEmberDeprecationSilencer();
const idArray = [].concat(deprecationIds);
try {
idArray.forEach((id) => disabledDeprecations.add(id));
const result = callback();
if (result instanceof Promise) {
throw new Error(
"withSilencedDeprecations callback returned a promise. Use withSilencedDeprecationsAsync instead."
);
}
return result;
} finally {
idArray.forEach((id) => disabledDeprecations.delete(id));
}
}
/**
* Silence one or more deprecations while running an async `callback`
* @async
* @param {(string|string[])} deprecationIds A single id, or an array of ids, of deprecations to silence
* @param {function} callback The asynchronous function to call while deprecations are silenced.
*/
export async function withSilencedDeprecationsAsync(deprecationIds, callback) {
ensureEmberDeprecationSilencer();
const idArray = [].concat(deprecationIds);
try {
idArray.forEach((id) => disabledDeprecations.add(id));
return await callback();
} finally {
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
);
}
}