Merge pull request #13101 from electron/deprecate-property
feat: add deprecate.property()
This commit is contained in:
Коммит
d907f43e6e
|
@ -87,20 +87,33 @@ deprecate.getHandler = () => deprecationHandler
|
|||
// return this[member][method].apply(this[member], arguments)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Deprecate a property.
|
||||
// deprecate.property = (object, property, method) => {
|
||||
// return Object.defineProperty(object, property, {
|
||||
// get: function () {
|
||||
// let warned = false
|
||||
// if (!(warned || process.noDeprecation)) {
|
||||
// warned = true
|
||||
// deprecate.warn(`${property} property`, `${method} method`)
|
||||
// }
|
||||
// return this[method]()
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
//
|
||||
|
||||
// Deprecate the old name of a property
|
||||
deprecate.property = (object, deprecatedName, newName) => {
|
||||
let warned = false
|
||||
let warn = () => {
|
||||
if (!(warned || process.noDeprecation)) {
|
||||
warned = true
|
||||
deprecate.warn(deprecatedName, newName)
|
||||
}
|
||||
}
|
||||
|
||||
if ((typeof object[newName] === 'undefined') &&
|
||||
(typeof object[deprecatedName] !== 'undefined')) {
|
||||
warn()
|
||||
object[newName] = object[deprecatedName]
|
||||
}
|
||||
|
||||
return Object.defineProperty(object, deprecatedName, {
|
||||
get: function () {
|
||||
warn()
|
||||
return this[newName]
|
||||
},
|
||||
set: function (value) {
|
||||
warn()
|
||||
this[newName] = value
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = deprecate
|
||||
|
|
|
@ -49,6 +49,46 @@ describe('deprecations', () => {
|
|||
assert.equal(typeof nativeImage.createFromDataUrl, 'function')
|
||||
})
|
||||
|
||||
it('renames a property', () => {
|
||||
let msg
|
||||
deprecations.setHandler((m) => { msg = m })
|
||||
|
||||
const oldPropertyName = 'dingyOldName'
|
||||
const newPropertyName = 'shinyNewName'
|
||||
|
||||
let value = 0
|
||||
let o = { [newPropertyName]: value }
|
||||
assert.strictEqual(typeof o[oldPropertyName], 'undefined')
|
||||
assert.strictEqual(typeof o[newPropertyName], 'number')
|
||||
|
||||
deprecate.property(o, oldPropertyName, newPropertyName)
|
||||
assert.notEqual(typeof msg, 'string')
|
||||
o[oldPropertyName] = ++value
|
||||
|
||||
assert.strictEqual(typeof msg, 'string')
|
||||
assert.ok(msg.includes(oldPropertyName))
|
||||
assert.ok(msg.includes(newPropertyName))
|
||||
|
||||
assert.strictEqual(o[newPropertyName], value)
|
||||
assert.strictEqual(o[oldPropertyName], value)
|
||||
})
|
||||
|
||||
it('warns if deprecated property is already set', () => {
|
||||
let msg
|
||||
deprecations.setHandler((m) => { msg = m })
|
||||
|
||||
const oldPropertyName = 'dingyOldName'
|
||||
const newPropertyName = 'shinyNewName'
|
||||
const value = 0
|
||||
|
||||
let o = { [oldPropertyName]: value }
|
||||
deprecate.property(o, oldPropertyName, newPropertyName)
|
||||
|
||||
assert.strictEqual(typeof msg, 'string')
|
||||
assert.ok(msg.includes(oldPropertyName))
|
||||
assert.ok(msg.includes(newPropertyName))
|
||||
})
|
||||
|
||||
it('throws an exception if no deprecation handler is specified', () => {
|
||||
assert.throws(() => {
|
||||
deprecate.log('this is deprecated')
|
||||
|
|
Загрузка…
Ссылка в новой задаче