summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-cpows-in-tests.js
blob: 415cb2fc98e8af8ccbc5640dc84ac244b3a361cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * @fileoverview Prevent access to CPOWs in browser mochitests.
 *
 * 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 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.getTestType(this) != "browser") {
        return;
      }

      var expression = context.getSource(node);

      // Only report a single CPOW error per node -- so if checking
      // |cpows| reports one, don't report another below.
      var someCpowFound = cpows.some(function(cpow) {
        if (cpow.test(expression)) {
          showError(node, expression);
          return true;
        }
        return false;
      });
      if (!someCpowFound && helpers.getIsGlobalScope(context.getAncestors())) {
        if (/^content\./.test(expression)) {
          showError(node, expression);
          return;
        }
      }
    },

    Identifier: function(node) {
      if (helpers.getTestType(this) != "browser") {
        return;
      }

      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;
      }
    }
  };
};