Bug 1415483 - Extend reject-importGlobalProperties to reject any priviliged items already in scope. r=mossop
☠☠ backed out by 546d25793dae ☠ ☠
authorMark Banner <standard8@mozilla.com>
Tue, 11 Dec 2018 14:09:24 +0000
changeset 449804 8dea94258f54d938088a1da31d14376aac8e8db7
parent 449803 fee0a53015fbc579d68f621111cf9883e1d7b5b0
child 449805 d00748de66fcf465e872d412c4b974aeff038605
push id74492
push usermbanner@mozilla.com
push dateTue, 11 Dec 2018 14:20:16 +0000
treeherderautoland@d00748de66fc [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"}],
+  }],
+});