Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//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