deprecate.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Deprecate a method.
  2. const deprecate = function (oldName, newName, fn) {
  3. let warned = false
  4. return function () {
  5. if (!(warned || process.noDeprecation)) {
  6. warned = true
  7. deprecate.warn(oldName, newName)
  8. }
  9. return fn.apply(this, arguments)
  10. }
  11. }
  12. // The method is aliases and the old method is retained for backwards compat
  13. deprecate.alias = function (object, deprecatedName, existingName) {
  14. let warned = false
  15. const newMethod = function () {
  16. if (!(warned || process.noDeprecation)) {
  17. warned = true
  18. deprecate.warn(deprecatedName, existingName)
  19. }
  20. return this[existingName].apply(this, arguments)
  21. }
  22. if (typeof object === 'function') {
  23. object.prototype[deprecatedName] = newMethod
  24. } else {
  25. object[deprecatedName] = newMethod
  26. }
  27. }
  28. deprecate.warn = (oldName, newName) => {
  29. return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
  30. }
  31. let deprecationHandler = null
  32. // Print deprecation message.
  33. deprecate.log = (message) => {
  34. if (typeof deprecationHandler === 'function') {
  35. deprecationHandler(message)
  36. } else if (process.throwDeprecation) {
  37. throw new Error(message)
  38. } else if (process.traceDeprecation) {
  39. return console.trace(message)
  40. } else {
  41. return console.warn(`(electron) ${message}`)
  42. }
  43. }
  44. // Deprecate an event.
  45. deprecate.event = (emitter, oldName, newName) => {
  46. let warned = false
  47. return emitter.on(newName, function (...args) {
  48. // There are no listeners for this event
  49. if (this.listenerCount(oldName) === 0) { return }
  50. // noDeprecation set or if user has already been warned
  51. if (warned || process.noDeprecation) { return }
  52. warned = true
  53. const isInternalEvent = newName.startsWith('-')
  54. if (isInternalEvent) {
  55. // The event cannot be use anymore. Log that.
  56. deprecate.log(`'${oldName}' event has been deprecated.`)
  57. } else {
  58. // The event has a new name now. Warn with that.
  59. deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
  60. }
  61. this.emit(oldName, ...args)
  62. })
  63. }
  64. deprecate.setHandler = (handler) => {
  65. deprecationHandler = handler
  66. }
  67. deprecate.getHandler = () => deprecationHandler
  68. // None of the below methods are used, and so will be commented
  69. // out until such time that they are needed to be used and tested.
  70. // // Forward the method to member.
  71. // deprecate.member = (object, method, member) => {
  72. // let warned = false
  73. // object.prototype[method] = function () {
  74. // if (!(warned || process.noDeprecation)) {
  75. // warned = true
  76. // deprecate.warn(method, `${member}.${method}`)
  77. // }
  78. // return this[member][method].apply(this[member], arguments)
  79. // }
  80. // }
  81. // Deprecate the old name of a property
  82. deprecate.property = (object, deprecatedName, newName) => {
  83. let warned = false
  84. let warn = () => {
  85. if (!(warned || process.noDeprecation)) {
  86. warned = true
  87. deprecate.warn(deprecatedName, newName)
  88. }
  89. }
  90. if ((typeof object[newName] === 'undefined') &&
  91. (typeof object[deprecatedName] !== 'undefined')) {
  92. warn()
  93. object[newName] = object[deprecatedName]
  94. }
  95. return Object.defineProperty(object, deprecatedName, {
  96. get: function () {
  97. warn()
  98. return this[newName]
  99. },
  100. set: function (value) {
  101. warn()
  102. this[newName] = value
  103. }
  104. })
  105. }
  106. module.exports = deprecate