Bug 1352969 - Improve ESLint environment browser-window globals list, and automatically inject it into browser-window scripts. r?Mossop draft
authorMark Banner <standard8@mozilla.com>
Thu, 09 Mar 2017 17:17:26 +0000
changeset 555655 6fdad5a14b738dc6beb51aed9d97c789c0384455
parent 555653 e4e140b6452586ab7610364a53394af746abbce6
child 622660 c899b36652937d770a3e856932e5e401e43c0e51
push id52295
push userbmo:standard8@mozilla.com
push dateTue, 04 Apr 2017 16:14:13 +0000
reviewersMossop
bugs1352969
milestone55.0a1
Bug 1352969 - Improve ESLint environment browser-window globals list, and automatically inject it into browser-window scripts. r?Mossop MozReview-Commit-ID: 3lv0K1unAc
.eslintrc.js
tools/lint/docs/linters/eslint-plugin-mozilla.rst
tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js
tools/lint/eslint/eslint-plugin-mozilla/lib/environments/browser-window.js
tools/lint/eslint/eslint-plugin-mozilla/lib/index.js
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-browser-window-globals.js
tools/lint/eslint/eslint-plugin-mozilla/package.json
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -3,16 +3,17 @@
 module.exports = {
   // When adding items to this file please check for effects on sub-directories.
   "plugins": [
     "mozilla"
   ],
   "rules": {
     "mozilla/avoid-removeChild": "error",
     "mozilla/avoid-nsISupportsString-preferences": "error",
+    "mozilla/import-browser-window-globals": "error",
     "mozilla/import-globals": "warn",
     "mozilla/no-import-into-var-and-global": "error",
     "mozilla/no-useless-parameters": "error",
     "mozilla/no-useless-removeEventListener": "error",
     "mozilla/use-default-preference-values": "error",
     "mozilla/use-ownerGlobal": "error",
 
     // No (!foo in bar) or (!object instanceof Class)
--- a/tools/lint/docs/linters/eslint-plugin-mozilla.rst
+++ b/tools/lint/docs/linters/eslint-plugin-mozilla.rst
@@ -11,17 +11,18 @@ e.g.
    /* eslint-env mozilla/chrome-worker */
 
 There are also built-in ESLint environments available as well:
 http://eslint.org/docs/user-guide/configuring#specifying-environments
 
 browser-window
 --------------
 
-Defines the environment for scripts that are in the main browser.xul scope.
+Defines the environment for scripts that are in the main browser.xul scope (
+note: includes the places-overlay environment).
 
 places-overlay
 --------------
 
 Defines the environment for scripts that are in a scope where placesOverlay.xul
 is included.
 
 chrome-worker
@@ -50,16 +51,21 @@ avoid-nsISupportsString-preferences
 Rejects using getComplexValue and setComplexValue with nsISupportsString.
 
 balanced-listeners
 ------------------
 
 Checks that for every occurence of 'addEventListener' or 'on' there is an
 occurence of 'removeEventListener' or 'off' with the same event name.
 
+import-browser-window-globals
+-----------------------------
+
+For scripts included in browser-window, this will automatically inject the
+browser-window global scopes into the file.
 
 import-globals
 --------------
 
 Checks the filename of imported files e.g. ``Cu.import("some/path/Blah.jsm")``
 adds Blah to the global scope.
 
 Note: uses modules.json for a list of globals listed in each file.
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js
@@ -12,16 +12,17 @@ module.exports = {
   "parserOptions": {
     "ecmaVersion": 8
   },
   // When adding items to this file please check for effects on all of toolkit
   // and browser
   "rules": {
     "mozilla/avoid-removeChild": "error",
     "mozilla/avoid-nsISupportsString-preferences": "error",
+    "mozilla/import-browser-window-globals": "error",
     "mozilla/import-globals": "error",
     "mozilla/no-import-into-var-and-global": "error",
     "mozilla/no-useless-parameters": "error",
     "mozilla/no-useless-removeEventListener": "error",
     "mozilla/use-default-preference-values": "error",
     "mozilla/use-ownerGlobal": "error",
 
     // Braces only needed for multi-line arrow function blocks
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/browser-window.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/browser-window.js
@@ -12,16 +12,17 @@
 // -----------------------------------------------------------------------------
 // Rule Definition
 // -----------------------------------------------------------------------------
 
 var fs = require("fs");
 var path = require("path");
 var helpers = require("../helpers");
 var globals = require("../globals");
+var placesGlobals = require("./places-overlay").globals;
 
 const rootDir = helpers.getRootDir(module.filename);
 
 // These are scripts not included in global-scripts.inc, but which are loaded
 // via overlays.
 const EXTRA_SCRIPTS = [
   "browser/base/content/nsContextMenu.js",
   "toolkit/content/contentAreaUtils.js",
@@ -103,17 +104,19 @@ function getScriptGlobals() {
       throw new Error(`Could not load globals from file ${fileName}: ${e}`);
     }
   }
 
   return fileGlobals;
 }
 
 function mapGlobals(fileGlobals) {
-  var globalObjects = {};
+  // placesOverlay.xul is also included in the browser scope, so include
+  // those globals here.
+  var globalObjects = Object.assign({}, placesGlobals);
   for (let global of fileGlobals) {
     globalObjects[global.name] = global.writable;
   }
   return globalObjects;
 }
 
 module.exports = {
   globals: mapGlobals(getScriptGlobals()),
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/index.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/index.js
@@ -29,16 +29,18 @@ module.exports = {
   processors: {
     ".xml": require("../lib/processors/xbl-bindings")
   },
   rules: {
     "avoid-removeChild": require("../lib/rules/avoid-removeChild"),
     "avoid-nsISupportsString-preferences":
       require("../lib/rules/avoid-nsISupportsString-preferences"),
     "balanced-listeners": require("../lib/rules/balanced-listeners"),
+    "import-browser-window-globals":
+      require("../lib/rules/import-browser-window-globals"),
     "import-globals": require("../lib/rules/import-globals"),
     "import-headjs-globals": require("../lib/rules/import-headjs-globals"),
     "mark-test-function-used": require("../lib/rules/mark-test-function-used"),
     "no-aArgs": require("../lib/rules/no-aArgs"),
     "no-cpows-in-tests": require("../lib/rules/no-cpows-in-tests"),
     "no-single-arg-cu-import": require("../lib/rules/no-single-arg-cu-import"),
     "no-import-into-var-and-global":
       require("../lib/rules/no-import-into-var-and-global.js"),
@@ -52,16 +54,17 @@ module.exports = {
       require("../lib/rules/use-default-preference-values"),
     "use-ownerGlobal": require("../lib/rules/use-ownerGlobal"),
     "var-only-at-top-level": require("../lib/rules/var-only-at-top-level")
   },
   rulesConfig: {
     "avoid-removeChild": "off",
     "avoid-nsISupportsString-preferences": "off",
     "balanced-listeners": "off",
+    "import-browser-window-globals": "off",
     "import-globals": "off",
     "import-headjs-globals": "off",
     "mark-test-function-used": "off",
     "no-aArgs": "off",
     "no-cpows-in-tests": "off",
     "no-single-arg-cu-import": "off",
     "no-import-into-var-and-global": "off",
     "no-useless-parameters": "off",
new file mode 100644
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-browser-window-globals.js
@@ -0,0 +1,40 @@
+/**
+ * @fileoverview For scripts included in browser-window, this will automatically
+ *               inject the browser-window global scopes into the file.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+"use strict";
+
+// -----------------------------------------------------------------------------
+// Rule Definition
+// -----------------------------------------------------------------------------
+
+var path = require("path");
+var helpers = require("../helpers");
+var globals = require("../globals");
+var browserWindowEnv = require("../environments/browser-window");
+
+module.exports = function(context) {
+  // ---------------------------------------------------------------------------
+  // Public
+  // ---------------------------------------------------------------------------
+
+  return {
+    Program: function(node) {
+      let filePath = helpers.getAbsoluteFilePath(context);
+      let relativePath = path.relative(helpers.getRootDir(filePath), filePath);
+
+      if (browserWindowEnv.browserjsScripts &&
+          browserWindowEnv.browserjsScripts.includes(relativePath)) {
+        for (let global in browserWindowEnv.globals) {
+          helpers.addVarToScope(global, context.getScope(),
+                                browserWindowEnv.globals[global]);
+        }
+      }
+    }
+  };
+};
--- a/tools/lint/eslint/eslint-plugin-mozilla/package.json
+++ b/tools/lint/eslint/eslint-plugin-mozilla/package.json
@@ -1,11 +1,11 @@
 {
   "name": "eslint-plugin-mozilla",
-  "version": "0.2.34",
+  "version": "0.2.35",
   "description": "A collection of rules that help enforce JavaScript coding standard in the Mozilla project.",
   "keywords": [
     "eslint",
     "eslintplugin",
     "eslint-plugin",
     "mozilla",
     "firefox"
   ],