Bug 1343519 - Change the ESLint rule 'import-test-globals' to be an environment (mozilla/simpletest) to better describe the purpose of it. r?Mossop draft
authorMark Banner <standard8@mozilla.com>
Wed, 01 Mar 2017 21:34:24 +0000
changeset 491207 9d0979e01c5991bd8b19fcd4a1a71fa52d0b29a6
parent 491206 d55fd6e8a3150f6bcb386433900f23dc4208d599
child 491208 830094084c1c6028c40ac99bb557895618c62a69
push id47342
push usermbanner@mozilla.com
push dateWed, 01 Mar 2017 22:33:48 +0000
reviewersMossop
bugs1343519
milestone54.0a1
Bug 1343519 - Change the ESLint rule 'import-test-globals' to be an environment (mozilla/simpletest) to better describe the purpose of it. r?Mossop MozReview-Commit-ID: DbrsnUVrAas
testing/mochitest/browser.eslintrc.js
testing/mochitest/chrome.eslintrc.js
testing/mochitest/mochitest.eslintrc.js
tools/lint/eslint/eslint-plugin-mozilla/lib/environments/simpletest.js
tools/lint/eslint/eslint-plugin-mozilla/lib/index.js
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-test-globals.js
--- a/testing/mochitest/browser.eslintrc.js
+++ b/testing/mochitest/browser.eslintrc.js
@@ -1,19 +1,19 @@
 // Parent config file for all browser-chrome files.
 module.exports = {
   "rules": {
     "mozilla/import-headjs-globals": "warn",
-    "mozilla/import-test-globals": "warn",
     "mozilla/mark-test-function-used": "warn",
   },
 
   "env": {
     "browser": true,
     "mozilla/browser-window": true,
+    "mozilla/simpletest": true,
     //"node": true
   },
 
   "plugins": [
     "mozilla"
   ],
 
   // All globals made available in the test environment.
--- a/testing/mochitest/chrome.eslintrc.js
+++ b/testing/mochitest/chrome.eslintrc.js
@@ -1,19 +1,19 @@
 // Parent config file for all mochitest files.
 module.exports = {
   rules: {
     "mozilla/import-headjs-globals": "warn",
-    "mozilla/import-test-globals": "warn",
     "mozilla/mark-test-function-used": "warn",
   },
 
   "env": {
     "browser": true,
     "mozilla/browser-window": true,
+    "mozilla/simpletest": true,
   },
 
   "plugins": [
     "mozilla"
   ],
 
   // All globals made available in the test environment.
   "globals": {
--- a/testing/mochitest/mochitest.eslintrc.js
+++ b/testing/mochitest/mochitest.eslintrc.js
@@ -1,19 +1,19 @@
 // Parent config file for all mochitest files.
 module.exports = {
   rules: {
     "mozilla/import-headjs-globals": "warn",
-    "mozilla/import-test-globals": "warn",
     "mozilla/mark-test-function-used": "warn",
     "no-shadow": "error",
   },
 
   "env": {
     "browser": true,
+    "mozilla/simpletest": true,
   },
 
   // All globals made available in the test environment.
   "globals": {
     // `$` is defined in SimpleTest.js
     "$": false,
     "add_task": false,
     "addLoadEvent": false,
rename from tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-test-globals.js
rename to tools/lint/eslint/eslint-plugin-mozilla/lib/environments/simpletest.js
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-test-globals.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/environments/simpletest.js
@@ -1,44 +1,53 @@
 /**
- * @fileoverview Import globals from common mochitest files, so that we
- * don't need to specify them individually.
+ * @fileoverview Defines the environment for scripts that use the SimpleTest
+ *               mochitest harness. 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 fs = require("fs");
 var path = require("path");
 var helpers = require("../helpers");
 var globals = require("../globals");
 
 const simpleTestFiles = [
   "EventUtils.js",
   "MockObjects.js",
   "SimpleTest.js",
   "WindowSnapshot.js"
 ];
 const simpleTestPath = "testing/mochitest/tests/SimpleTest";
 
-module.exports = function(context) {
-  // ---------------------------------------------------------------------------
-  // Public
-  // ---------------------------------------------------------------------------
+function getScriptGlobals() {
+  let fileGlobals = [];
+  let root = helpers.getRootDir(module.filename);
+  for (let file of simpleTestFiles) {
+    let fileName = path.join(root, simpleTestPath, file)
+    try {
+      fileGlobals = fileGlobals.concat(globals.getGlobalsForFile(fileName));
+    } catch (e) {
+      throw new Error(`Could not load globals from file ${fileName}: ${e}`)
+    }
+  }
 
-  return {
-    Program: function(node) {
-      let rootDir = helpers.getRootDir(context.getFilename());
-      for (let file of simpleTestFiles) {
-        let newGlobals =
-          globals.getGlobalsForFile(path.join(rootDir, simpleTestPath, file));
-        helpers.addGlobals(newGlobals, context.getScope());
-      }
-    }
-  };
+  return fileGlobals;
+}
+
+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,27 +11,27 @@
 //------------------------------------------------------------------------------
 // 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"),
+    "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"),
     "balanced-listeners": require("../lib/rules/balanced-listeners"),
     "import-globals": require("../lib/rules/import-globals"),
     "import-headjs-globals": require("../lib/rules/import-headjs-globals"),
-    "import-test-globals": require("../lib/rules/import-test-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"),
     "no-useless-parameters": require("../lib/rules/no-useless-parameters"),
     "no-useless-removeEventListener": require("../lib/rules/no-useless-removeEventListener"),
     "reject-importGlobalProperties": require("../lib/rules/reject-importGlobalProperties"),
@@ -39,17 +39,16 @@ module.exports = {
     "use-ownerGlobal": require("../lib/rules/use-ownerGlobal"),
     "var-only-at-top-level": require("../lib/rules/var-only-at-top-level")
   },
   rulesConfig: {
     "avoid-removeChild": 0,
     "balanced-listeners": 0,
     "import-globals": 0,
     "import-headjs-globals": 0,
-    "import-test-globals": 0,
     "mark-test-function-used": 0,
     "no-aArgs": 0,
     "no-cpows-in-tests": 0,
     "no-single-arg-cu-import": 0,
     "no-import-into-var-and-global": 0,
     "no-useless-parameters": 0,
     "no-useless-removeEventListener": 0,
     "reject-importGlobalProperties": 0,