summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/valid-ci-uses.js
blob: e88a333cdae458e0287ebee47362eeb9b1976f1b (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
 * @fileoverview Reject uses of unknown interfaces on Ci and properties of those
 * interfaces.
 *
 * 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 os = require("os");
const helpers = require("../helpers");

// These interfaces are all platform specific, so may be not present
// on all platforms.
const platformSpecificInterfaces = new Map([
  ["nsIAboutThirdParty", "windows"],
  ["nsIAboutWindowsMessages", "windows"],
  ["nsIJumpListItem", "windows"],
  ["nsIJumpListLink", "windows"],
  ["nsIJumpListSeparator", "windows"],
  ["nsIJumpListShortcut", "windows"],
  ["nsITaskbarWindowPreview", "windows"],
  ["nsIWindowsAlertsService", "windows"],
  ["nsIWinAppHelper", "windows"],
  ["nsIWinTaskbar", "windows"],
  ["nsIWinTaskSchedulerService", "windows"],
  ["nsIWindowsRegKey", "windows"],
  ["nsIWindowsPackageManager", "windows"],
  ["nsIWindowsShellService", "windows"],
  ["nsIAccessibleMacEvent", "darwin"],
  ["nsIAccessibleMacInterface", "darwin"],
  ["nsILocalFileMac", "darwin"],
  ["nsIAccessibleMacEvent", "darwin"],
  ["nsIMacAttributionService", "darwin"],
  ["nsIMacShellService", "darwin"],
  ["nsIMacDockSupport", "darwin"],
  ["nsIMacFinderProgress", "darwin"],
  ["nsIMacPreferencesReader", "darwin"],
  ["nsIMacSharingService", "darwin"],
  ["nsIMacUserActivityUpdater", "darwin"],
  ["nsIMacWebAppUtils", "darwin"],
  ["nsIStandaloneNativeMenu", "darwin"],
  ["nsITouchBarHelper", "darwin"],
  ["nsITouchBarInput", "darwin"],
  ["nsITouchBarUpdater", "darwin"],
  ["mozISandboxReporter", "linux"],
  ["nsIApplicationChooser", "linux"],
  ["nsIGNOMEShellService", "linux"],
  ["nsIGtkTaskbarProgress", "linux"],

  // These are used in the ESLint test code.
  ["amIFoo", "any"],
  ["nsIMeh", "any"],
  // Can't easily detect android builds from ESLint at the moment.
  ["nsIAndroidBridge", "any"],
  ["nsIAndroidView", "any"],
  // Code coverage is enabled only for certain builds (MOZ_CODE_COVERAGE).
  ["nsICodeCoverage", "any"],
  // Layout debugging is enabled only for certain builds (MOZ_LAYOUT_DEBUGGER).
  ["nsILayoutDebuggingTools", "any"],
  // Sandbox test is only enabled for certain configurations (MOZ_SANDBOX,
  // MOZ_DEBUG, ENABLE_TESTS).
  ["mozISandboxTest", "any"],
]);

function interfaceHasProperty(interfaceName, propertyName) {
  // `Ci.nsIFoo.number` is valid, it returns the iid.
  if (propertyName == "number") {
    return true;
  }

  let interfaceInfo = helpers.xpidlData.get(interfaceName);

  if (!interfaceInfo) {
    return true;
  }

  // If the property is not in the lists of consts for this interface, check
  // any parents as well.
  if (!interfaceInfo.consts.find(e => e.name === propertyName)) {
    if (interfaceInfo.parent && interfaceInfo.parent != "nsISupports") {
      return interfaceHasProperty(interfaceName.parent, propertyName);
    }
    return false;
  }
  return true;
}

module.exports = {
  meta: {
    docs: {
      url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/valid-ci-uses.html",
    },
    messages: {
      missingInterface:
        "{{ interface }} is defined in this rule's platform specific list, but is not available",
      unknownInterface: "Use of unknown interface Ci.{{ interface}}",
      unknownProperty:
        "Use of unknown property Ci.{{ interface }}.{{ property }}",
    },
    schema: [],
    type: "problem",
  },

  create(context) {
    return {
      MemberExpression(node) {
        if (
          node.computed === false &&
          node.type === "MemberExpression" &&
          node.object.type === "Identifier" &&
          node.object.name === "Ci" &&
          node.property.type === "Identifier" &&
          node.property.name.includes("I")
        ) {
          if (!helpers.xpidlData.get(node.property.name)) {
            let platformSpecific = platformSpecificInterfaces.get(
              node.property.name
            );
            if (!platformSpecific) {
              context.report({
                node,
                messageId: "unknownInterface",
                data: {
                  interface: node.property.name,
                },
              });
            } else if (platformSpecific == os.platform) {
              context.report({
                node,
                messageId: "missingInterface",
                data: {
                  interface: node.property.name,
                },
              });
            }
          }
        }

        if (
          node.computed === false &&
          node.object.type === "MemberExpression" &&
          node.object.object.type === "Identifier" &&
          node.object.object.name === "Ci" &&
          node.object.property.type === "Identifier" &&
          node.object.property.name.includes("I") &&
          node.property.type === "Identifier"
        ) {
          if (
            !interfaceHasProperty(node.object.property.name, node.property.name)
          ) {
            context.report({
              node,
              messageId: "unknownProperty",
              data: {
                interface: node.object.property.name,
                property: node.property.name,
              },
            });
          }
        }
      },
    };
  },
};