summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-mixing-eager-and-lazy.js
blob: 5779a90afd39207186f39c4159e73e5a254f1730 (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
/**
 * @fileoverview Reject use of lazy getters for modules that's loaded early in
 *               the startup process and not necessarily be lazy.
 *
 * 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 isIdentifier(node, id) {
  return node.type === "Identifier" && node.name === id;
}

function isString(node) {
  return node.type === "Literal" && typeof node.value === "string";
}

function checkMixed(loadedModules, context, node, type, resourceURI) {
  if (!loadedModules.has(resourceURI)) {
    loadedModules.set(resourceURI, type);
  }

  if (loadedModules.get(resourceURI) === type) {
    return;
  }

  context.report({
    node,
    messageId: "mixedEagerAndLazy",
    data: { uri: resourceURI },
  });
}

module.exports = {
  meta: {
    docs: {
      url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/rules/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-mixed-eager-and-lazy.html",
    },
    messages: {
      mixedEagerAndLazy:
        'Module "{{uri}}" is loaded eagerly, and should not be used for lazy getter.',
    },
    schema: [],
    type: "problem",
  },

  create(context) {
    const loadedModules = new Map();

    return {
      ImportDeclaration(node) {
        const resourceURI = node.source.value;
        checkMixed(loadedModules, context, node, "eager", resourceURI);
      },
      CallExpression(node) {
        if (node.callee.type !== "MemberExpression") {
          return;
        }

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

        if (
          (callerSource === "ChromeUtils.import" ||
            callerSource === "ChromeUtils.importESModule") &&
          helpers.getIsTopLevelAndUnconditionallyExecuted(
            context.getAncestors()
          )
        ) {
          if (node.arguments.length < 1) {
            return;
          }
          const resourceURINode = node.arguments[0];
          if (!isString(resourceURINode)) {
            return;
          }
          checkMixed(
            loadedModules,
            context,
            node,
            "eager",
            resourceURINode.value
          );
        }

        if (callerSource === "ChromeUtils.defineModuleGetter") {
          if (node.arguments.length < 3) {
            return;
          }
          if (!isIdentifier(node.arguments[0], "lazy")) {
            return;
          }

          const resourceURINode = node.arguments[2];
          if (!isString(resourceURINode)) {
            return;
          }
          checkMixed(
            loadedModules,
            context,
            node,
            "lazy",
            resourceURINode.value
          );
        } else if (
          callerSource === "XPCOMUtils.defineLazyModuleGetters" ||
          callerSource === "ChromeUtils.defineESModuleGetters"
        ) {
          if (node.arguments.length < 2) {
            return;
          }
          if (!isIdentifier(node.arguments[0], "lazy")) {
            return;
          }

          const obj = node.arguments[1];
          if (obj.type !== "ObjectExpression") {
            return;
          }
          for (let prop of obj.properties) {
            if (prop.type !== "Property") {
              continue;
            }
            if (prop.kind !== "init") {
              continue;
            }
            const resourceURINode = prop.value;
            if (!isString(resourceURINode)) {
              continue;
            }
            checkMixed(
              loadedModules,
              context,
              node,
              "lazy",
              resourceURINode.value
            );
          }
        }
      },
    };
  },
};