first pass at deprecation spec updates

This commit is contained in:
Shelley Vohr 2017-11-13 20:21:57 -05:00
parent 4d364fa27a
commit d05a1f8053
No known key found for this signature in database
GPG key ID: F13993A75599653C
2 changed files with 62 additions and 29 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
@ -13,9 +12,8 @@ const deprecate = function (oldName, newName, fn) {
// The method is renamed. // The method is renamed.
deprecate.rename = function (object, oldName, newName) { deprecate.rename = function (object, oldName, newName) {
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(oldName, newName)
@ -30,27 +28,25 @@ deprecate.rename = function (object, oldName, newName) {
} }
// Forward the method to member. // Forward the method to member.
deprecate.member = function (object, method, member) { deprecate.member = (object, method, member) => {
var warned let warned = false
warned = false
object.prototype[method] = function () { object.prototype[method] = function () {
if (!(warned || process.noDeprecation)) { if (!(warned || process.noDeprecation)) {
warned = true warned = true
deprecate.warn(method, member + '.' + method) deprecate.warn(method, `${member}.${method}`)
} }
return this[member][method].apply(this[member], arguments) return this[member][method].apply(this[member], arguments)
} }
} }
// Deprecate a property. // Deprecate a property.
deprecate.property = function (object, property, method) { deprecate.property = (object, property, method) => {
return Object.defineProperty(object, property, { return Object.defineProperty(object, property, {
get: function () { get: function () {
var warned let warned = false
warned = false
if (!(warned || process.noDeprecation)) { if (!(warned || process.noDeprecation)) {
warned = true warned = true
deprecate.warn(property + ' property', method + ' method') deprecate.warn(`${property} property`, `${method} method`)
} }
return this[method]() return this[method]()
} }
@ -58,14 +54,13 @@ deprecate.property = function (object, property, method) {
} }
// Deprecate an event. // Deprecate an event.
deprecate.event = function (emitter, oldName, newName, fn) { deprecate.event = (emitter, oldName, newName, fn) => {
var warned = false let warned = false
return emitter.on(newName, function (...args) { return emitter.on(newName, function (...args) {
// there is listeners for old API.
if (this.listenerCount(oldName) > 0) { if (this.listenerCount(oldName) > 0) {
if (!(warned || process.noDeprecation)) { if (!(warned || process.noDeprecation)) {
warned = true warned = true
deprecate.warn("'" + oldName + "' event", "'" + newName + "' event") deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
} }
if (fn != null) { if (fn != null) {
fn.apply(this, arguments) fn.apply(this, arguments)
@ -76,15 +71,14 @@ deprecate.event = function (emitter, oldName, newName, fn) {
}) })
} }
// Print deprecation warning. deprecate.warn = (oldName, newName) => {
deprecate.warn = function (oldName, newName) { return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
return deprecate.log(oldName + ' is deprecated. Use ' + newName + ' instead.')
} }
var deprecationHandler = null let 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 +86,14 @@ 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
}
module.exports = deprecate module.exports = deprecate

View file

@ -1,7 +1,7 @@
const assert = require('assert') const assert = require('assert')
const {deprecations, deprecate} = require('electron') const {deprecations, deprecate, ipcRenderer} = require('electron')
describe('deprecations', () => { describe.only('deprecations', () => {
beforeEach(() => { beforeEach(() => {
deprecations.setHandler(null) deprecations.setHandler(null)
process.throwDeprecation = true process.throwDeprecation = true
@ -18,9 +18,50 @@ 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('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')
}, /this is deprecated/) }, /this is deprecated/)
}) })
// it('deprecates a property', () => {
// deprecate.property(object, property, method)
// })
//
// it('deprecates an event', () => {
// deprecate.event(emitter, oldName, newName, fn)
// })
//
// it('forwards a method to member', () => {
// deprecate.member(object, method, member)
// })
it('renames a method', () => {
assert(typeof ipcRenderer.sendSync === 'function')
deprecate.rename(ipcRenderer, 'sendSync', 'sendChannelSync')
assert(typeof ipcRenderer.sendSync === 'undefined')
// assert(typeof ipcRenderer.sendChannelSync === 'function')
})
}) })