import-content-task-globals.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @fileoverview For ContentTask.spawn, this will automatically declare the
  3. * frame script variables in the global scope.
  4. * Note: due to the way ESLint works, it appears it is only
  5. * easy to declare these variables on a file-global scope, rather
  6. * than function global.
  7. *
  8. * This Source Code Form is subject to the terms of the Mozilla Public
  9. * License, v. 2.0. If a copy of the MPL was not distributed with this
  10. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  11. */
  12. "use strict";
  13. var helpers = require("../helpers");
  14. var frameScriptEnv = require("../environments/frame-script");
  15. var sandboxEnv = require("../environments/special-powers-sandbox");
  16. module.exports = {
  17. meta: {
  18. docs: {
  19. url:
  20. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/import-content-task-globals.html",
  21. },
  22. type: "problem",
  23. },
  24. create(context) {
  25. return {
  26. "CallExpression[callee.object.name='ContentTask'][callee.property.name='spawn']": function(
  27. node
  28. ) {
  29. // testing/mochitest/BrowserTestUtils/content/content-task.js
  30. // This script is loaded as a sub script into a frame script.
  31. for (let [name, value] of Object.entries(frameScriptEnv.globals)) {
  32. helpers.addVarToScope(name, context.getScope(), value);
  33. }
  34. },
  35. "CallExpression[callee.object.name='SpecialPowers'][callee.property.name='spawn']": function(
  36. node
  37. ) {
  38. for (let [name, value] of Object.entries(sandboxEnv.globals)) {
  39. helpers.addVarToScope(name, context.getScope(), value);
  40. }
  41. let globals = [
  42. // testing/specialpowers/content/SpecialPowersChild.sys.mjs
  43. // SpecialPowersChild._spawnTask
  44. "SpecialPowers",
  45. "ContentTaskUtils",
  46. "content",
  47. "docShell",
  48. ];
  49. for (let global of globals) {
  50. helpers.addVarToScope(global, context.getScope(), false);
  51. }
  52. },
  53. "CallExpression[callee.object.name='SpecialPowers'][callee.property.name='spawnChrome']": function(
  54. node
  55. ) {
  56. for (let [name, value] of Object.entries(sandboxEnv.globals)) {
  57. helpers.addVarToScope(name, context.getScope(), value);
  58. }
  59. let globals = [
  60. // testing/specialpowers/content/SpecialPowersParent.sys.mjs
  61. // SpecialPowersParent._spawnChrome
  62. "windowGlobalParent",
  63. "browsingContext",
  64. ];
  65. for (let global of globals) {
  66. helpers.addVarToScope(global, context.getScope(), false);
  67. }
  68. },
  69. };
  70. },
  71. };