Bug 1229224: Add an eslint plugin for importing all browser.js globals for browser-chrome tests. r=miker draft
authorDave Townsend <dtownsend@oxymoronical.com>
Thu, 17 Dec 2015 15:33:32 -0800
changeset 316389 08a1eaa364748ac9209f73e4aab4db0b53452265
parent 316388 6a2a03d21d02331ae336c6b363d3951f4b6110aa
child 316390 a29625ec60a873b46b3a15758111a0bb0b54b7d2
push id8536
push userdtownsend@mozilla.com
push dateFri, 18 Dec 2015 16:57:50 +0000
reviewersmiker
bugs1229224
milestone46.0a1
Bug 1229224: Add an eslint plugin for importing all browser.js globals for browser-chrome tests. r=miker
testing/eslint-plugin-mozilla/docs/import-browserjs-globals.rst
testing/eslint-plugin-mozilla/lib/index.js
testing/eslint-plugin-mozilla/lib/rules/import-browserjs-globals.js
new file mode 100644
--- /dev/null
+++ b/testing/eslint-plugin-mozilla/docs/import-browserjs-globals.rst
@@ -0,0 +1,12 @@
+.. _import-browserjs-globals:
+
+========================
+import-browserjs-globals
+========================
+
+Rule Details
+------------
+
+When included files from the main browser UI scripts will be loaded and any
+declared globals will be defined for the current file. This is mostly useful for
+browser-chrome mochitests that call browser functions.
--- a/testing/eslint-plugin-mozilla/lib/index.js
+++ b/testing/eslint-plugin-mozilla/lib/index.js
@@ -16,16 +16,17 @@ module.exports = {
   processors: {
     ".xml": require("../lib/processors/xbl-bindings"),
   },
   rules: {
     "balanced-listeners": require("../lib/rules/balanced-listeners"),
     "components-imports": require("../lib/rules/components-imports"),
     "import-globals-from": require("../lib/rules/import-globals-from"),
     "import-headjs-globals": require("../lib/rules/import-headjs-globals"),
+    "import-browserjs-globals": require("../lib/rules/import-browserjs-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"),
     "var-only-at-top-level": require("../lib/rules/var-only-at-top-level")
   },
   rulesConfig: {
     "balanced-listeners": 0,
     "components-imports": 0,
new file mode 100644
--- /dev/null
+++ b/testing/eslint-plugin-mozilla/lib/rules/import-browserjs-globals.js
@@ -0,0 +1,78 @@
+/**
+ * @fileoverview Import globals from browser.js.
+ *
+ * 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 fs = require("fs");
+var path = require("path");
+var helpers = require("../helpers");
+
+const SCRIPTS = [
+  "toolkit/components/printing/content/printUtils.js",
+  "toolkit/content/viewZoomOverlay.js",
+  "browser/components/places/content/browserPlacesViews.js",
+  "browser/base/content/browser.js",
+  "browser/components/downloads/content/downloads.js",
+  "browser/components/downloads/content/indicator.js",
+  "browser/components/customizableui/content/panelUI.js",
+  "toolkit/obsolete/content/inlineSpellCheckUI.js",
+  "toolkit/components/viewsource/content/viewSourceUtils.js",
+  "browser/base/content/browser-addons.js",
+  "browser/base/content/browser-ctrlTab.js",
+  "browser/base/content/browser-customization.js",
+  "browser/base/content/browser-devedition.js",
+  "browser/base/content/browser-eme.js",
+  "browser/base/content/browser-feeds.js",
+  "browser/base/content/browser-fullScreen.js",
+  "browser/base/content/browser-fullZoom.js",
+  "browser/base/content/browser-gestureSupport.js",
+  "browser/base/content/browser-places.js",
+  "browser/base/content/browser-plugins.js",
+  "browser/base/content/browser-safebrowsing.js",
+  "browser/base/content/browser-sidebar.js",
+  "browser/base/content/browser-social.js",
+  "browser/base/content/browser-syncui.js",
+  "browser/base/content/browser-tabsintitlebar.js",
+  "browser/base/content/browser-thumbnails.js",
+  "browser/base/content/browser-trackingprotection.js",
+  "browser/base/content/browser-data-submission-info-bar.js",
+  "browser/base/content/browser-fxaccounts.js",
+];
+
+module.exports = function(context) {
+  return {
+    Program: function(node) {
+      if (!helpers.getIsBrowserMochitest(this)) {
+        return;
+      }
+
+      let root = helpers.getRootDir(context);
+      for (let script of SCRIPTS) {
+        let fileName = path.join(root, script);
+        try {
+          let globals = helpers.getGlobalsForFile(fileName);
+          helpers.addGlobals(globals, context);
+        }
+        catch (e) {
+          context.report(
+            node,
+            "Could not load globals from file {{filePath}}: {{error}}",
+            {
+              filePath: path.relative(root, fileName),
+              error: e
+            }
+          );
+        }
+      }
+    }
+  };
+};