Commit 8ca4d49d authored by Mark Banner's avatar Mark Banner
Browse files

Bug 1766228 - Add an ESLint rule to ensure that member property accesses of...

Bug 1766228 - Add an ESLint rule to ensure that member property accesses of Services are valid. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D149393
parent b813d153
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@
// This fixture is probably doing too much and should be reduced to the minimum
// needed to pass the tests.

/* eslint-disable mozilla/valid-services */

// Some constants from nsIPrefBranch.idl.
const PREF_INVALID = 0;
const PREF_STRING = 32;
+22 −0
Original line number Diff line number Diff line
valid-services
==============

Ensures that accesses of the ``Services`` object are valid.

Examples of incorrect code for this rule:
-----------------------------------------

Assuming ``foo`` is not defined within Services.

.. code-block:: js

    Services.foo.fn();

Examples of correct code for this rule:
---------------------------------------

Assuming ``bar`` is defined within Services.

.. code-block:: js

    Services.bar.fn();
+1 −0
Original line number Diff line number Diff line
@@ -179,6 +179,7 @@ module.exports = {
    "mozilla/use-ownerGlobal": "error",
    "mozilla/use-returnValue": "error",
    "mozilla/use-services": "error",
    "mozilla/valid-services": "error",

    // Use [] instead of Array()
    "no-array-constructor": "error",
+1 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ module.exports = {
    "use-isInstance": require("./rules/use-isInstance"),
    "use-returnValue": require("../lib/rules/use-returnValue"),
    "use-services": require("../lib/rules/use-services"),
    "valid-services": require("../lib/rules/valid-services"),
    "var-only-at-top-level": require("../lib/rules/var-only-at-top-level"),
  },
};
+59 −0
Original line number Diff line number Diff line
/**
 * @fileoverview Require use of Services.* rather than getService.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

"use strict";
const helpers = require("../helpers");

module.exports = {
  meta: {
    docs: {
      url:
        "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-services.html",
    },
    type: "problem",
  },

  create(context) {
    let servicesInterfaceMap = helpers.servicesData;
    let serviceAliases = new Set([
      ...Object.values(servicesInterfaceMap),
      // This is defined only for Android, so most builds won't pick it up.
      "androidBridge",
      // These are defined without interfaces and hence are not in the services map.
      "cpmm",
      "crashmanager",
      "mm",
      "ppmm",
      // The new xulStore also does not have an interface.
      "xulStore",
    ]);
    return {
      MemberExpression(node) {
        if (node.computed || node.object.type !== "Identifier") {
          return;
        }

        let alias;
        if (node.object.name == "Services") {
          alias = node.property.name;
        } else if (
          node.property.name == "Services" &&
          node.parent.type == "MemberExpression"
        ) {
          alias = node.parent.property.name;
        } else {
          return;
        }

        if (!serviceAliases.has(alias)) {
          context.report(node, `Unknown Services member property ${alias}`);
        }
      },
    };
  },
};
Loading