UNPKG

755 BJavaScriptView Raw
1/**
2 * Method property decorator factory. Augments original method with
3 * deprecation message (via console), shown when method is invoked.
4 * Accepts optional message arg. Throws error if assigned property
5 * is not a function.
6 *
7 * @param msg - deprecation message
8 */
9export const deprecated = (msg, log = console.log) => function (target, prop, descriptor) {
10 const signature = `${target.constructor.name}#${prop.toString()}`;
11 const fn = descriptor.value;
12 if (typeof fn !== "function") {
13 throw new Error(`${signature} is not a function`);
14 }
15 descriptor.value = function () {
16 log(`DEPRECATED ${signature}: ${msg || "will be removed soon"}`);
17 return fn.apply(this, arguments);
18 };
19 return descriptor;
20};