feat: warn if deprecated property is already set

This commit is contained in:
Charles Kerr 2018-05-29 15:58:02 +02:00
Родитель 61fac1bbc1
Коммит 2275625e1a
2 изменённых файлов: 32 добавлений и 10 удалений

Просмотреть файл

@ -90,21 +90,27 @@ deprecate.getHandler = () => deprecationHandler
// 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 () {
let warned = false
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(deprecatedName, newName)
}
warn()
return this[newName]
},
set: function (value) {
let warned = false
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(deprecatedName, newName)
}
warn()
this[newName] = value
}
})

Просмотреть файл

@ -73,6 +73,22 @@ describe('deprecations', () => {
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')