summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/valid-services-property.js
blob: c3600fcb25282db9421fd7c1441a81672a235eca (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * @fileoverview Ensures that property accesses on Services.<alias> are valid.
 * Although this largely duplicates the valid-services rule, the checks here
 * require an objdir and a manual run.
 *
 * 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 helpers = require("../helpers");

function findInterfaceNames(name) {
  let interfaces = [];
  for (let [key, value] of Object.entries(helpers.servicesData)) {
    if (value == name) {
      interfaces.push(key);
    }
  }
  return interfaces;
}

function isInInterface(interfaceName, name) {
  let interfaceDetails = helpers.xpidlData.get(interfaceName);

  // TODO: Bug 1790261 - check only methods if the expression is callable.
  if (interfaceDetails.methods.some(m => m.name == name)) {
    return true;
  }

  if (interfaceDetails.consts.some(c => c.name == name)) {
    return true;
  }

  if (interfaceDetails.parent) {
    return isInInterface(interfaceDetails.parent, name);
  }
  return false;
}

module.exports = {
  meta: {
    docs: {
      url:
        "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/valid-services-property.html",
    },
    messages: {
      unknownProperty:
        "Unknown property access Services.{{ alias }}.{{ propertyName }}, Interfaces: {{ checkedInterfaces }}",
    },
    type: "problem",
  },

  create(context) {
    let servicesInterfaceMap = helpers.servicesData;
    let serviceAliases = new Set([
      ...Object.values(servicesInterfaceMap),
      // This is defined only for Android, so most builds won't pick it up.
      "androidBridge",
      // These are defined without interfaces and hence are not in the services map.
      "cpmm",
      "crashmanager",
      "mm",
      "ppmm",
      // The new xulStore also does not have an interface.
      "xulStore",
    ]);
    return {
      MemberExpression(node) {
        if (node.computed || node.object.type !== "Identifier") {
          return;
        }

        let mainNode;
        if (node.object.name == "Services") {
          mainNode = node;
        } else if (
          node.property.name == "Services" &&
          node.parent.type == "MemberExpression"
        ) {
          mainNode = node.parent;
        } else {
          return;
        }

        let alias = mainNode.property.name;
        if (!serviceAliases.has(alias)) {
          return;
        }

        if (
          mainNode.parent.type == "MemberExpression" &&
          !mainNode.parent.computed
        ) {
          let propertyName = mainNode.parent.property.name;
          if (propertyName == "wrappedJSObject") {
            return;
          }
          let interfaces = findInterfaceNames(alias);
          if (!interfaces.length) {
            return;
          }

          let checkedInterfaces = [];
          for (let item of interfaces) {
            if (isInInterface(item, propertyName)) {
              return;
            }
            checkedInterfaces.push(item);
          }
          context.report({
            node,
            messageId: "unknownProperty",
            data: {
              alias,
              propertyName,
              checkedInterfaces,
            },
          });
        }
      },
    };
  },
};