2019-02-14 22:29:20 +00:00
|
|
|
let deprecationHandler: ElectronInternal.DeprecationHandler | null = null;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
function warnOnce (oldName: string, newName?: string) {
|
2018-09-12 22:13:22 +00:00
|
|
|
let warned = false;
|
|
|
|
const msg = newName
|
|
|
|
? `'${oldName}' is deprecated and will be removed. Please use '${newName}' instead.`
|
|
|
|
: `'${oldName}' is deprecated and will be removed.`;
|
|
|
|
return () => {
|
|
|
|
if (!warned && !process.noDeprecation) {
|
|
|
|
warned = true;
|
|
|
|
deprecate.log(msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
const deprecate: ElectronInternal.DeprecationUtil = {
|
2019-05-23 22:31:38 +00:00
|
|
|
warnOnce,
|
2018-09-11 18:16:49 +00:00
|
|
|
setHandler: (handler) => { deprecationHandler = handler; },
|
|
|
|
getHandler: () => deprecationHandler,
|
|
|
|
warn: (oldName, newName) => {
|
2018-12-05 09:34:09 +00:00
|
|
|
if (!process.noDeprecation) {
|
|
|
|
deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`);
|
|
|
|
}
|
2018-09-11 18:16:49 +00:00
|
|
|
},
|
|
|
|
log: (message) => {
|
|
|
|
if (typeof deprecationHandler === 'function') {
|
|
|
|
deprecationHandler(message);
|
|
|
|
} else if (process.throwDeprecation) {
|
|
|
|
throw new Error(message);
|
|
|
|
} else if (process.traceDeprecation) {
|
|
|
|
return console.trace(message);
|
2018-04-23 19:46:12 +00:00
|
|
|
} else {
|
2018-09-11 18:16:49 +00:00
|
|
|
return console.warn(`(electron) ${message}`);
|
2018-04-23 19:46:12 +00:00
|
|
|
}
|
2018-09-11 18:16:49 +00:00
|
|
|
},
|
2018-09-12 22:13:22 +00:00
|
|
|
|
2019-07-09 01:01:35 +00:00
|
|
|
// remove a function with no replacement
|
|
|
|
removeFunction: (fn, removedName) => {
|
|
|
|
if (!fn) { throw Error(`'${removedName} function' is invalid or does not exist.`); }
|
|
|
|
|
|
|
|
// wrap the deprecated function to warn user
|
|
|
|
const warn = warnOnce(`${fn.name} function`);
|
|
|
|
return function (this: any) {
|
|
|
|
warn();
|
|
|
|
fn.apply(this, arguments);
|
2020-10-08 01:01:23 +00:00
|
|
|
} as unknown as typeof fn;
|
2019-07-09 01:01:35 +00:00
|
|
|
},
|
|
|
|
|
2019-03-14 22:19:19 +00:00
|
|
|
// change the name of a function
|
2019-07-09 01:01:35 +00:00
|
|
|
renameFunction: (fn, newName) => {
|
|
|
|
const warn = warnOnce(`${fn.name} function`, `${newName} function`);
|
2019-02-14 22:29:20 +00:00
|
|
|
return function (this: any) {
|
2019-02-08 17:50:11 +00:00
|
|
|
warn();
|
2019-08-14 20:42:55 +00:00
|
|
|
return fn.apply(this, arguments);
|
2020-10-08 01:01:23 +00:00
|
|
|
} as unknown as typeof fn;
|
2019-08-14 20:42:55 +00:00
|
|
|
},
|
|
|
|
|
2020-06-30 19:49:08 +00:00
|
|
|
moveAPI<T extends Function> (fn: T, oldUsage: string, newUsage: string): T {
|
2019-08-14 20:42:55 +00:00
|
|
|
const warn = warnOnce(oldUsage, newUsage);
|
|
|
|
return function (this: any) {
|
|
|
|
warn();
|
|
|
|
return fn.apply(this, arguments);
|
2020-10-08 01:01:23 +00:00
|
|
|
} as unknown as typeof fn;
|
2019-02-08 17:50:11 +00:00
|
|
|
},
|
|
|
|
|
2019-03-14 22:19:19 +00:00
|
|
|
// change the name of an event
|
2018-09-11 18:16:49 +00:00
|
|
|
event: (emitter, oldName, newName) => {
|
2018-09-12 22:13:22 +00:00
|
|
|
const warn = newName.startsWith('-') /* internal event */
|
|
|
|
? warnOnce(`${oldName} event`)
|
|
|
|
: warnOnce(`${oldName} event`, `${newName} event`);
|
2019-02-14 22:29:20 +00:00
|
|
|
return emitter.on(newName, function (this: NodeJS.EventEmitter, ...args) {
|
2018-09-12 22:13:22 +00:00
|
|
|
if (this.listenerCount(oldName) !== 0) {
|
|
|
|
warn();
|
|
|
|
this.emit(oldName, ...args);
|
2018-09-11 18:16:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2018-05-29 13:58:02 +00:00
|
|
|
|
2019-03-14 22:19:19 +00:00
|
|
|
// remove a property with no replacement
|
2020-02-26 22:03:52 +00:00
|
|
|
removeProperty: (o, removedName, onlyForValues) => {
|
2018-09-12 22:13:22 +00:00
|
|
|
// if the property's already been removed, warn about it
|
2020-02-26 22:03:52 +00:00
|
|
|
const info = Object.getOwnPropertyDescriptor((o as any).__proto__, removedName) // eslint-disable-line
|
|
|
|
if (!info) {
|
2018-09-12 22:13:22 +00:00
|
|
|
deprecate.log(`Unable to remove property '${removedName}' from an object that lacks it.`);
|
2020-02-26 22:03:52 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
if (!info.get || !info.set) {
|
|
|
|
deprecate.log(`Unable to remove property '${removedName}' from an object does not have a getter / setter`);
|
|
|
|
return o;
|
2018-09-11 18:16:49 +00:00
|
|
|
}
|
2018-05-29 13:58:02 +00:00
|
|
|
|
2018-09-12 22:13:22 +00:00
|
|
|
// wrap the deprecated property in an accessor to warn
|
|
|
|
const warn = warnOnce(removedName);
|
|
|
|
return Object.defineProperty(o, removedName, {
|
2018-09-11 18:16:49 +00:00
|
|
|
configurable: true,
|
|
|
|
get: () => {
|
|
|
|
warn();
|
2020-02-26 22:03:52 +00:00
|
|
|
return info.get!.call(o);
|
2018-09-11 18:16:49 +00:00
|
|
|
},
|
2018-09-12 22:13:22 +00:00
|
|
|
set: newVal => {
|
2020-02-26 22:03:52 +00:00
|
|
|
if (!onlyForValues || onlyForValues.includes(newVal)) {
|
|
|
|
warn();
|
|
|
|
}
|
|
|
|
return info.set!.call(o, newVal);
|
2018-09-11 18:16:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-03-14 22:19:19 +00:00
|
|
|
// change the name of a property
|
2018-09-12 22:13:22 +00:00
|
|
|
renameProperty: (o, oldName, newName) => {
|
|
|
|
const warn = warnOnce(oldName, newName);
|
|
|
|
|
|
|
|
// if the new property isn't there yet,
|
|
|
|
// inject it and warn about it
|
|
|
|
if ((oldName in o) && !(newName in o)) {
|
2018-05-29 13:58:02 +00:00
|
|
|
warn();
|
2019-02-14 22:29:20 +00:00
|
|
|
o[newName] = (o as any)[oldName];
|
2018-05-29 13:40:19 +00:00
|
|
|
}
|
2018-09-11 18:16:49 +00:00
|
|
|
|
2018-09-12 22:13:22 +00:00
|
|
|
// wrap the deprecated property in an accessor to warn
|
|
|
|
// and redirect to the new property
|
|
|
|
return Object.defineProperty(o, oldName, {
|
|
|
|
get: () => {
|
2018-09-11 18:16:49 +00:00
|
|
|
warn();
|
2018-09-12 22:13:22 +00:00
|
|
|
return o[newName];
|
2018-09-11 18:16:49 +00:00
|
|
|
},
|
2018-09-12 22:13:22 +00:00
|
|
|
set: value => {
|
2018-09-11 18:16:49 +00:00
|
|
|
warn();
|
2018-09-12 22:13:22 +00:00
|
|
|
o[newName] = value;
|
2018-09-11 18:16:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-05-29 13:40:19 +00:00
|
|
|
};
|
2017-11-20 14:12:34 +00:00
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
export default deprecate;
|