Skip to content

Instantly share code, notes, and snippets.

@tytskyi
Forked from robwormald/deprecate.ts
Created January 30, 2018 20:27
Show Gist options
  • Save tytskyi/3888d8e135b64f164ec814ab50731b83 to your computer and use it in GitHub Desktop.
Save tytskyi/3888d8e135b64f164ec814ab50731b83 to your computer and use it in GitHub Desktop.
//copied from https://github.com/jayphelps/core-decorators
const { defineProperty, getOwnPropertyDescriptor,
getOwnPropertyNames, getOwnPropertySymbols } = Object;
export function isDescriptor(desc) {
if (!desc || !desc.hasOwnProperty) {
return false;
}
const keys = ['value', 'initializer', 'get', 'set'];
for (let i = 0, l = keys.length; i < l; i++) {
if (desc.hasOwnProperty(keys[i])) {
return true;
}
}
return false;
}
export function decorate(handleDescriptor, entryArgs) {
if (isDescriptor(entryArgs[entryArgs.length - 1])) {
return handleDescriptor(...entryArgs, []);
} else {
return function () {
return handleDescriptor(...Array.prototype.slice.call(arguments), entryArgs);
};
}
}
const DEFAULT_MSG = 'This function will be removed in future versions.';
function handleDescriptor(target, key, descriptor, [msg = DEFAULT_MSG, options = {url: undefined}]) {
if (typeof descriptor.value !== 'function') {
throw new SyntaxError('Only functions can be marked as deprecated');
}
const methodSignature = `${target.constructor.name}#${key}`;
if (options.url) {
msg += `\n\n See ${options.url} for more details.\n\n`;
}
return {
...descriptor,
value: function deprecationWrapper() {
console.log(`DEPRECATION ${methodSignature}: ${msg}`);
return descriptor.value.apply(this, arguments);
}
};
}
export function deprecate(...args) {
return decorate(handleDescriptor, args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment