From 8f623405f74e6affb771ac3e9109dc41be0ee383 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Fri, 17 Sep 2021 16:14:05 +0200 Subject: [PATCH] Modernize deprecation handling --- src/LightDMMock.js | 68 ++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/src/LightDMMock.js b/src/LightDMMock.js index 84790f4..26d8ff6 100644 --- a/src/LightDMMock.js +++ b/src/LightDMMock.js @@ -338,6 +338,27 @@ class LightDMMock { user.session = this.sessions[Math.floor((Math.random() * this.sessions.length))].name; } }); + + const deprecations = { + properties: [ + { deprecated: "default_language", alternative: "lightdm.language" }, + { deprecated: "default_layout", alternative: "lightdm.layout" }, + { deprecated: "select_guest", alternative: "lightdm.select_guest_hint" }, + { deprecated: "select_user", alternative: "lightdm.select_user_hint" }, + { deprecated: "timed_login_user", alternative: "lightdm.autologin_user" }, + { deprecated: "timed_login_delay", alternative: "lightdm.autologin_timeout" }, + ], + }; + + return new Proxy(this, { + get: (o, k, r) => { + if (deprecations.properties.map(o => o.deprecated).includes(k)) { + deprecationNotifier("property", k, deprecations.properties.alternative) + } else { + return Reflect.get(o, k, r); + } + } + }); } } } @@ -652,16 +673,21 @@ function IncompatibleArgumentTypesException(number, expected, received) { ******************************************************************************/ /** - * global helper deprecationNotifier - * throws ~balls~ errors at users who use deprecated methods and properties. + * @desc + * throws ~balls~ errors at users who use deprecated methods and properties. * - * @param {String} type [method||property] - * @param {String} depricated [deprecated method or property name] - * @param {String} alternative [alternative method or property to use] + * @param {String} type + * "method" or "property" + * + * @param {String} deprecated + * deprecated method or property name + * + * @param {String} alternative + * alternative method or property to use * * @throws {DeprecationException} */ -window.deprecationNotifier = function(type, deprecated, alternative) { +export const deprecationNotifier = (type, deprecated, alternative) => { throw new DeprecationException(type, deprecated, alternative); }; @@ -876,36 +902,6 @@ if(!Object.prototype.unwatch) { }); } - -/****************************************************************************** - * Deprecated properties * - ******************************************************************************/ - -LightDMMock.watch('default_language', function() { - window.deprecationNotifier("property", "default_language", "lightdm.language"); -}); - -LightDMMock.watch('default_layout', function() { - window.deprecationNotifier("property", "default_layout", "lightdm.layout"); -}); - -LightDMMock.watch('select_guest', function() { - window.deprecationNotifier("property", "select_guest", "lightdm.select_guest_hint"); -}); - -LightDMMock.watch('select_user', function() { - window.deprecationNotifier("property", "select_user", "lightdm.select_user_hint"); -}); - -LightDMMock.watch('timed_login_user', function() { - window.deprecationNotifier("property", "timed_login_user", "lightdm.autologin_user"); -}); - -LightDMMock.watch('timed_login_delay', function() { - window.deprecationNotifier("property", "timed_login_delay", "lightdm.autologin_timeout"); -}); - - /****************************************************************************** * Module loading * ******************************************************************************/