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.
const deprecate = function (oldName, newName, fn) {
var warned
warned = false
let warned = false
return function () {
if (!(warned || process.noDeprecation)) {
warned = true
@ -13,9 +12,8 @@ const deprecate = function (oldName, newName, fn) {
// The method is renamed.
deprecate.rename = function (object, oldName, newName) {
var newMethod, warned
warned = false
newMethod = function () {
let warned = false
const newMethod = function () {
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(oldName, newName)
@ -30,27 +28,25 @@ deprecate.rename = function (object, oldName, newName) {
}
// Forward the method to member.
deprecate.member = function (object, method, member) {
var warned
warned = false
deprecate.member = (object, method, member) => {
let warned = false
object.prototype[method] = function () {
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(method, member + '.' + method)
deprecate.warn(method, `${member}.${method}`)
}
return this[member][method].apply(this[member], arguments)
}
}
// Deprecate a property.
deprecate.property = function (object, property, method) {
deprecate.property = (object, property, method) => {
return Object.defineProperty(object, property, {
get: function () {
var warned
warned = false
let warned = false
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(property + ' property', method + ' method')
deprecate.warn(`${property} property`, `${method} method`)
}
return this[method]()
}
@ -58,14 +54,13 @@ deprecate.property = function (object, property, method) {
}
// Deprecate an event.
deprecate.event = function (emitter, oldName, newName, fn) {
var warned = false
deprecate.event = (emitter, oldName, newName, fn) => {
let 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")
deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
}
if (fn != null) {
fn.apply(this, arguments)
@ -76,15 +71,14 @@ deprecate.event = function (emitter, oldName, newName, fn) {
})
}
// Print deprecation warning.
deprecate.warn = function (oldName, newName) {
return deprecate.log(oldName + ' is deprecated. Use ' + newName + ' instead.')
deprecate.warn = (oldName, newName) => {
return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
}
var deprecationHandler = null
let deprecationHandler = null
// Print deprecation message.
deprecate.log = function (message) {
deprecate.log = (message) => {
if (typeof deprecationHandler === 'function') {
deprecationHandler(message)
} else if (process.throwDeprecation) {
@ -92,16 +86,14 @@ deprecate.log = function (message) {
} else if (process.traceDeprecation) {
return console.trace(message)
} else {
return console.warn('(electron) ' + message)
return console.warn(`(electron) ${message}`)
}
}
deprecate.setHandler = function (handler) {
deprecate.setHandler = (handler) => {
deprecationHandler = handler
}
deprecate.getHandler = function () {
return deprecationHandler
}
deprecate.getHandler = () => deprecationHandler
module.exports = deprecate

View file

@ -1,7 +1,7 @@
const assert = require('assert')
const {deprecations, deprecate} = require('electron')
const {deprecations, deprecate, ipcRenderer} = require('electron')
describe('deprecations', () => {
describe.only('deprecations', () => {
beforeEach(() => {
deprecations.setHandler(null)
process.throwDeprecation = true
@ -18,9 +18,50 @@ describe('deprecations', () => {
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', () => {
assert.throws(() => {
deprecate.log('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')
})
})