import-browser-window-globals.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @fileoverview For scripts included in browser-window, this will automatically
  3. * inject the browser-window global scopes into the file.
  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. var path = require("path");
  11. var helpers = require("../helpers");
  12. var browserWindowEnv = require("../environments/browser-window");
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. url:
  17. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/import-browser-window-globals.html",
  18. },
  19. type: "problem",
  20. },
  21. create(context) {
  22. return {
  23. Program(node) {
  24. let filePath = helpers.getAbsoluteFilePath(context);
  25. let relativePath = path.relative(helpers.rootDir, filePath);
  26. // We need to translate the path on Windows, due to the change
  27. // from \ to /, and browserjsScripts assumes Posix.
  28. if (path.win32) {
  29. relativePath = relativePath.split(path.sep).join("/");
  30. }
  31. if (browserWindowEnv.browserjsScripts?.includes(relativePath)) {
  32. for (let global in browserWindowEnv.globals) {
  33. helpers.addVarToScope(
  34. global,
  35. context.getScope(),
  36. browserWindowEnv.globals[global]
  37. );
  38. }
  39. }
  40. },
  41. };
  42. },
  43. };