2016-01-12 02:40:23 +00:00
|
|
|
|
2017-11-14 01:21:57 +00:00
|
|
|
let deprecationHandler = null
|
2016-02-16 23:09:35 +00:00
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
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)
|
2018-04-23 19:46:12 +00:00
|
|
|
} else {
|
2018-09-11 18:16:49 +00:00
|
|
|
return console.warn(`(electron) ${message}`)
|
2018-04-23 19:46:12 +00:00
|
|
|
}
|
2018-09-11 18:16:49 +00:00
|
|
|
},
|
|
|
|
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
|
2018-06-27 06:47:01 +00:00
|
|
|
|
2018-05-29 13:58:02 +00:00
|
|
|
warned = true
|
2018-09-11 18:16:49 +00:00
|
|
|
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.`)
|
|
|
|
}
|
2018-05-29 13:58:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
if (!(deprecated in object)) {
|
|
|
|
throw new Error('Cannot deprecate a property on an object which does not have that property')
|
|
|
|
}
|
2018-05-29 13:58:02 +00:00
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
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)) {
|
2018-05-29 13:58:02 +00:00
|
|
|
warn()
|
2018-09-11 18:16:49 +00:00
|
|
|
object[newName] = object[oldName]
|
2018-05-29 13:40:19 +00:00
|
|
|
}
|
2018-09-11 18:16:49 +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
|