Last active
January 30, 2018 20:27
-
-
Save robwormald/9379194b191700b6564f8e8e2b077b95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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