Merge pull request #11117 from electron/add_deprecations_spec

Add to deprecations spec
This commit is contained in:
Charles Kerr 2017-11-20 09:07:06 -06:00 committed by GitHub
commit 4c04f1c7ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 69 deletions

View file

@ -1,7 +1,6 @@
// Deprecate a method. // Deprecate a method.
const deprecate = function (oldName, newName, fn) { const deprecate = function (oldName, newName, fn) {
var warned let warned = false
warned = false
return function () { return function () {
if (!(warned || process.noDeprecation)) { if (!(warned || process.noDeprecation)) {
warned = true warned = true
@ -11,80 +10,31 @@ const deprecate = function (oldName, newName, fn) {
} }
} }
// The method is renamed. // The method is aliases and the old method is retained for backwards compat
deprecate.rename = function (object, oldName, newName) { deprecate.alias = function (object, deprecatedName, existingName) {
var newMethod, warned let warned = false
warned = false const newMethod = function () {
newMethod = function () {
if (!(warned || process.noDeprecation)) { if (!(warned || process.noDeprecation)) {
warned = true warned = true
deprecate.warn(oldName, newName) deprecate.warn(deprecatedName, existingName)
} }
return this[newName].apply(this, arguments) return this[existingName].apply(this, arguments)
} }
if (typeof object === 'function') { if (typeof object === 'function') {
object.prototype[oldName] = newMethod object.prototype[deprecatedName] = newMethod
} else { } else {
object[oldName] = newMethod object[deprecatedName] = newMethod
} }
} }
// Forward the method to member. deprecate.warn = (oldName, newName) => {
deprecate.member = function (object, method, member) { return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
var warned
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 a property. let deprecationHandler = null
deprecate.property = function (object, property, method) {
return Object.defineProperty(object, property, {
get: function () {
var warned
warned = false
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(property + ' property', method + ' method')
}
return this[method]()
}
})
}
// Deprecate an event.
deprecate.event = function (emitter, oldName, newName, fn) {
var warned = false
return emitter.on(newName, function (...args) {
// there is listeners for old API.
if (this.listenerCount(oldName) > 0) {
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn("'" + oldName + "' event", "'" + newName + "' event")
}
if (fn != null) {
fn.apply(this, arguments)
} else {
this.emit.apply(this, [oldName].concat(args))
}
}
})
}
// Print deprecation warning.
deprecate.warn = function (oldName, newName) {
return deprecate.log(oldName + ' is deprecated. Use ' + newName + ' instead.')
}
var deprecationHandler = null
// Print deprecation message. // Print deprecation message.
deprecate.log = function (message) { deprecate.log = (message) => {
if (typeof deprecationHandler === 'function') { if (typeof deprecationHandler === 'function') {
deprecationHandler(message) deprecationHandler(message)
} else if (process.throwDeprecation) { } else if (process.throwDeprecation) {
@ -92,16 +42,61 @@ deprecate.log = function (message) {
} else if (process.traceDeprecation) { } else if (process.traceDeprecation) {
return console.trace(message) return console.trace(message)
} else { } else {
return console.warn('(electron) ' + message) return console.warn(`(electron) ${message}`)
} }
} }
deprecate.setHandler = function (handler) { deprecate.setHandler = (handler) => {
deprecationHandler = handler deprecationHandler = handler
} }
deprecate.getHandler = function () { deprecate.getHandler = () => deprecationHandler
return 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 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 an event.
// deprecate.event = (emitter, oldName, newName, fn) => {
// let warned = false
// return emitter.on(newName, function (...args) {
// if (this.listenerCount(oldName) > 0) {
// if (!(warned || process.noDeprecation)) {
// warned = true
// deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
// }
// if (fn != null) {
// fn.apply(this, arguments)
// } else {
// this.emit.apply(this, [oldName].concat(args))
// }
// }
// })
// }
module.exports = deprecate module.exports = deprecate

View file

@ -1,5 +1,5 @@
const assert = require('assert') const assert = require('assert')
const {deprecations, deprecate} = require('electron') const {deprecations, deprecate, nativeImage} = require('electron')
describe('deprecations', () => { describe('deprecations', () => {
beforeEach(() => { beforeEach(() => {
@ -18,6 +18,37 @@ describe('deprecations', () => {
assert.deepEqual(messages, ['this is deprecated']) assert.deepEqual(messages, ['this is deprecated'])
}) })
it('returns a deprecation handler after one is set', () => {
const messages = []
deprecations.setHandler((message) => {
messages.push(message)
})
deprecate.log('this is deprecated')
assert(typeof deprecations.getHandler() === 'function')
})
it('returns a deprecation warning', () => {
const messages = []
deprecations.setHandler((message) => {
messages.push(message)
})
deprecate.warn('old', 'new')
assert.deepEqual(messages, [`'old' is deprecated. Use 'new' instead.`])
})
it('renames a method', () => {
assert.equal(typeof nativeImage.createFromDataUrl, 'undefined')
assert.equal(typeof nativeImage.createFromDataURL, 'function')
deprecate.alias(nativeImage, 'createFromDataUrl', 'createFromDataURL')
assert.equal(typeof nativeImage.createFromDataUrl, 'function')
})
it('throws an exception if no deprecation handler is specified', () => { it('throws an exception if no deprecation handler is specified', () => {
assert.throws(() => { assert.throws(() => {
deprecate.log('this is deprecated') deprecate.log('this is deprecated')