Merge pull request #4507 from atom/mb-add-deprecation-handler-api

Add API for custom handling of deprecations
This commit is contained in:
Cheng Zhao 2016-02-17 10:51:30 +08:00
commit c02a49a1db
5 changed files with 58 additions and 1 deletions

View file

@ -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;

View file

@ -0,0 +1,11 @@
'use strict';
const deprecate = require('electron').deprecate;
exports.setHandler = function (deprecationHandler) {
deprecate.setHandler(deprecationHandler);
};
exports.getHandler = function () {
return deprecate.getHandler();
};

View file

@ -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() {

View file

@ -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',

View file

@ -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.");
});
});