Bug 1344027 - Add a places-overlay environment for ESLint for when placesOverlay.xul is included. r=mossop
authorMark Banner <standard8@mozilla.com>
Fri, 03 Mar 2017 09:25:31 +0000
changeset 345958 bdd4e1f734a272b61b7a5209c47e61b25fa9baf7
parent 345957 b5cf23ffc1d084f319f5b3f19004b6ac3454f195
child 345959 feea1e54c19bf38897657c5b73458cc48e3f2483
push id31451
push usercbook@mozilla.com
push dateMon, 06 Mar 2017 09:52:09 +0000
treeherdermozilla-central@7099e03837e8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmossop
bugs1344027
milestone54.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 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"
   ],