From d05a1f8053883917d9a105b32c366c004b121722 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 13 Nov 2017 20:21:57 -0500 Subject: [PATCH] first pass at deprecation spec updates --- lib/common/api/deprecate.js | 46 +++++++++++++++-------------------- spec/api-deprecations-spec.js | 45 ++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/lib/common/api/deprecate.js b/lib/common/api/deprecate.js index c146e03e6a80..97ed3551b545 100644 --- a/lib/common/api/deprecate.js +++ b/lib/common/api/deprecate.js @@ -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 diff --git a/spec/api-deprecations-spec.js b/spec/api-deprecations-spec.js index 00815b284cb3..44e20b241739 100644 --- a/spec/api-deprecations-spec.js +++ b/spec/api-deprecations-spec.js @@ -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') + }) })