deprecate.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.setHandler = (handler) => {
  45. deprecationHandler = handler
  46. }
  47. deprecate.getHandler = () => deprecationHandler
  48. // None of the below methods are used, and so will be commented
  49. // out until such time that they are needed to be used and tested.
  50. // // Forward the method to member.
  51. // deprecate.member = (object, method, member) => {
  52. // let warned = false
  53. // object.prototype[method] = function () {
  54. // if (!(warned || process.noDeprecation)) {
  55. // warned = true
  56. // deprecate.warn(method, `${member}.${method}`)
  57. // }
  58. // return this[member][method].apply(this[member], arguments)
  59. // }
  60. // }
  61. //
  62. // // Deprecate a property.
  63. // deprecate.property = (object, property, method) => {
  64. // return Object.defineProperty(object, property, {
  65. // get: function () {
  66. // let warned = false
  67. // if (!(warned || process.noDeprecation)) {
  68. // warned = true
  69. // deprecate.warn(`${property} property`, `${method} method`)
  70. // }
  71. // return this[method]()
  72. // }
  73. // })
  74. // }
  75. //
  76. // // Deprecate an event.
  77. // deprecate.event = (emitter, oldName, newName, fn) => {
  78. // let warned = false
  79. // return emitter.on(newName, function (...args) {
  80. // if (this.listenerCount(oldName) > 0) {
  81. // if (!(warned || process.noDeprecation)) {
  82. // warned = true
  83. // deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
  84. // }
  85. // if (fn != null) {
  86. // fn.apply(this, arguments)
  87. // } else {
  88. // this.emit.apply(this, [oldName].concat(args))
  89. // }
  90. // }
  91. // })
  92. // }
  93. module.exports = deprecate