chore: refactor deprecation apis (#14495)
This commit is contained in:
parent
3a6e88c0e7
commit
2157d09956
3 changed files with 148 additions and 146 deletions
|
@ -42,9 +42,13 @@ Object.assign(app, {
|
||||||
|
|
||||||
const nativeFn = app.getAppMetrics
|
const nativeFn = app.getAppMetrics
|
||||||
app.getAppMetrics = () => {
|
app.getAppMetrics = () => {
|
||||||
deprecate.removeProperty(nativeFn, 'privateBytes')
|
let metrics = nativeFn.call(app)
|
||||||
deprecate.removeProperty(nativeFn, 'sharedBytes')
|
for (const {memory} of metrics) {
|
||||||
return nativeFn.call(app)
|
deprecate.removeProperty(memory, 'privateBytes')
|
||||||
|
deprecate.removeProperty(memory, 'sharedBytes')
|
||||||
|
}
|
||||||
|
|
||||||
|
return metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
app.isPackaged = (() => {
|
app.isPackaged = (() => {
|
||||||
|
|
|
@ -1,40 +1,13 @@
|
||||||
// Deprecate a method.
|
|
||||||
const deprecate = function (oldName, newName, fn) {
|
|
||||||
let warned = false
|
|
||||||
return function () {
|
|
||||||
if (!(warned || process.noDeprecation)) {
|
|
||||||
warned = true
|
|
||||||
deprecate.warn(oldName, newName)
|
|
||||||
}
|
|
||||||
return fn.apply(this, arguments)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The method is aliases and the old method is retained for backwards compat
|
|
||||||
deprecate.alias = function (object, deprecatedName, existingName) {
|
|
||||||
let warned = false
|
|
||||||
const newMethod = function () {
|
|
||||||
if (!(warned || process.noDeprecation)) {
|
|
||||||
warned = true
|
|
||||||
deprecate.warn(deprecatedName, existingName)
|
|
||||||
}
|
|
||||||
return this[existingName].apply(this, arguments)
|
|
||||||
}
|
|
||||||
if (typeof object === 'function') {
|
|
||||||
object.prototype[deprecatedName] = newMethod
|
|
||||||
} else {
|
|
||||||
object[deprecatedName] = newMethod
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deprecate.warn = (oldName, newName) => {
|
|
||||||
return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
|
|
||||||
}
|
|
||||||
|
|
||||||
let deprecationHandler = null
|
let deprecationHandler = null
|
||||||
|
|
||||||
// Print deprecation message.
|
const deprecate = {
|
||||||
deprecate.log = (message) => {
|
setHandler: (handler) => { deprecationHandler = handler },
|
||||||
|
getHandler: () => deprecationHandler,
|
||||||
|
warn: (oldName, newName) => {
|
||||||
|
return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
|
||||||
|
},
|
||||||
|
log: (message) => {
|
||||||
if (typeof deprecationHandler === 'function') {
|
if (typeof deprecationHandler === 'function') {
|
||||||
deprecationHandler(message)
|
deprecationHandler(message)
|
||||||
} else if (process.throwDeprecation) {
|
} else if (process.throwDeprecation) {
|
||||||
|
@ -44,79 +17,93 @@ deprecate.log = (message) => {
|
||||||
} else {
|
} else {
|
||||||
return console.warn(`(electron) ${message}`)
|
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)
|
||||||
// Deprecate an event.
|
}
|
||||||
deprecate.event = (emitter, oldName, newName) => {
|
},
|
||||||
|
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
|
let warned = false
|
||||||
return emitter.on(newName, function (...args) {
|
return emitter.on(newName, function (...args) {
|
||||||
// There are no listeners for this event
|
if (this.listenerCount(oldName) === 0) return
|
||||||
if (this.listenerCount(oldName) === 0) { return }
|
if (warned || process.noDeprecation) return
|
||||||
// noDeprecation set or if user has already been warned
|
|
||||||
if (warned || process.noDeprecation) { return }
|
|
||||||
warned = true
|
warned = true
|
||||||
const isInternalEvent = newName.startsWith('-')
|
if (newName.startsWith('-')) {
|
||||||
if (isInternalEvent) {
|
|
||||||
// The event cannot be use anymore. Log that.
|
|
||||||
deprecate.log(`'${oldName}' event has been deprecated.`)
|
deprecate.log(`'${oldName}' event has been deprecated.`)
|
||||||
} else {
|
} else {
|
||||||
// The event has a new name now. Warn with that.
|
|
||||||
deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
|
deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
|
||||||
}
|
}
|
||||||
this.emit(oldName, ...args)
|
this.emit(oldName, ...args)
|
||||||
})
|
})
|
||||||
}
|
},
|
||||||
|
removeProperty: (object, deprecated) => {
|
||||||
deprecate.setHandler = (handler) => {
|
|
||||||
deprecationHandler = handler
|
|
||||||
}
|
|
||||||
|
|
||||||
deprecate.getHandler = () => deprecationHandler
|
|
||||||
|
|
||||||
// Commented out until such time as it is needed
|
|
||||||
// // Forward the method to member.
|
|
||||||
// deprecate.member = (object, method, member) => {
|
|
||||||
// let warned = false
|
|
||||||
// object.prototype[method] = function () {
|
|
||||||
// if (!(warned || process.noDeprecation)) {
|
|
||||||
// warned = true
|
|
||||||
// deprecate.warn(method, `${member}.${method}`)
|
|
||||||
// }
|
|
||||||
// return this[member][method].apply(this[member], arguments)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Remove a property with no replacement
|
|
||||||
deprecate.removeProperty = (object, deprecatedName) => {
|
|
||||||
if (!process.noDeprecation) {
|
|
||||||
deprecate.log(`The '${deprecatedName}' property of ${object} has been deprecated and marked for removal.`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove a function with no replacement
|
|
||||||
deprecate.removeFunction = (deprecatedName) => {
|
|
||||||
if (!process.noDeprecation) {
|
|
||||||
deprecate.log(`The '${deprecatedName}' function has been deprecated and marked for removal.`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace the old name of a property
|
|
||||||
deprecate.renameProperty = (object, deprecatedName, newName) => {
|
|
||||||
let warned = false
|
let warned = false
|
||||||
let warn = () => {
|
let warn = () => {
|
||||||
if (!(warned || process.noDeprecation)) {
|
if (!(warned || process.noDeprecation)) {
|
||||||
warned = true
|
warned = true
|
||||||
deprecate.warn(deprecatedName, newName)
|
deprecate.log(`The '${deprecated}' property has been deprecated and marked for removal.`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((typeof object[newName] === 'undefined') &&
|
if (!(deprecated in object)) {
|
||||||
(typeof object[deprecatedName] !== 'undefined')) {
|
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()
|
warn()
|
||||||
object[newName] = object[deprecatedName]
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.defineProperty(object, deprecatedName, {
|
if (!(newName in object) && (oldName in object)) {
|
||||||
|
warn()
|
||||||
|
object[newName] = object[oldName]
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.defineProperty(object, oldName, {
|
||||||
get: function () {
|
get: function () {
|
||||||
warn()
|
warn()
|
||||||
return this[newName]
|
return this[newName]
|
||||||
|
@ -127,5 +114,6 @@ deprecate.renameProperty = (object, deprecatedName, newName) => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = deprecate
|
module.exports = deprecate
|
||||||
|
|
|
@ -5,7 +5,7 @@ const {deprecations, deprecate, nativeImage} = require('electron')
|
||||||
const {expect} = chai
|
const {expect} = chai
|
||||||
chai.use(dirtyChai)
|
chai.use(dirtyChai)
|
||||||
|
|
||||||
describe('deprecations', () => {
|
describe.only('deprecations', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
deprecations.setHandler(null)
|
deprecations.setHandler(null)
|
||||||
process.throwDeprecation = true
|
process.throwDeprecation = true
|
||||||
|
@ -55,54 +55,64 @@ describe('deprecations', () => {
|
||||||
|
|
||||||
it('renames a property', () => {
|
it('renames a property', () => {
|
||||||
let msg
|
let msg
|
||||||
deprecations.setHandler((m) => { msg = m })
|
deprecations.setHandler(m => { msg = m })
|
||||||
|
|
||||||
const oldPropertyName = 'dingyOldName'
|
const oldProp = 'dingyOldName'
|
||||||
const newPropertyName = 'shinyNewName'
|
const newProp = 'shinyNewName'
|
||||||
|
|
||||||
let value = 0
|
let value = 0
|
||||||
let o = { [newPropertyName]: value }
|
const o = {[newProp]: value}
|
||||||
expect(o).to.not.have.a.property(oldPropertyName)
|
expect(o).to.not.have.a.property(oldProp)
|
||||||
expect(o).to.have.a.property(newPropertyName).that.is.a('number')
|
expect(o).to.have.a.property(newProp).that.is.a('number')
|
||||||
|
|
||||||
deprecate.renameProperty(o, oldPropertyName, newPropertyName)
|
deprecate.renameProperty(o, oldProp, newProp)
|
||||||
o[oldPropertyName] = ++value
|
o[oldProp] = ++value
|
||||||
|
|
||||||
expect(msg).to.be.a('string')
|
expect(msg).to.be.a('string')
|
||||||
expect(msg).to.include(oldPropertyName)
|
expect(msg).to.include(oldProp)
|
||||||
expect(msg).to.include(newPropertyName)
|
expect(msg).to.include(newProp)
|
||||||
|
|
||||||
expect(o).to.have.a.property(newPropertyName).that.is.equal(value)
|
expect(o).to.have.a.property(newProp).that.is.equal(value)
|
||||||
expect(o).to.have.a.property(oldPropertyName).that.is.equal(value)
|
expect(o).to.have.a.property(oldProp).that.is.equal(value)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('doesn\'t deprecate a property not on an object', () => {
|
||||||
|
const o = {}
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
deprecate.removeProperty(o, 'iDontExist')
|
||||||
|
}).to.throw(/Cannot deprecate a property on an object which does not have that property/)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('deprecates a property of an object', () => {
|
it('deprecates a property of an object', () => {
|
||||||
let msg
|
let msg
|
||||||
deprecations.setHandler(m => { msg = m })
|
deprecations.setHandler(m => { msg = m })
|
||||||
|
|
||||||
const propertyName = 'itMustGo'
|
const prop = 'itMustGo'
|
||||||
const o = { [propertyName]: 0 }
|
let o = {[prop]: 0}
|
||||||
|
|
||||||
deprecate.removeProperty(o, propertyName)
|
deprecate.removeProperty(o, prop)
|
||||||
|
|
||||||
|
const temp = o[prop]
|
||||||
|
|
||||||
|
expect(temp).to.equal(0)
|
||||||
expect(msg).to.be.a('string')
|
expect(msg).to.be.a('string')
|
||||||
expect(msg).to.include(propertyName)
|
expect(msg).to.include(prop)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('warns if deprecated property is already set', () => {
|
it('warns if deprecated property is already set', () => {
|
||||||
let msg
|
let msg
|
||||||
deprecations.setHandler((m) => { msg = m })
|
deprecations.setHandler(m => { msg = m })
|
||||||
|
|
||||||
const oldPropertyName = 'dingyOldName'
|
const oldProp = 'dingyOldName'
|
||||||
const newPropertyName = 'shinyNewName'
|
const newProp = 'shinyNewName'
|
||||||
const value = 0
|
|
||||||
|
|
||||||
let o = { [oldPropertyName]: value }
|
let o = {[oldProp]: 0}
|
||||||
deprecate.renameProperty(o, oldPropertyName, newPropertyName)
|
deprecate.renameProperty(o, oldProp, newProp)
|
||||||
|
|
||||||
expect(msg).to.be.a('string')
|
expect(msg).to.be.a('string')
|
||||||
expect(msg).to.include(oldPropertyName)
|
expect(msg).to.include(oldProp)
|
||||||
expect(msg).to.include(newPropertyName)
|
expect(msg).to.include(newProp)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws an exception if no deprecation handler is specified', () => {
|
it('throws an exception if no deprecation handler is specified', () => {
|
||||||
|
|
Loading…
Reference in a new issue