Commit 81ada6ab authored by Tooru Fujisawa's avatar Tooru Fujisawa
Browse files

Bug 1772358 - Add ESLint rule to reject defining lazy getters for always...

Bug 1772358 - Add ESLint rule to reject defining lazy getters for always available modules. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D149872
parent ba8e631b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ The plugin implements the following rules:
   eslint-plugin-mozilla/prefer-formatValues
   eslint-plugin-mozilla/reject-addtask-only
   eslint-plugin-mozilla/reject-chromeutils-import-params
   eslint-plugin-mozilla/reject-eager-module-in-lazy-getter
   eslint-plugin-mozilla/reject-global-this
   eslint-plugin-mozilla/reject-globalThis-modification
   eslint-plugin-mozilla/reject-importGlobalProperties
+35 −0
Original line number Diff line number Diff line
reject-eager-module-in-lazy-getter
==================================

Rejects defining a lazy getter for module that's known to be loaded early in the
startup process and it is not necessary to lazy load it.

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

.. code-block:: js

    ChromeUtils.defineESModuleGetters(lazy, {
      Services: "resource://gre/modules/Services.sys.mjs",
    });
    XPCOMUtils.defineLazyModuleGetters(lazy, {
      XPCOMUtils: "resource://gre/modules/XPCOMUtils.jsm",
    });
    XPCOMUtils.defineLazyModuleGetter(
      lazy,
      "AppConstants",
      "resource://gre/modules/AppConstants.jsm",
    });

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

.. code-block:: js

    import { Services } from "resource://gre/modules/Services.sys.mjs";
    const { XPCOMUtils } = ChromeUtils.import(
      "resource://gre/modules/XPCOMUtils.jsm"
    );
    const { AppConstants } = ChromeUtils.import(
      "resource://gre/modules/AppConstants.jsm"
    );
+1 −0
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ module.exports = {
      files: ["**/*.sys.mjs", "**/*.jsm", "**/*.jsm.js"],
      rules: {
        "mozilla/lazy-getter-object-name": "error",
        "mozilla/reject-eager-module-in-lazy-getter": "error",
        "mozilla/reject-global-this": "error",
        "mozilla/reject-globalThis-modification": "error",
        "mozilla/reject-top-level-await": "error",
+1 −0
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ module.exports = {
    "prefer-formatValues": require("../lib/rules/prefer-formatValues"),
    "reject-addtask-only": require("../lib/rules/reject-addtask-only"),
    "reject-chromeutils-import-params": require("../lib/rules/reject-chromeutils-import-params"),
    "reject-eager-module-in-lazy-getter": require("../lib/rules/reject-eager-module-in-lazy-getter"),
    "reject-global-this": require("../lib/rules/reject-global-this"),
    "reject-globalThis-modification": require("../lib/rules/reject-globalThis-modification"),
    "reject-import-system-module-from-non-system": require("../lib/rules/reject-import-system-module-from-non-system"),
+103 −0
Original line number Diff line number Diff line
/**
 * @fileoverview Reject use of lazy getters for modules that's loaded early in
 *               the startup process and not necessarily be lazy.
 *
 * 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");

function isString(node) {
  return node.type === "Literal" && typeof node.value === "string";
}

function isEagerModule(resourceURI) {
  return [
    "resource://gre/modules/Services",
    "resource://gre/modules/XPCOMUtils",
    "resource://gre/modules/AppConstants",
  ].includes(resourceURI.replace(/(\.jsm|\.jsm\.js|\.js|\.sys\.mjs)$/, ""));
}

function checkEagerModule(context, node, resourceURI) {
  if (!isEagerModule(resourceURI)) {
    return;
  }
  context.report({
    node,
    messageId: "eagerModule",
    data: { uri: resourceURI },
  });
}

module.exports = {
  meta: {
    docs: {
      url:
        "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-eager-module-in-lazy-getter.html",
    },
    messages: {
      eagerModule:
        'Module "{{uri}}" is known to be loaded early in the startup process, and should be loaded eagerly, instead of defining a lazy getter.',
    },
    type: "problem",
  },

  create(context) {
    return {
      CallExpression(node) {
        if (node.callee.type !== "MemberExpression") {
          return;
        }

        let callerSource;
        try {
          callerSource = helpers.getASTSource(node.callee);
        } catch (e) {
          return;
        }

        if (
          callerSource === "XPCOMUtils.defineLazyModuleGetter" ||
          callerSource === "ChromeUtils.defineModuleGetter"
        ) {
          if (node.arguments.length < 3) {
            return;
          }
          const resourceURINode = node.arguments[2];
          if (!isString(resourceURINode)) {
            return;
          }
          checkEagerModule(context, node, resourceURINode.value);
        } else if (
          callerSource === "XPCOMUtils.defineLazyModuleGetters" ||
          callerSource === "ChromeUtils.defineESModuleGetters"
        ) {
          if (node.arguments.length < 2) {
            return;
          }
          const obj = node.arguments[1];
          if (obj.type !== "ObjectExpression") {
            return;
          }
          for (let prop of obj.properties) {
            if (prop.type !== "Property") {
              continue;
            }
            if (prop.kind !== "init") {
              continue;
            }
            const resourceURINode = prop.value;
            if (!isString(resourceURINode)) {
              continue;
            }
            checkEagerModule(context, node, resourceURINode.value);
          }
        }
      },
    };
  },
};
Loading