Bug 1344027 - Add a places-overlay environment for ESLint for when placesOverlay.xul is included. r?Mossop draft
authorMark Banner <standard8@mozilla.com>
Fri, 03 Mar 2017 09:25:31 +0000
changeset 493729 ac3c69144d59720e9b828a3ba77ef2f919eb3015
parent 493728 3ff400d4a7a299cde4e5d228e58d4e044d64189b
child 493730 d68679dfb80c2ed119ea38017f263590ea163c94
push id47828
push userbmo:standard8@mozilla.com
push dateSun, 05 Mar 2017 19:41:37 +0000
reviewersMossop
bugs1344027
milestone54.0a1
Bug 1344027 - Add a places-overlay environment for ESLint for when placesOverlay.xul is included. r?Mossop MozReview-Commit-ID: 3uoneyJdEmx
tools/lint/docs/linters/eslint-plugin-mozilla.rst
tools/lint/eslint/eslint-plugin-mozilla/lib/environments/places-overlay.js
tools/lint/eslint/eslint-plugin-mozilla/lib/index.js
tools/lint/eslint/eslint-plugin-mozilla/package.json
--- a/tools/lint/docs/linters/eslint-plugin-mozilla.rst
+++ b/tools/lint/docs/linters/eslint-plugin-mozilla.rst
@@ -13,16 +13,22 @@ e.g.
 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.
 
+places-overlay
+--------------
+
+Defines the environment for scripts that are in a scope where placesOverlay.xul
+is included.
+
 chrome-worker
 -------------
 
 Defines the environment for chrome workers. This differs from normal workers by
 the fact that `ctypes` can be accessed as well.
 
 frame-script
 ------------
new file mode 100644
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/places-overlay.js
@@ -0,0 +1,79 @@
+/**
+ * @fileoverview Defines the environment when placesOverlay.xul is used
+ *               as an overlay alongside scripts. Imports the globals
+ *               from the relevant files.
+ *
+ * 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 root = helpers.getRootDir(module.filename);
+var modules = require(path.join(root, "tools", "lint", "eslint", "modules.json"));
+
+const placesOverlayFiles = [
+  "toolkit/content/globalOverlay.js",
+  "browser/base/content/utilityOverlay.js",
+  "browser/components/places/content/controller.js",
+  "browser/components/places/content/treeView.js",
+];
+
+const extraPlacesDefinitions = [
+  // Straight definitions.
+  {name: "Cc", writable: false},
+  {name: "Ci", writable: false},
+  {name: "Cr", writable: false},
+  {name: "Cu", writable: false},
+  // Via Components.utils / XPCOMUtils.defineLazyModuleGetter (and map to single)
+  // variable.
+  {name: "XPCOMUtils", writable: false},
+  {name: "Task", writable: false},
+  {name: "PlacesUIUtils", writable: false},
+  {name: "PlacesTransactions", writable: false},
+]
+
+const placesOverlayModules = [
+  "PlacesUtils.jsm",
+]
+
+function getScriptGlobals() {
+  let fileGlobals = [];
+  for (let file of placesOverlayFiles) {
+    let fileName = path.join(root, file)
+    try {
+      fileGlobals = fileGlobals.concat(globals.getGlobalsForFile(fileName));
+    } catch (e) {
+      throw new Error(`Could not load globals from file ${fileName}: ${e}`)
+    }
+  }
+
+  for (let file of placesOverlayModules) {
+    if (file in modules) {
+      for (let globalVar of modules[file]) {
+        fileGlobals.push({name: globalVar, writable: false});
+      }
+    }
+  }
+
+  return fileGlobals.concat(extraPlacesDefinitions);
+}
+
+function mapGlobals(fileGlobals) {
+  var globalObjects = {};
+  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
@@ -11,16 +11,17 @@
 //------------------------------------------------------------------------------
 // Plugin Definition
 //------------------------------------------------------------------------------
 module.exports = {
   environments: {
     "browser-window": require("../lib/environments/browser-window.js"),
     "chrome-worker": require("../lib/environments/chrome-worker.js"),
     "frame-script": require("../lib/environments/frame-script.js"),
+    "places-overlay": require("../lib/environments/places-overlay.js"),
     "simpletest": require("../lib/environments/simpletest.js"),
   },
   processors: {
     ".xml": require("../lib/processors/xbl-bindings"),
     ".js": require("../lib/processors/self-hosted"),
   },
   rules: {
     "avoid-removeChild": require("../lib/rules/avoid-removeChild"),
--- 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.24",
+  "version": "0.2.25",
   "description": "A collection of rules that help enforce JavaScript coding standard in the Mozilla project.",
   "keywords": [
     "eslint",
     "eslintplugin",
     "eslint-plugin",
     "mozilla",
     "firefox"
   ],