Bug 1264063 - 2 - Make the CPOW rule log errors and ignore ContentTask.spawn; r=Mossop draft
authorPatrick Brosset <pbrosset@mozilla.com>
Wed, 13 Apr 2016 11:05:04 +0200
changeset 350454 099489c97f2b0fcae3903cbdd06d43b84e4113b1
parent 350453 bba6594effdccba983c33cad79471ffca76c6938
child 518345 5a2e55d53a158270e9435aa848bc45f6cc688198
push id15353
push userpbrosset@mozilla.com
push dateWed, 13 Apr 2016 20:00:37 +0000
reviewersMossop
bugs1264063
milestone48.0a1
Bug 1264063 - 2 - Make the CPOW rule log errors and ignore ContentTask.spawn; r=Mossop MozReview-Commit-ID: KN2MAgAvX2c
devtools/.eslintrc
devtools/client/responsive.html/test/browser/browser_viewport_basics.js
testing/eslint-plugin-mozilla/lib/rules/no-cpows-in-tests.js
--- a/devtools/.eslintrc
+++ b/devtools/.eslintrc
@@ -28,17 +28,17 @@
   },
   "rules": {
     // These are the rules that have been configured so far to match the
     // devtools coding style.
 
     // Rules from the mozilla plugin
     "mozilla/mark-test-function-used": 1,
     "mozilla/no-aArgs": 1,
-    "mozilla/no-cpows-in-tests": 1,
+    "mozilla/no-cpows-in-tests": 2,
     // See bug 1224289.
     "mozilla/reject-importGlobalProperties": 1,
     "mozilla/var-only-at-top-level": 1,
 
     // Rules from the React plugin
     "react/display-name": 2,
     "react/no-danger": 2,
     "react/no-did-mount-set-state": 2,
--- a/devtools/client/responsive.html/test/browser/browser_viewport_basics.js
+++ b/devtools/client/responsive.html/test/browser/browser_viewport_basics.js
@@ -19,12 +19,12 @@ addRDMTask(TEST_URL, function* ({ ui }) 
   is(ui.toolWindow.getComputedStyle(viewport).getPropertyValue("width"),
      "320px", "Viewport has default width");
   is(ui.toolWindow.getComputedStyle(viewport).getPropertyValue("height"),
      "480px", "Viewport has default height");
 
   // Browser's location should match original tab
   yield waitForFrameLoad(ui, TEST_URL);
   let location = yield spawnViewportTask(ui, {}, function* () {
-    return content.location.href;
+    return content.location.href; // eslint-disable-line
   });
   is(location, TEST_URL, "Viewport location matches");
 });
--- a/testing/eslint-plugin-mozilla/lib/rules/no-cpows-in-tests.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/no-cpows-in-tests.js
@@ -17,34 +17,61 @@ var helpers = require("../helpers");
 var cpows = [
   /^gBrowser\.contentWindow/,
   /^gBrowser\.contentDocument/,
   /^gBrowser\.selectedBrowser.contentWindow/,
   /^browser\.contentDocument/,
   /^window\.content/
 ];
 
+var isInContentTask = false;
+
 module.exports = function(context) {
   // ---------------------------------------------------------------------------
   // Helpers
   // ---------------------------------------------------------------------------
 
   function showError(node, identifier) {
+    if (isInContentTask) {
+      return;
+    }
+
     context.report({
       node: node,
       message: identifier +
                " is a possible Cross Process Object Wrapper (CPOW)."
     });
   }
 
+  function isContentTask(node) {
+    return node &&
+           node.type === "MemberExpression" &&
+           node.property.type === "Identifier" &&
+           node.property.name === "spawn" &&
+           node.object.type === "Identifier" &&
+           node.object.name === "ContentTask";
+  }
+
   // ---------------------------------------------------------------------------
   // Public
   // ---------------------------------------------------------------------------
 
   return {
+    CallExpression: function(node) {
+      if (isContentTask(node.callee)) {
+        isInContentTask = true;
+      }
+    },
+
+    "CallExpression:exit": function(node) {
+      if (isContentTask(node.callee)) {
+        isInContentTask = false;
+      }
+    },
+
     MemberExpression: function(node) {
       if (!helpers.getIsBrowserMochitest(this)) {
         return;
       }
 
       var expression = context.getSource(node);
 
       // Only report a single CPOW error per node -- so if checking
@@ -72,15 +99,14 @@ module.exports = function(context) {
       var expression = context.getSource(node);
       if (expression == "content" || /^content\./.test(expression)) {
         if (node.parent.type === "MemberExpression" &&
             node.parent.object &&
             node.parent.object.type === "Identifier" &&
             node.parent.object.name != "content") {
           return;
         }
-
         showError(node, expression);
         return;
       }
     }
   };
 };