Bug 1431069 - allow eslint to deal with ChromeUtils imports, r?Standard8 draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Wed, 17 Jan 2018 13:20:59 +0000
changeset 722183 2d15d1cf0ef95df0728b45a47ccfdcd52f271f7f
parent 722069 abbe0ca5d9693e06484fcb52bbcac189c3461f50
child 746548 bdbeaff94031b7f130fca723bcfe7ac0a8fc52f0
push id96077
push usergijskruitbosch@gmail.com
push dateThu, 18 Jan 2018 15:55:24 +0000
reviewersStandard8
bugs1431069
milestone59.0a1
Bug 1431069 - allow eslint to deal with ChromeUtils imports, r?Standard8 MozReview-Commit-ID: KHVewE1Rrov
tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-import-into-var-and-global.js
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-single-arg-cu-import.js
tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-import-into-var-and-global.js
tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-single-arg-cu-import.js
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
@@ -35,17 +35,17 @@ const callExpressionDefinitions = [
 ];
 
 const callExpressionMultiDefinitions = [
   "XPCOMUtils.defineLazyModuleGetters(this,",
   "XPCOMUtils.defineLazyServiceGetters(this,"
 ];
 
 const imports = [
-  /^(?:Cu|Components\.utils)\.import\(".*\/((.*?)\.jsm?)"(?:, this)?\)/
+  /^(?:Cu|Components\.utils|ChromeUtils)\.import\(".*\/((.*?)\.jsm?)"(?:, this)?\)/
 ];
 
 const workerImportFilenameMatch = /(.*\/)*(.*?\.jsm?)/;
 
 module.exports = {
   get modulesGlobalData() {
     if (!gModules) {
       if (this.isMozillaCentralBased()) {
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-import-into-var-and-global.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-import-into-var-and-global.js
@@ -30,17 +30,17 @@ module.exports = function(context) {
       if (node.callee.type === "MemberExpression" &&
           node.parent.type === "VariableDeclarator" &&
           node.arguments.length <= 2) {
         let memexp = node.callee;
 
         // Is the expression starting with `Cu` or `Components.utils`?
         let isACu =
           ((memexp.object.type === "Identifier" &&
-            memexp.object.name === "Cu") ||
+            (memexp.object.name === "Cu" || memexp.object.name === "ChromeUtils")) ||
            (memexp.object.type === "MemberExpression" &&
             memexp.object.object && memexp.object.property &&
             memexp.object.object.name === "Components" &&
             memexp.object.property.name === "utils"));
 
         if (isACu &&
             // Now check its `Cu.import` (or `Components.utils.import`).
             memexp.property.type === "Identifier" &&
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-single-arg-cu-import.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-single-arg-cu-import.js
@@ -18,18 +18,19 @@ module.exports = function(context) {
   // Public
   //  --------------------------------------------------------------------------
 
   return {
     "CallExpression": function(node) {
       if (node.callee.type === "MemberExpression") {
         let memexp = node.callee;
         if (memexp.object.type === "Identifier" &&
-            // Only Cu, not Components.utils; see bug 1230369.
-            memexp.object.name === "Cu" &&
+            // Only Cu and ChromeUtils, not Components.utils; see bug 1230369.
+            (memexp.object.name === "Cu" ||
+             memexp.object.name === "ChromeUtils") &&
             memexp.property.type === "Identifier" &&
             memexp.property.name === "import" &&
             node.arguments.length === 1) {
           context.report(node, "Single argument Cu.import exposes new " +
                                "globals to all modules");
         }
       }
     }
--- a/tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-import-into-var-and-global.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-import-into-var-and-global.js
@@ -27,10 +27,16 @@ ruleTester.run("no-import-into-var-and-g
     "var foo = Components.utils.import('fake', {});"
   ],
   invalid: [{
     code: "var foo = Cu.import('fake', this);",
     errors: [ExpectedError]
   }, {
     code: "var foo = Cu.import('fake');",
     errors: [ExpectedError]
+  }, {
+    code: "var foo = Components.utils.import('fake');",
+    errors: [ExpectedError]
+  }, {
+    code: "var foo = ChromeUtils.import('fake');",
+    errors: [ExpectedError]
   }]
 });
--- a/tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-single-arg-cu-import.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-single-arg-cu-import.js
@@ -23,10 +23,13 @@ const ExpectedError = {
 
 ruleTester.run("no-single-arg-cu-import", rule, {
   valid: [
     "Cu.import('fake', {});"
   ],
   invalid: [{
     code: "Cu.import('fake');",
     errors: [ExpectedError]
+  }, {
+    code: "ChromeUtils.import('fake');",
+    errors: [ExpectedError]
   }]
 });