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

Differential Revision: https://phabricator.services.mozilla.com/D147400
This commit is contained in:
Tooru Fujisawa 2022-06-17 04:59:13 +00:00
parent 9084893802
commit 088b6d97cd
6 changed files with 123 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,25 @@
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”,
});

View File

@ -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",

View File

@ -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"),

View File

@ -0,0 +1,45 @@
/**
* @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`",
});
}
},
};
},
};

View File

@ -0,0 +1,50 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
var rule = require("../lib/rules/lazy-getter-object-name");
var RuleTester = require("eslint").RuleTester;
const ruleTester = new RuleTester();
// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
function invalidCode(code) {
let message =
"The variable name of the object passed to ChromeUtils.defineESModuleGetters must be `lazy`";
return { code, errors: [{ message, type: "CallExpression" }] };
}
ruleTester.run("lazy-getter-object-name", rule, {
valid: [
`
ChromeUtils.defineESModuleGetters(lazy, {
Services: "resource://gre/modules/Services.sys.mjs",
});
`,
],
invalid: [
invalidCode(`
ChromeUtils.defineESModuleGetters(obj, {
Services: "resource://gre/modules/Services.sys.mjs",
});
`),
invalidCode(`
ChromeUtils.defineESModuleGetters(this, {
Services: "resource://gre/modules/Services.sys.mjs",
});
`),
invalidCode(`
ChromeUtils.defineESModuleGetters(window, {
Services: "resource://gre/modules/Services.sys.mjs",
});
`),
],
});