Bug 1343519 - Change the ESLint browser-window environment to determine most of the scripts for globals by parsing global-scripts.inc. r=mossop
authorMark Banner <standard8@mozilla.com>
Wed, 01 Mar 2017 21:44:45 +0000
changeset 394490 72b573571f722d34fc7e91421f19884f5afd7718
parent 394489 8dbddf1d169e67ed97ea852013bbac2269d01aaa
child 394491 180a160ae22a4d63867cfd02608e7621e8e697d1
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmossop
bugs1343519
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 1343519 - Change the ESLint browser-window environment to determine most of the scripts for globals by parsing global-scripts.inc. r=mossop MozReview-Commit-ID: 6rxN3ORgItH
tools/lint/eslint/eslint-plugin-mozilla/lib/environments/browser-window.js
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/browser-window.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/browser-window.js
@@ -8,67 +8,86 @@
  */
 
 "use strict";
 
 // -----------------------------------------------------------------------------
 // Rule Definition
 // -----------------------------------------------------------------------------
 
+var fs = require("fs");
 var path = require("path");
 var helpers = require("../helpers");
 var globals = require("../globals");
 
-const SCRIPTS = [
+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",
   "browser/components/places/content/editBookmarkOverlay.js",
-  "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/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-feeds.js",
-  "browser/base/content/browser-fullScreenAndPointerLock.js",
-  "browser/base/content/browser-fullZoom.js",
-  "browser/base/content/browser-gestureSupport.js",
-  "browser/base/content/browser-media.js",
-  "browser/base/content/browser-places.js",
-  "browser/base/content/browser-plugins.js",
-  "browser/base/content/browser-refreshblocker.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",
   // This gets loaded into the same scopes as browser.js via browser.xul and
   // placesOverlay.xul.
   "toolkit/content/globalOverlay.js",
   // Via editMenuOverlay.xul
   "toolkit/content/editMenuOverlay.js"
 ];
 
+// Some files in global-scripts.inc need mapping to specific locations.
+const MAPPINGS = {
+  "printUtils.js": "toolkit/components/printing/content/printUtils.js",
+  "browserPlacesViews.js": "browser/components/places/content/browserPlacesViews.js",
+  "panelUI.js": "browser/components/customizableui/content/panelUI.js",
+  "viewSourceUtils.js": "toolkit/components/viewsource/content/viewSourceUtils.js",
+};
+
+const globalScriptsRegExp = /<script type=\"application\/javascript\" src=\"(.*)\"\/>/;
+
+function getGlobalScriptsIncludes() {
+  let globalScriptsPath = path.join(rootDir, "browser", "base", "content",
+                                    "global-scripts.inc");
+  let fileData = fs.readFileSync(globalScriptsPath, {encoding: "utf8"});
+
+  fileData = fileData.split("\n");
+
+  let result = [];
+
+  for (let line of fileData) {
+    let match = line.match(globalScriptsRegExp);
+    if (match) {
+      let sourceFile = match[1].replace("chrome://browser/content/", "browser/base/content/")
+                               .replace("chrome://global/content/", "toolkit/content/");
+
+      for (let mapping of Object.getOwnPropertyNames(MAPPINGS)) {
+        if (sourceFile.includes(mapping)) {
+          sourceFile = MAPPINGS[mapping];
+        }
+      }
+
+      result.push(sourceFile);
+    }
+  }
+
+  return result;
+}
+
 function getScriptGlobals() {
   let fileGlobals = [];
-  let root = helpers.getRootDir(module.filename);
-  for (let script of SCRIPTS) {
-    let fileName = path.join(root, script);
+  var scripts = EXTRA_SCRIPTS.concat(getGlobalScriptsIncludes());
+  for (let script of scripts) {
+    let fileName = path.join(rootDir, script);
     try {
       fileGlobals = fileGlobals.concat(globals.getGlobalsForFile(fileName));
     } catch (e) {
+      console.error(`Could not load globals from file ${fileName}: ${e}`);
+      console.error(`You may need to update the mappings in ${module.filename}`);
       throw new Error(`Could not load globals from file ${fileName}: ${e}`)
     }
   }
 
   return fileGlobals;
 }
 
 function mapGlobals(fileGlobals) {