Commit 088b6d97 authored by Tooru Fujisawa's avatar Tooru Fujisawa
Browse files

Bug 1771097 - Add ESLint rule for ChromeUtils.defineESModuleGetters lazy object name. r=Standard8

parent 90848938
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ The plugin implements the following rules:
   eslint-plugin-mozilla/import-globals
   eslint-plugin-mozilla/import-globals-from
   eslint-plugin-mozilla/import-headjs-globals
   eslint-plugin-mozilla/lazy-getter-object-name
   eslint-plugin-mozilla/mark-exported-symbols-as-used
   eslint-plugin-mozilla/mark-test-function-used
   eslint-plugin-mozilla/no-aArgs
+25 −0
Original line number Diff line number Diff line
lazy-getter-object-name
=============================

Enforce the standard object variable name ``lazy`` for
``ChromeUtils.defineESModuleGetters``

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

.. code-block:: js

    const obj = {};
    ChromeUtils.defineESModuleGetters(obj, {
      Services: “resource://gre/modules/Services.sys.mjs”,
    });

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

.. code-block:: js

    const lazy = {};
    ChromeUtils.defineESModuleGetters(lazy, {
      Services: “resource://gre/modules/Services.sys.mjs”,
    });
+1 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ module.exports = {
      },
      files: ["**/*.sys.mjs", "**/*.jsm", "**/*.jsm.js"],
      rules: {
        "mozilla/lazy-getter-object-name": "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
@@ -39,6 +39,7 @@ module.exports = {
    "import-content-task-globals": require("../lib/rules/import-content-task-globals"),
    "import-globals": require("../lib/rules/import-globals"),
    "import-headjs-globals": require("../lib/rules/import-headjs-globals"),
    "lazy-getter-object-name": require("../lib/rules/lazy-getter-object-name"),
    "mark-exported-symbols-as-used": require("../lib/rules/mark-exported-symbols-as-used"),
    "mark-test-function-used": require("../lib/rules/mark-test-function-used"),
    "no-aArgs": require("../lib/rules/no-aArgs"),
+45 −0
Original line number Diff line number Diff line
/**
 * @fileoverview Enforce the standard object name for
 * ChromeUtils.defineESModuleGetters
 *
 * 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";

function isIdentifier(node, id) {
  return node.type === "Identifier" && node.name === id;
}

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

  create(context) {
    return {
      CallExpression(node) {
        let { callee } = node;
        if (
          callee.type === "MemberExpression" &&
          isIdentifier(callee.object, "ChromeUtils") &&
          isIdentifier(callee.property, "defineESModuleGetters") &&
          node.arguments.length >= 1 &&
          !isIdentifier(node.arguments[0], "lazy")
        ) {
          context.report({
            node,
            message:
              "The variable name of the object passed to ChromeUtils.defineESModuleGetters must be `lazy`",
          });
        }
      },
    };
  },
};
Loading