From ccef805e9b85c4e17114e68cb14070c98e8a166c Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 16 Feb 2016 15:09:35 -0800 Subject: [PATCH] Add API for custom handling of deprecations --- atom/common/api/lib/deprecate.js | 14 ++++++++++++- atom/common/api/lib/deprecations.js | 11 ++++++++++ atom/common/api/lib/exports/electron.js | 6 ++++++ filenames.gypi | 1 + spec/api-deprecations-spec.js | 27 +++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 atom/common/api/lib/deprecations.js create mode 100644 spec/api-deprecations-spec.js diff --git a/atom/common/api/lib/deprecate.js b/atom/common/api/lib/deprecate.js index dcaf5f6cf6f..04b56e06b96 100644 --- a/atom/common/api/lib/deprecate.js +++ b/atom/common/api/lib/deprecate.js @@ -88,9 +88,13 @@ deprecate.warn = function(oldName, newName) { return deprecate.log(oldName + " is deprecated. Use " + newName + " instead."); }; +var deprecationHandler = null; + // Print deprecation message. deprecate.log = function(message) { - if (process.throwDeprecation) { + if (typeof deprecationHandler === 'function') { + deprecationHandler(message); + } else if (process.throwDeprecation) { throw new Error(message); } else if (process.traceDeprecation) { return console.trace(message); @@ -99,4 +103,12 @@ deprecate.log = function(message) { } }; +deprecate.setHandler = function(handler) { + deprecationHandler = handler; +}; + +deprecate.getHandler = function() { + return deprecationHandler; +} + module.exports = deprecate; diff --git a/atom/common/api/lib/deprecations.js b/atom/common/api/lib/deprecations.js new file mode 100644 index 00000000000..871ad036272 --- /dev/null +++ b/atom/common/api/lib/deprecations.js @@ -0,0 +1,11 @@ +'use strict'; + +const deprecate = require('electron').deprecate; + +exports.setHandler = function (deprecationHandler) { + deprecate.setHandler(deprecationHandler); +}; + +exports.getHandler = function () { + return deprecate.getHandler(); +} diff --git a/atom/common/api/lib/exports/electron.js b/atom/common/api/lib/exports/electron.js index 6811e73a10b..51ab48ded83 100644 --- a/atom/common/api/lib/exports/electron.js +++ b/atom/common/api/lib/exports/electron.js @@ -31,6 +31,12 @@ exports.defineProperties = function(exports) { return require('../crash-reporter'); } }, + deprecations: { + enumerable: true, + get: function() { + return require('../deprecations'); + } + }, nativeImage: { enumerable: true, get: function() { diff --git a/filenames.gypi b/filenames.gypi index a5369570cad..abb11453210 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -41,6 +41,7 @@ 'atom/common/api/lib/clipboard.js', 'atom/common/api/lib/crash-reporter.js', 'atom/common/api/lib/deprecate.js', + 'atom/common/api/lib/deprecations.js', 'atom/common/api/lib/exports/electron.js', 'atom/common/api/lib/native-image.js', 'atom/common/api/lib/shell.js', diff --git a/spec/api-deprecations-spec.js b/spec/api-deprecations-spec.js new file mode 100644 index 00000000000..282991a6858 --- /dev/null +++ b/spec/api-deprecations-spec.js @@ -0,0 +1,27 @@ +const assert = require('assert'); +const deprecations = require('electron').deprecations; + +describe('deprecations', function() { + beforeEach(function() { + deprecations.setHandler(null); + process.throwDeprecation = true; + }); + + it('allows a deprecation handler function to be specified', function() { + var messages = []; + + deprecations.setHandler(function (message) { + messages.push(message) + }); + + require('electron').webFrame.registerUrlSchemeAsSecure('some-scheme') + + assert.deepEqual(messages, ['registerUrlSchemeAsSecure is deprecated. Use registerURLSchemeAsSecure instead.']); + }); + + it('throws an exception if no deprecation handler is specified', function() { + assert.throws(function() { + require('electron').webFrame.registerUrlSchemeAsPrivileged('some-scheme') + }, "registerUrlSchemeAsPrivileged is deprecated. Use registerURLSchemeAsPrivileged instead."); + }); +})