Commit 01a583ac authored by Mark Banner's avatar Mark Banner
Browse files

Bug 1602940 - Add nsIUUIDGenerator to the Javascript Services object. r=Gijs

This also adds a commented out option for automatically fixing use-services in the ESLint rule. This cannot be enabled at the moment as it needs special treatment for chrome tests.

Differential Revision: https://phabricator.services.mozilla.com/D124391
parent 4759491e
Loading
Loading
Loading
Loading
+41 −27
Original line number Diff line number Diff line
@@ -14,10 +14,11 @@ let servicesInterfaceMap = helpers.servicesData;
// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------
module.exports = function(context) {
  // ---------------------------------------------------------------------------
  // Public
  //  --------------------------------------------------------------------------
module.exports = {
  meta: {
    // fixable: "code",
  },
  create(context) {
    return {
      CallExpression(node) {
        if (
@@ -35,11 +36,24 @@ module.exports = function(context) {
        }

        let serviceName = servicesInterfaceMap[node.arguments[0].property.name];

      context.report(
        context.report({
          node,
        `Use Services.${serviceName} rather than getService().`
      );
          message: `Use Services.${serviceName} rather than getService().`,
          // This is not enabled by default as for mochitest plain tests we
          // would need to replace with `SpecialPowers.Services.${serviceName}`.
          // At the moment we do not have an easy way to detect that.
          // fix(fixer) {
          //   let sourceCode = context.getSourceCode();
          //   return fixer.replaceTextRange(
          //     [
          //       sourceCode.getFirstToken(node.callee).range[0],
          //       sourceCode.getLastToken(node).range[1],
          //     ],
          //     `Services.${serviceName}`
          //   );
          // },
        });
      },
    };
  },
};
+1 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@
    "nsITextToSubURI": "textToSubURI",
    "nsIThreadManager": "tm",
    "nsIURLFormatter": "urlFormatter",
    "nsIUUIDGenerator": "uuid",
    "nsIVersionComparator": "vc",
    "nsIWindowMediator": "wm",
    "nsIWindowWatcher": "ww",
+2 −2
Original line number Diff line number Diff line
@@ -23,8 +23,8 @@ function invalidCode(code, name) {

ruleTester.run("use-services", rule, {
  valid: [
    'Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator)',
    'Components.classes["@mozilla.org/uuid-generator;1"].getService(Components.interfaces.nsIUUIDGenerator)',
    'Cc["@mozilla.org/fakeservice;1"].getService(Ci.nsIFake)',
    'Components.classes["@mozilla.org/fakeservice;1"].getService(Components.interfaces.nsIFake)',
    "Services.wm.addListener()",
  ],
  invalid: [
+2 −0
Original line number Diff line number Diff line
@@ -205,9 +205,11 @@ Classes = [
        'headers': ['/xpcom/build/XPCOMModule.h'],
    },
    {
        'js_name': 'uuid',
        'name': 'UUIDGenerator',
        'cid': '{706d36bb-bf79-4293-81f2-8f6828c18f9d}',
        'contract_ids': ['@mozilla.org/uuid-generator;1'],
        'interfaces': ['nsIUUIDGenerator'],
        'type': 'nsUUIDGenerator',
        'headers': ['/xpcom/base/nsUUIDGenerator.h'],
        'init_method': 'Init',