Bug 1352969 - Improve ESLint environment browser-window globals list, and automatically inject it into browser-window scripts. r=mossop
authorMark Banner <standard8@mozilla.com>
Thu, 09 Mar 2017 17:17:26 +0000
changeset 351176 a7d64bc0d9749e65ac63874a5196ed7432d49a0e
parent 351175 23b17c071b03be263fe9c1dcc66356be6895c089
child 351177 9fee9ccec3c996cfbb4bea5f7645dc72425f5acf
push id31603
push usercbook@mozilla.com
push dateWed, 05 Apr 2017 12:30:29 +0000
treeherdermozilla-central@c7e37c13ea9e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmossop
bugs1352969
milestone55.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 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"
   ],