commit d96e9bd92c21f9576e858ce240757549d2ffb046 parent 024c584172aec2b37e8465b395c3b92029f2b8c5 Author: Mark Banner <standard8@mozilla.com> Date: Fri, 17 Oct 2025 09:32:54 +0000 Bug 1993543 - Simplify XPCOMUtils.defineLazyServiceGetter to require the interface parameter as an nsIID. r=spidermonkey-reviewers,frontend-codestyle-reviewers,Gijs,arai Differential Revision: https://phabricator.services.mozilla.com/D268195 Diffstat:
5 files changed, 15 insertions(+), 17 deletions(-)
diff --git a/js/xpconnect/loader/XPCOMUtils.sys.mjs b/js/xpconnect/loader/XPCOMUtils.sys.mjs @@ -119,18 +119,12 @@ export var XPCOMUtils = { * The name of the getter to define on aObject for the service. * @param {string} aContract * The contract used to obtain the service. - * @param {nsID|string} aInterface + * @param {nsIID} aInterface * The interface or name of interface to query the service to. */ defineLazyServiceGetter(aObject, aName, aContract, aInterface) { ChromeUtils.defineLazyGetter(aObject, aName, () => { - if (aInterface) { - if (typeof aInterface === "string") { - aInterface = Ci[aInterface]; - } - return Cc[aContract].getService(aInterface); - } - return Cc[aContract].getService().wrappedJSObject; + return Cc[aContract].getService(aInterface); }); }, diff --git a/js/xpconnect/tests/unit/test_xpcomutils.js b/js/xpconnect/tests/unit/test_xpcomutils.js @@ -48,7 +48,7 @@ add_test(function test_defineLazyServiceGetter() let obj = { }; XPCOMUtils.defineLazyServiceGetter(obj, "service", "@mozilla.org/consoleservice;1", - "nsIConsoleService"); + Ci.nsIConsoleService); let service = Cc["@mozilla.org/consoleservice;1"]. getService(Ci.nsIConsoleService); diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-services.mjs b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-services.mjs @@ -35,10 +35,12 @@ export default { node.callee.property.type == "Identifier" && node.callee.property.name == "defineLazyServiceGetter" && node.arguments.length == 4 && - node.arguments[3].type == "Literal" && - node.arguments[3].value in servicesInterfaceMap + node.arguments[3].type == "MemberExpression" && + node.arguments[3].property.type == "Identifier" && + node.arguments[3].property.name in servicesInterfaceMap ) { - let serviceName = servicesInterfaceMap[node.arguments[3].value]; + let serviceName = + servicesInterfaceMap[node.arguments[3].property.name]; context.report({ node, @@ -61,10 +63,12 @@ export default { if ( property.value.type == "ArrayExpression" && property.value.elements.length == 2 && - property.value.elements[1].value in servicesInterfaceMap + property.value.elements[1].type == "MemberExpression" && + property.value.elements[1].property.type == "Identifier" && + property.value.elements[1].property.name in servicesInterfaceMap ) { let serviceName = - servicesInterfaceMap[property.value.elements[1].value]; + servicesInterfaceMap[property.value.elements[1].property.name]; context.report({ node: property.value, diff --git a/tools/lint/eslint/eslint-plugin-mozilla/tests/reject-lazy-imports-into-globals.mjs b/tools/lint/eslint/eslint-plugin-mozilla/tests/reject-lazy-imports-into-globals.mjs @@ -32,7 +32,7 @@ ruleTester.run("reject-lazy-imports-into-globals", rule, { `XPCOMUtils.defineLazyPreferenceGetter(globalThis, "foo", "foo.bar");` ), invalidCode( - `XPCOMUtils.defineLazyServiceGetter(globalThis, "foo", "@foo", "nsIFoo");` + `XPCOMUtils.defineLazyServiceGetter(globalThis, "foo", "@foo", Ci.nsIFoo);` ), invalidCode(`XPCOMUtils.defineLazyGlobalGetters(globalThis, {});`), invalidCode(`XPCOMUtils.defineLazyGlobalGetters(window, {});`), diff --git a/tools/lint/eslint/eslint-plugin-mozilla/tests/use-services.mjs b/tools/lint/eslint/eslint-plugin-mozilla/tests/use-services.mjs @@ -42,7 +42,7 @@ ruleTester.run("use-services", rule, { ), invalidCode( `XPCOMUtils.defineLazyServiceGetters(this, { - uuidGen: ["@mozilla.org/uuid-generator;1", "nsIUUIDGenerator"], + uuidGen: ["@mozilla.org/uuid-generator;1", Ci.nsIUUIDGenerator], });`, "uuid", "defineLazyServiceGetters", @@ -53,7 +53,7 @@ ruleTester.run("use-services", rule, { this, "gELS", "@mozilla.org/eventlistenerservice;1", - "nsIEventListenerService" + Ci.nsIEventListenerService );`, "els", "defineLazyServiceGetter"