Report deprecated BrowserWindow options

This commit is contained in:
Kevin Sawicki 2016-03-16 09:38:03 -07:00
parent 1b6e01ce6d
commit 15397bf879
2 changed files with 71 additions and 1 deletions

View file

@ -8,7 +8,6 @@ const BrowserWindow = process.atomBinding('window').BrowserWindow;
BrowserWindow.prototype.__proto__ = EventEmitter.prototype;
BrowserWindow.prototype._init = function() {
// avoid recursive require.
var app, menu;
app = require('electron').app;
@ -240,4 +239,56 @@ BrowserWindow.prototype.getPageTitle = deprecate('getPageTitle', 'webContents.ge
return (ref1 = this.webContents) != null ? ref1.getTitle() : void 0;
});
const isDeprecatedKey = function(key) {
return key.indexOf('-') >= 0;
};
// Map deprecated key with hyphens to camel case key
const getNonDeprecatedKey = function(deprecatedKey) {
return deprecatedKey.replace(/-./g, function(match) {
return match[1].toUpperCase();
});
};
// TODO Remove for 1.0
const checkForDeprecatedOptions = function(options) {
if (!options) return '';
let keysToCheck = Object.keys(options);
if (options.webPreferences) {
keysToCheck = keysToCheck.concat(Object.keys(options.webPreferences));
}
// Check options for keys with hypens in them
let deprecatedKey = keysToCheck.filter(isDeprecatedKey)[0];
if (deprecatedKey) {
try {
deprecate.warn(deprecatedKey, getNonDeprecatedKey(deprecatedKey));
} catch (error) {
// Return error message so it can be rethrown via C++
return error.message;
}
}
let webPreferenceOption;
if (options.hasOwnProperty('nodeIntegration')) {
webPreferenceOption = 'nodeIntegration';
} else if (options.hasOwnProperty('preload')) {
webPreferenceOption = 'preload';
} else if (options.hasOwnProperty('zoomFactor')) {
webPreferenceOption = 'zoomFactor';
}
if (webPreferenceOption) {
try {
deprecate.log(`Setting ${webPreferenceOption} on options is deprecated. Set it on options.webPreferences instead.`);
} catch (error) {
// Return error message so it can be rethrown via C++
return error.message;
}
}
return '';
};
BrowserWindow._setDeprecatedOptionsCheck(checkForDeprecatedOptions);
module.exports = BrowserWindow;