summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/valid-lazy.js
blob: 048ed17e3ee3f00872c4a65bca8497228adc57db (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
 * @fileoverview Ensures that definitions and uses of properties on the
 * ``lazy`` object are valid.
 *
 * 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");

const items = [
  "loader",
  "XPCOMUtils",
  "Integration",
  "ChromeUtils",
  "DevToolsUtils",
  "Object",
  "Reflect",
];

const callExpressionDefinitions = [
  /^loader\.lazyGetter\(lazy, "(\w+)"/,
  /^loader\.lazyServiceGetter\(lazy, "(\w+)"/,
  /^loader\.lazyRequireGetter\(lazy, "(\w+)"/,
  /^XPCOMUtils\.defineLazyGetter\(lazy, "(\w+)"/,
  /^Integration\.downloads\.defineESModuleGetter\(lazy, "(\w+)"/,
  /^ChromeUtils\.defineLazyGetter\(lazy, "(\w+)"/,
  /^ChromeUtils\.defineModuleGetter\(lazy, "(\w+)"/,
  /^XPCOMUtils\.defineLazyPreferenceGetter\(lazy, "(\w+)"/,
  /^XPCOMUtils\.defineLazyScriptGetter\(lazy, "(\w+)"/,
  /^XPCOMUtils\.defineLazyServiceGetter\(lazy, "(\w+)"/,
  /^XPCOMUtils\.defineConstant\(lazy, "(\w+)"/,
  /^DevToolsUtils\.defineLazyGetter\(lazy, "(\w+)"/,
  /^Object\.defineProperty\(lazy, "(\w+)"/,
  /^Reflect\.defineProperty\(lazy, "(\w+)"/,
];

const callExpressionMultiDefinitions = [
  "ChromeUtils.defineESModuleGetters(lazy,",
  "XPCOMUtils.defineLazyModuleGetters(lazy,",
  "XPCOMUtils.defineLazyServiceGetters(lazy,",
  "Object.defineProperties(lazy,",
  "loader.lazyRequireGetter(lazy,",
];

module.exports = {
  meta: {
    docs: {
      url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/rules/valid-lazy.html",
    },
    messages: {
      duplicateSymbol: "Duplicate symbol {{name}} being added to lazy.",
      incorrectType: "Unexpected literal for property name {{name}}",
      unknownProperty: "Unknown lazy member property {{name}}",
      unusedProperty: "Unused lazy property {{name}}",
      topLevelAndUnconditional:
        "Lazy property {{name}} is used at top-level unconditionally. It should be non-lazy.",
    },
    schema: [],
    type: "problem",
  },

  create(context) {
    let lazyProperties = new Map();
    let unknownProperties = [];
    let isLazyExported = false;

    function getAncestorNodes(node) {
      const ancestors = [];
      node = node.parent;
      while (node) {
        ancestors.unshift(node);
        node = node.parent;
      }
      return ancestors;
    }

    // Returns true if lazy getter definitions in prevNode and currNode are
    // duplicate.
    // This returns false if prevNode and currNode have the same IfStatement as
    // ancestor and they're in different branches.
    function isDuplicate(prevNode, currNode) {
      const prevAncestors = getAncestorNodes(prevNode);
      const currAncestors = getAncestorNodes(currNode);

      for (
        let i = 0;
        i < prevAncestors.length && i < currAncestors.length;
        i++
      ) {
        const prev = prevAncestors[i];
        const curr = currAncestors[i];
        if (prev === curr && prev.type === "IfStatement") {
          if (prevAncestors[i + 1] !== currAncestors[i + 1]) {
            return false;
          }
        }
      }

      return true;
    }

    function addProp(callNode, propNode, name) {
      if (
        lazyProperties.has(name) &&
        isDuplicate(lazyProperties.get(name).callNode, callNode)
      ) {
        context.report({
          node: propNode,
          messageId: "duplicateSymbol",
          data: { name },
        });
        return;
      }
      lazyProperties.set(name, { used: false, callNode, propNode });
    }

    function setPropertiesFromArgument(callNode, arg) {
      if (arg.type === "ObjectExpression") {
        for (let propNode of arg.properties) {
          if (propNode.key.type == "Literal") {
            context.report({
              node: propNode,
              messageId: "incorrectType",
              data: { name: propNode.key.value },
            });
            continue;
          }
          addProp(callNode, propNode, propNode.key.name);
        }
      } else if (arg.type === "ArrayExpression") {
        for (let propNode of arg.elements) {
          if (propNode.type != "Literal") {
            continue;
          }
          addProp(callNode, propNode, propNode.value);
        }
      }
    }

    return {
      VariableDeclarator(node) {
        if (
          node.id.type === "Identifier" &&
          node.id.name == "lazy" &&
          node.init.type == "CallExpression" &&
          node.init.callee.name == "createLazyLoaders"
        ) {
          setPropertiesFromArgument(node.init, node.init.arguments[0]);
        }
      },

      CallExpression(node) {
        if (
          node.callee.type != "MemberExpression" ||
          (node.callee.object.type == "MemberExpression" &&
            !items.includes(node.callee.object.object.name)) ||
          (node.callee.object.type != "MemberExpression" &&
            !items.includes(node.callee.object.name))
        ) {
          return;
        }

        let source;
        try {
          source = helpers.getASTSource(node);
        } catch (e) {
          return;
        }

        for (let reg of callExpressionDefinitions) {
          let match = source.match(reg);
          if (match) {
            if (
              lazyProperties.has(match[1]) &&
              isDuplicate(lazyProperties.get(match[1]).callNode, node)
            ) {
              context.report({
                node,
                messageId: "duplicateSymbol",
                data: { name: match[1] },
              });
              return;
            }
            lazyProperties.set(match[1], {
              used: false,
              callNode: node,
              propNode: node,
            });
            break;
          }
        }

        if (
          callExpressionMultiDefinitions.some(expr =>
            source.startsWith(expr)
          ) &&
          node.arguments[1]
        ) {
          setPropertiesFromArgument(node, node.arguments[1]);
        }
      },

      MemberExpression(node) {
        if (node.computed || node.object.type !== "Identifier") {
          return;
        }

        let name;
        if (node.object.name == "lazy") {
          name = node.property.name;
        } else {
          return;
        }
        let property = lazyProperties.get(name);
        if (!property) {
          // These will be reported on Program:exit - some definitions may
          // be after first use, so we need to wait until we've processed
          // the whole file before reporting.
          unknownProperties.push({ name, node });
        } else {
          property.used = true;
        }
        if (
          helpers.getIsTopLevelAndUnconditionallyExecuted(
            context.getAncestors()
          )
        ) {
          context.report({
            node,
            messageId: "topLevelAndUnconditional",
            data: { name },
          });
        }
      },

      ExportNamedDeclaration(node) {
        for (const spec of node.specifiers) {
          if (spec.local.name === "lazy") {
            // If the lazy object is exported, do not check unused property.
            isLazyExported = true;
            break;
          }
        }
      },

      "Program:exit": function () {
        for (let { name, node } of unknownProperties) {
          let property = lazyProperties.get(name);
          if (!property) {
            context.report({
              node,
              messageId: "unknownProperty",
              data: { name },
            });
          } else {
            property.used = true;
          }
        }
        if (!isLazyExported) {
          for (let [name, property] of lazyProperties.entries()) {
            if (!property.used) {
              context.report({
                node: property.propNode,
                messageId: "unusedProperty",
                data: { name },
              });
            }
          }
        }
      },
    };
  },
};