Bug 1596918: Part 2 - Add ESLint support for SpecialPowers.spawn globals. r=Standard8
authorKris Maglione <maglione.k@gmail.com>
Sat, 07 Dec 2019 18:44:33 +0000
changeset 568113 e2312373dc55560eaee7f28ddd1bff4db06cf1f5
parent 568112 5bae838f369c88fd0a703f4f1f684884359c9e29
child 568114 de196b077000731af0dd911d9e957b7b2d1b46e0
push id12493
push userffxbld-merge
push dateMon, 06 Jan 2020 15:38:57 +0000
treeherdermozilla-beta@63ae456b848d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersStandard8
bugs1596918
milestone73.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1596918: Part 2 - Add ESLint support for SpecialPowers.spawn globals. r=Standard8 This patch updates the existing ContentTask.spawn rule to do similar things for SpecialPowers.spawn calls, only with a slightly different set of globals. Differential Revision: https://phabricator.services.mozilla.com/D53739
testing/specialpowers/content/SpecialPowersSandbox.jsm
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-content-task-globals.js
--- a/testing/specialpowers/content/SpecialPowersSandbox.jsm
+++ b/testing/specialpowers/content/SpecialPowersSandbox.jsm
@@ -13,23 +13,30 @@
 var EXPORTED_SYMBOLS = ["SpecialPowersSandbox"];
 
 ChromeUtils.defineModuleGetter(
   this,
   "Assert",
   "resource://testing-common/Assert.jsm"
 );
 
+// Note: When updating the set of globals exposed to sandboxes by
+// default, please also update the ESLint plugin rule defined in
+// import-content-task-globals.js.
 const SANDBOX_GLOBALS = [
   "Blob",
   "ChromeUtils",
   "TextDecoder",
   "TextEncoder",
   "URL",
 ];
+const EXTRA_IMPORTS = {
+  EventUtils: "resource://specialpowers/SpecialPowersEventUtils.jsm",
+  Services: "resource://gre/modules/Services.jsm",
+};
 
 let expectFail = false;
 function expectingFail(fn) {
   try {
     expectFail = true;
     fn();
   } finally {
     expectFail = false;
@@ -57,24 +64,26 @@ class SpecialPowersSandbox {
           return this.Assert;
         },
         enumerable: true,
         configurable: true,
       });
     }
 
     let imports = {
-      EventUtils: "resource://specialpowers/SpecialPowersEventUtils.jsm",
-      Services: "resource://gre/modules/Services.jsm",
+      ...EXTRA_IMPORTS,
       ...opts.imports,
     };
     for (let [symbol, url] of Object.entries(imports)) {
       ChromeUtils.defineModuleGetter(this.sandbox, symbol, url);
     }
 
+    // Note: When updating the set of globals exposed to sandboxes by
+    // default, please also update the ESLint plugin rule defined in
+    // import-content-task-globals.js.
     Object.assign(this.sandbox, {
       BrowsingContext,
       InspectorUtils,
       ok: (...args) => {
         this.Assert.ok(...args);
       },
       is: (...args) => {
         this.Assert.equal(...args);
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-content-task-globals.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-content-task-globals.js
@@ -31,10 +31,43 @@ module.exports = function(context) {
       for (let global in frameScriptEnv.globals) {
         helpers.addVarToScope(
           global,
           context.getScope(),
           frameScriptEnv.globals[global]
         );
       }
     },
+    "CallExpression[callee.object.name='SpecialPowers'][callee.property.name='spawn']": function(
+      node
+    ) {
+      // The global environment of SpecialPowers.spawn tasks is
+      // controlled by the Sandbox environment created by
+      // SpecialPowersSandbox.jsm. This list should be kept in sync with
+      // that module.
+      let globals = [
+        "Assert",
+        "Blob",
+        "BrowsingContext",
+        "ChromeUtils",
+        "ContentTaskUtils",
+        "EventUtils",
+        "Services",
+        "SpecialPowers",
+        "TextDecoder",
+        "TextEncoder",
+        "URL",
+        "assert",
+        "content",
+        "docShell",
+        "info",
+        "is",
+        "isnot",
+        "ok",
+        "todo",
+        "todo_is",
+      ];
+      for (let global of globals) {
+        helpers.addVarToScope(global, context.getScope(), false);
+      }
+    },
   };
 };