From 64dd29e2b8029e0ebbd9d6df4ea6a1168fd789e1 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sat, 7 Dec 2019 18:44:33 +0000 Subject: [PATCH] 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 --HG-- extra : moz-landing-system : lando --- .../content/SpecialPowersSandbox.jsm | 13 ++++++-- .../lib/rules/import-content-task-globals.js | 33 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/testing/specialpowers/content/SpecialPowersSandbox.jsm b/testing/specialpowers/content/SpecialPowersSandbox.jsm index ee85db9e9e45..97a2924bb485 100644 --- a/testing/specialpowers/content/SpecialPowersSandbox.jsm +++ b/testing/specialpowers/content/SpecialPowersSandbox.jsm @@ -18,6 +18,9 @@ ChromeUtils.defineModuleGetter( "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", @@ -25,6 +28,10 @@ const SANDBOX_GLOBALS = [ "TextEncoder", "URL", ]; +const EXTRA_IMPORTS = { + EventUtils: "resource://specialpowers/SpecialPowersEventUtils.jsm", + Services: "resource://gre/modules/Services.jsm", +}; let expectFail = false; function expectingFail(fn) { @@ -62,14 +69,16 @@ class SpecialPowersSandbox { } 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, diff --git 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 index 8814d0de661b..c67f75931723 100644 --- 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 @@ -36,5 +36,38 @@ module.exports = function(context) { ); } }, + "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); + } + }, }; };