lazy-getter-object-name.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * @fileoverview Enforce the standard object name for
  3. * ChromeUtils.defineESModuleGetters
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. "use strict";
  10. function isIdentifier(node, id) {
  11. return node.type === "Identifier" && node.name === id;
  12. }
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. url:
  17. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/lazy-getter-object-name.html",
  18. },
  19. type: "problem",
  20. },
  21. create(context) {
  22. return {
  23. CallExpression(node) {
  24. let { callee } = node;
  25. if (
  26. callee.type === "MemberExpression" &&
  27. isIdentifier(callee.object, "ChromeUtils") &&
  28. isIdentifier(callee.property, "defineESModuleGetters") &&
  29. node.arguments.length >= 1 &&
  30. !isIdentifier(node.arguments[0], "lazy")
  31. ) {
  32. context.report({
  33. node,
  34. message:
  35. "The variable name of the object passed to ChromeUtils.defineESModuleGetters must be `lazy`",
  36. });
  37. }
  38. },
  39. };
  40. },
  41. };