electron/lib/common/api/deprecate.js

120 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-01-12 02:40:23 +00:00
2017-11-14 01:21:57 +00:00
let deprecationHandler = null
const deprecate = {
setHandler: (handler) => { deprecationHandler = handler },
getHandler: () => deprecationHandler,
warn: (oldName, newName) => {
return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
},
log: (message) => {
if (typeof deprecationHandler === 'function') {
deprecationHandler(message)
} else if (process.throwDeprecation) {
throw new Error(message)
} else if (process.traceDeprecation) {
return console.trace(message)
} else {
return console.warn(`(electron) ${message}`)
}
},
renameFunction: function (fn, oldName, newName) {
let warned = false
return function () {
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(oldName, newName)
}
return fn.apply(this, arguments)
}
},
removeFunction: (oldName) => {
if (!process.noDeprecation) {
deprecate.log(`The '${oldName}' function has been deprecated and marked for removal.`)
}
},
alias: function (object, oldName, newName) {
let warned = false
const newFn = function () {
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(oldName, newName)
}
return this[newName].apply(this, arguments)
}
if (typeof object === 'function') {
object.prototype[newName] = newFn
} else {
object[oldName] = newFn
}
},
event: (emitter, oldName, newName) => {
let warned = false
return emitter.on(newName, function (...args) {
if (this.listenerCount(oldName) === 0) return
if (warned || process.noDeprecation) return
warned = true
if (newName.startsWith('-')) {
deprecate.log(`'${oldName}' event has been deprecated.`)
} else {
deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
}
this.emit(oldName, ...args)
})
},
removeProperty: (object, deprecated) => {
let warned = false
let warn = () => {
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.log(`The '${deprecated}' property has been deprecated and marked for removal.`)
}
}
if (!(deprecated in object)) {
throw new Error('Cannot deprecate a property on an object which does not have that property')
}
let temp = object[deprecated]
return Object.defineProperty(object, deprecated, {
configurable: true,
get: () => {
warn()
return temp
},
set: (newValue) => {
warn()
temp = newValue
}
})
},
renameProperty: (object, oldName, newName) => {
let warned = false
let warn = () => {
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(oldName, newName)
}
}
if (!(newName in object) && (oldName in object)) {
warn()
object[newName] = object[oldName]
2018-05-29 13:40:19 +00:00
}
return Object.defineProperty(object, oldName, {
get: function () {
warn()
return this[newName]
},
set: function (value) {
warn()
this[newName] = value
}
})
}
2018-05-29 13:40:19 +00:00
}
2017-11-20 14:12:34 +00:00
2016-03-25 19:50:33 +00:00
module.exports = deprecate