reject-lazy-imports-into-globals.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. */
  6. "use strict";
  7. const helpers = require("../helpers");
  8. const callExpressionDefinitions = [
  9. /^loader\.lazyGetter\((?:globalThis|window), "(\w+)"/,
  10. /^loader\.lazyServiceGetter\((?:globalThis|window), "(\w+)"/,
  11. /^loader\.lazyRequireGetter\((?:globalThis|window), "(\w+)"/,
  12. /^XPCOMUtils\.defineLazyGetter\((?:globalThis|window), "(\w+)"/,
  13. /^XPCOMUtils\.defineLazyModuleGetter\((?:globalThis|window), "(\w+)"/,
  14. /^ChromeUtils\.defineModuleGetter\((?:globalThis|window), "(\w+)"/,
  15. /^XPCOMUtils\.defineLazyPreferenceGetter\((?:globalThis|window), "(\w+)"/,
  16. /^XPCOMUtils\.defineLazyProxy\((?:globalThis|window), "(\w+)"/,
  17. /^XPCOMUtils\.defineLazyScriptGetter\((?:globalThis|window), "(\w+)"/,
  18. /^XPCOMUtils\.defineLazyServiceGetter\((?:globalThis|window), "(\w+)"/,
  19. /^XPCOMUtils\.defineConstant\((?:globalThis|window), "(\w+)"/,
  20. /^DevToolsUtils\.defineLazyModuleGetter\((?:globalThis|window), "(\w+)"/,
  21. /^DevToolsUtils\.defineLazyGetter\((?:globalThis|window), "(\w+)"/,
  22. /^Object\.defineProperty\((?:globalThis|window), "(\w+)"/,
  23. /^Reflect\.defineProperty\((?:globalThis|window), "(\w+)"/,
  24. /^this\.__defineGetter__\("(\w+)"/,
  25. ];
  26. const callExpressionMultiDefinitions = [
  27. "XPCOMUtils.defineLazyGlobalGetters(window,",
  28. "XPCOMUtils.defineLazyGlobalGetters(globalThis,",
  29. "XPCOMUtils.defineLazyModuleGetters(window,",
  30. "XPCOMUtils.defineLazyModuleGetters(globalThis,",
  31. "XPCOMUtils.defineLazyServiceGetters(window,",
  32. "XPCOMUtils.defineLazyServiceGetters(globalThis,",
  33. "ChromeUtils.defineESModuleGetters(window,",
  34. "ChromeUtils.defineESModuleGetters(globalThis,",
  35. "loader.lazyRequireGetter(window,",
  36. "loader.lazyRequireGetter(globalThis,",
  37. ];
  38. module.exports = {
  39. meta: {
  40. docs: {
  41. url:
  42. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-lazy-imports-into-globals.html",
  43. },
  44. messages: {
  45. rejectLazyImportsIntoGlobals:
  46. "Non-system modules should not import into globalThis nor window. Prefer a lazy object holder",
  47. },
  48. type: "suggestion",
  49. },
  50. create(context) {
  51. return {
  52. CallExpression(node) {
  53. let source;
  54. try {
  55. source = helpers.getASTSource(node);
  56. } catch (e) {
  57. return;
  58. }
  59. if (
  60. callExpressionDefinitions.some(expr => source.match(expr)) ||
  61. callExpressionMultiDefinitions.some(expr => source.startsWith(expr))
  62. ) {
  63. context.report({ node, messageId: "rejectLazyImportsIntoGlobals" });
  64. }
  65. },
  66. };
  67. },
  68. };