refactor: deprecation handling apis (#14615)

This commit is contained in:
Shelley Vohr 2018-09-13 21:39:00 -07:00 committed by GitHub
parent 7e5815c180
commit 89148bcf8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 161 deletions

View file

@ -60,9 +60,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 metric of metrics) {
return nativeFn.call(app) deprecate.removeProperty(metric, 'memory', true)
if ('memory' in metric) {
deprecate.removeProperty(metric, 'memory')
}
}
} }
app.isPackaged = (() => { app.isPackaged = (() => {

View file

@ -1,125 +1,95 @@
// Deprecate a method. 'use strict'
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. function warnOnce (oldName, newName) {
deprecate.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}`)
}
}
// Deprecate an event.
deprecate.event = (emitter, oldName, newName) => {
let warned = false let warned = false
return emitter.on(newName, function (...args) { const msg = newName
// There are no listeners for this event ? `'${oldName}' is deprecated and will be removed. Please use '${newName}' instead.`
if (this.listenerCount(oldName) === 0) { return } : `'${oldName}' is deprecated and will be removed.`
// noDeprecation set or if user has already been warned return () => {
if (warned || process.noDeprecation) { return } if (!warned && !process.noDeprecation) {
warned = true
const isInternalEvent = newName.startsWith('-')
if (isInternalEvent) {
// The event cannot be use anymore. Log that.
deprecate.log(`'${oldName}' event has been deprecated.`)
} else {
// The event has a new name now. Warn with that.
deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
}
this.emit(oldName, ...args)
})
}
deprecate.setHandler = (handler) => {
deprecationHandler = handler
}
deprecate.getHandler = () => deprecationHandler
// None of the below methods are used, and so will be commented
// out until such time that they are needed to be used and tested.
// // 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)
// }
// }
deprecate.removeProperty = (object, deprecatedName) => {
if (!process.noDeprecation) {
deprecate.log(`The '${deprecatedName}' property has been deprecated.`)
}
}
// Replace the old name of a property
deprecate.renameProperty = (object, deprecatedName, newName) => {
let warned = false
let warn = () => {
if (!(warned || process.noDeprecation)) {
warned = true warned = true
deprecate.warn(deprecatedName, newName) deprecate.log(msg)
} }
} }
}
if ((typeof object[newName] === 'undefined') && const deprecate = {
(typeof object[deprecatedName] !== 'undefined')) { setHandler: (handler) => { deprecationHandler = handler },
warn() getHandler: () => deprecationHandler,
object[newName] = object[deprecatedName] warn: (oldName, newName) => {
} return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
},
return Object.defineProperty(object, deprecatedName, { log: (message) => {
get: function () { if (typeof deprecationHandler === 'function') {
warn() deprecationHandler(message)
return this[newName] } else if (process.throwDeprecation) {
}, throw new Error(message)
set: function (value) { } else if (process.traceDeprecation) {
warn() return console.trace(message)
this[newName] = value } else {
return console.warn(`(electron) ${message}`)
} }
}) },
event: (emitter, oldName, newName) => {
const warn = newName.startsWith('-') /* internal event */
? warnOnce(`${oldName} event`)
: warnOnce(`${oldName} event`, `${newName} event`)
return emitter.on(newName, function (...args) {
if (this.listenerCount(oldName) !== 0) {
warn()
this.emit(oldName, ...args)
}
})
},
removeProperty: (o, removedName) => {
// if the property's already been removed, warn about it
if (!(removedName in o)) {
deprecate.log(`Unable to remove property '${removedName}' from an object that lacks it.`)
}
// wrap the deprecated property in an accessor to warn
const warn = warnOnce(removedName)
let val = o[removedName]
return Object.defineProperty(o, removedName, {
configurable: true,
get: () => {
warn()
return val
},
set: newVal => {
warn()
val = newVal
}
})
},
renameProperty: (o, oldName, newName) => {
const warn = warnOnce(oldName, newName)
// if the new property isn't there yet,
// inject it and warn about it
if ((oldName in o) && !(newName in o)) {
warn()
o[newName] = o[oldName]
}
// wrap the deprecated property in an accessor to warn
// and redirect to the new property
return Object.defineProperty(o, oldName, {
get: () => {
warn()
return o[newName]
},
set: value => {
warn()
o[newName] = value
}
})
}
} }
module.exports = deprecate module.exports = deprecate

View file

@ -1,6 +1,8 @@
'use strict'
const chai = require('chai') const chai = require('chai')
const dirtyChai = require('dirty-chai') const dirtyChai = require('dirty-chai')
const {deprecations, deprecate, nativeImage} = require('electron') const {deprecations, deprecate} = require('electron')
const {expect} = chai const {expect} = chai
chai.use(dirtyChai) chai.use(dirtyChai)
@ -33,76 +35,81 @@ describe('deprecations', () => {
expect(deprecations.getHandler()).to.be.a('function') expect(deprecations.getHandler()).to.be.a('function')
}) })
it('returns a deprecation warning', () => {
const messages = []
deprecations.setHandler(message => {
messages.push(message)
})
deprecate.warn('old', 'new')
expect(messages).to.deep.equal([`'old' is deprecated. Use 'new' instead.`])
})
it('renames a method', () => {
expect(nativeImage.createFromDataUrl).to.be.undefined()
expect(nativeImage.createFromDataURL).to.be.a('function')
deprecate.alias(nativeImage, 'createFromDataUrl', 'createFromDataURL')
expect(nativeImage.createFromDataUrl).to.be.a('function')
})
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, 'iDoNotExist')
}).to.throw(/iDoNotExist/)
}) })
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 only once per item', () => {
const messages = []
deprecations.setHandler(message => { messages.push(message) })
const key = 'foo'
const val = 'bar'
let o = {[key]: val}
deprecate.removeProperty(o, key)
for (let i = 0; i < 3; ++i) {
expect(o[key]).to.equal(val)
expect(messages).to.have.length(1)
}
}) })
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', () => {