From ccef805e9b85c4e17114e68cb14070c98e8a166c Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 16 Feb 2016 15:09:35 -0800 Subject: [PATCH 1/2] 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 dcaf5f6cf6f9..04b56e06b96d 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 000000000000..871ad0362721 --- /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 6811e73a10b6..51ab48ded833 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 a5369570cade..abb114532106 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 000000000000..282991a6858f --- /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."); + }); +}) From 3e3e59336600c88ebb5220c5be35fd3c07549878 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 16 Feb 2016 15:26:58 -0800 Subject: [PATCH 2/2] :shirt: Add missing semicolons --- atom/common/api/lib/deprecate.js | 2 +- atom/common/api/lib/deprecations.js | 2 +- spec/api-deprecations-spec.js | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/atom/common/api/lib/deprecate.js b/atom/common/api/lib/deprecate.js index 04b56e06b96d..852d544deea9 100644 --- a/atom/common/api/lib/deprecate.js +++ b/atom/common/api/lib/deprecate.js @@ -109,6 +109,6 @@ deprecate.setHandler = function(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 index 871ad0362721..e4290c749474 100644 --- a/atom/common/api/lib/deprecations.js +++ b/atom/common/api/lib/deprecations.js @@ -8,4 +8,4 @@ exports.setHandler = function (deprecationHandler) { exports.getHandler = function () { return deprecate.getHandler(); -} +}; diff --git a/spec/api-deprecations-spec.js b/spec/api-deprecations-spec.js index 282991a6858f..2f0100590598 100644 --- a/spec/api-deprecations-spec.js +++ b/spec/api-deprecations-spec.js @@ -11,17 +11,17 @@ describe('deprecations', function() { var messages = []; deprecations.setHandler(function (message) { - messages.push(message) + messages.push(message); }); - require('electron').webFrame.registerUrlSchemeAsSecure('some-scheme') + 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') + require('electron').webFrame.registerUrlSchemeAsPrivileged('some-scheme'); }, "registerUrlSchemeAsPrivileged is deprecated. Use registerURLSchemeAsPrivileged instead."); }); -}) +});