Bug 1415483 - Extend reject-importGlobalProperties to reject any priviliged items already in scope. r=mossop
authorMark Banner <standard8@mozilla.com>
Tue, 11 Dec 2018 21:39:38 +0000
changeset 450150 85f261cb71cf9f9fb36d20a791e5243c99330cb9
parent 450149 985505cc1347efa4cc451255e976f02d561efe7c
child 450151 2d59d8c885a3d8d6fd704b989a418ada8521fce2
push id35190
push userccoroiu@mozilla.com
push dateWed, 12 Dec 2018 05:10:47 +0000
treeherdermozilla-central@5bcbe5232a26 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmossop
bugs1415483
milestone66.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 1415483 - Extend reject-importGlobalProperties to reject any priviliged items already in scope. r=mossop Differential Revision: https://phabricator.services.mozilla.com/D13753
accessible/tests/browser/.eslintrc.js
devtools/.eslintrc.js
testing/talos/talos/tests/devtools/addon/content/tests/.eslintrc.js
toolkit/components/narrate/.eslintrc.js
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-importGlobalProperties.js
tools/lint/eslint/eslint-plugin-mozilla/tests/reject-importGlobalProperties.js
--- a/accessible/tests/browser/.eslintrc.js
+++ b/accessible/tests/browser/.eslintrc.js
@@ -4,17 +4,17 @@ module.exports = {
   "extends": [
     "plugin:mozilla/browser-test"
   ],
   "globals": {
     "gBrowser": false
   },
   "rules": {
     "mozilla/no-aArgs": "error",
-    "mozilla/reject-importGlobalProperties": "error",
+    "mozilla/reject-importGlobalProperties": ["error", "everything"],
     "mozilla/var-only-at-top-level": "error",
 
     "block-scoped-var": "error",
     "camelcase": "error",
     "comma-dangle": ["error", "never"],
     "complexity": ["error", 20],
     "consistent-this": "off",
     "curly": ["error", "multi-line"],
--- a/devtools/.eslintrc.js
+++ b/devtools/.eslintrc.js
@@ -175,17 +175,17 @@ module.exports = {
   "rules": {
     // These are the rules that have been configured so far to match the
     // devtools coding style.
 
     // Rules from the mozilla plugin
     "mozilla/no-aArgs": "error",
     "mozilla/no-single-arg-cu-import": "error",
     // See bug 1224289.
-    "mozilla/reject-importGlobalProperties": "error",
+    "mozilla/reject-importGlobalProperties": ["error", "everything"],
     // devtools/shared/platform is special; see the README.md in that
     // directory for details.  We reject requires using explicit
     // subdirectories of this directory.
     "mozilla/reject-some-requires": ["error", "^devtools/shared/platform/(chome|content)/"],
     "mozilla/var-only-at-top-level": "error",
     "mozilla/use-chromeutils-import": ["error", {allowCu: true}],
 
     // Rules from the React plugin
--- a/testing/talos/talos/tests/devtools/addon/content/tests/.eslintrc.js
+++ b/testing/talos/talos/tests/devtools/addon/content/tests/.eslintrc.js
@@ -16,17 +16,17 @@ module.exports = {
     "no-unused-vars": ["error", {"args": "none", "vars": "all"}],
     // These are the rules that have been configured so far to match the
     // devtools coding style.
 
     // Rules from the mozilla plugin
     "mozilla/no-aArgs": "error",
     "mozilla/no-single-arg-cu-import": "error",
     // See bug 1224289.
-    "mozilla/reject-importGlobalProperties": "error",
+    "mozilla/reject-importGlobalProperties": ["error", "everything"],
     // devtools/shared/platform is special; see the README.md in that
     // directory for details.  We reject requires using explicit
     // subdirectories of this directory.
     "mozilla/reject-some-requires": ["error", "^devtools/shared/platform/(chome|content)/"],
     "mozilla/var-only-at-top-level": "error",
     "mozilla/use-chromeutils-import": ["error", {allowCu: true}]
   }
 };
--- a/toolkit/components/narrate/.eslintrc.js
+++ b/toolkit/components/narrate/.eslintrc.js
@@ -1,14 +1,14 @@
 "use strict";
 
 module.exports = {
   "rules": {
     "mozilla/no-aArgs": "error",
-    "mozilla/reject-importGlobalProperties": "error",
+    "mozilla/reject-importGlobalProperties": ["error", "everything"],
     "mozilla/var-only-at-top-level": "error",
     "block-scoped-var": "error",
     "camelcase": "error",
     "complexity": ["error", {"max": 20}],
     "curly": "error",
     "dot-location": ["error", "property"],
     "indent-legacy": ["error", 2, {"SwitchCase": 1}],
     "max-len": ["error", 80, 2, {"ignoreUrls": true}],
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-importGlobalProperties.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-importGlobalProperties.js
@@ -3,33 +3,53 @@
  *
  * 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";
 
+const privilegedGlobals = Object.keys(require("../environments/privileged.js").globals);
+
 // -----------------------------------------------------------------------------
 // Rule Definition
 // -----------------------------------------------------------------------------
 
-module.exports = function(context) {
+module.exports = {
+  meta: {
+    messages: {
+      unexpectedCall: "Unexpected call to Cu.importGlobalProperties",
+      unexpectedCallWebIdl: "Unnecessary call to Cu.importGlobalProperties (webidl names are automatically imported)",
+    },
+    schema: [{
+      // XXX Better name?
+      "enum": ["everything", "allownonwebidl"],
+    }],
+    type: "problem",
+  },
 
-  // ---------------------------------------------------------------------------
-  // Public
-  //  --------------------------------------------------------------------------
-
-  return {
-    "CallExpression": function(node) {
-      if (node.callee.type === "MemberExpression") {
+  create(context) {
+    return {
+      "CallExpression": function(node) {
+        if (node.callee.type !== "MemberExpression") {
+          return;
+        }
         let memexp = node.callee;
         if (memexp.object.type === "Identifier" &&
             // Only Cu, not Components.utils; see bug 1230369.
             memexp.object.name === "Cu" &&
             memexp.property.type === "Identifier" &&
             memexp.property.name === "importGlobalProperties") {
-          context.report(node, "Unexpected call to Cu.importGlobalProperties");
+          if (context.options.includes("allownonwebidl")) {
+            for (let element of node.arguments[0].elements) {
+              if (privilegedGlobals.includes(element.value)) {
+                context.report({ node, messageId: "unexpectedCallWebIdl"});
+              }
+            }
+          } else {
+            context.report({node, messageId: "unexpectedCall"});
+          }
         }
-      }
-    },
-  };
+      },
+    };
+  },
 };
new file mode 100644
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/tests/reject-importGlobalProperties.js
@@ -0,0 +1,39 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+// ------------------------------------------------------------------------------
+// Requirements
+// ------------------------------------------------------------------------------
+
+var rule = require("../lib/rules/reject-importGlobalProperties");
+var RuleTester = require("eslint/lib/testers/rule-tester");
+
+const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 8 } });
+
+// ------------------------------------------------------------------------------
+// Tests
+// ------------------------------------------------------------------------------
+
+ruleTester.run("reject-importGlobalProperties", rule, {
+  valid: [{
+    code: "Cu.something();",
+  }, {
+    options: ["allownonwebidl"],
+    code: "Cu.importGlobalProperties(['fetch'])",
+  }],
+  invalid: [{
+    code: "Cu.importGlobalProperties(['fetch'])",
+    options: ["everything"],
+    errors: [{ messageId: "unexpectedCall"}],
+  }, {
+    code: "Cu.importGlobalProperties(['TextEncoder'])",
+    options: ["everything"],
+    errors: [{ messageId: "unexpectedCall"}],
+  }, {
+    code: "Cu.importGlobalProperties(['TextEncoder'])",
+    options: ["allownonwebidl"],
+    errors: [{ messageId: "unexpectedCallWebIdl"}],
+  }],
+});