summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/tests/valid-lazy.js
blob: 6e207584fa9284aff6594866657d720dc4728502 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

var rule = require("../lib/rules/valid-lazy");
var RuleTester = require("eslint").RuleTester;

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 13 } });

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

function invalidCode(code, name, messageId) {
  return { code, errors: [{ messageId, data: { name } }] };
}

ruleTester.run("valid-lazy", rule, {
  // Note: these tests build on top of one another, although lazy gets
  // re-declared, it
  valid: [
    `
      const lazy = {};
      XPCOMUtils.defineLazyGetter(lazy, "foo", () => {});
      if (x) { lazy.foo.bar(); }
    `,
    `
      const lazy = {};
      XPCOMUtils.defineLazyModuleGetters(lazy, {
        foo: "foo.jsm",
      });
      if (x) { lazy.foo.bar(); }
    `,
    `
      const lazy = {};
      ChromeUtils.defineESModuleGetters(lazy, {
        foo: "foo.mjs",
      });
      if (x) { lazy.foo.bar(); }
    `,
    `
      const lazy = {};
      Integration.downloads.defineESModuleGetter(lazy, "foo", "foo.sys.mjs");
      if (x) { lazy.foo.bar(); }
    `,
    `
      const lazy = createLazyLoaders({ foo: () => {}});
      if (x) { lazy.foo.bar(); }
    `,
    `
      const lazy = {};
      loader.lazyRequireGetter(
        lazy,
        ["foo1", "foo2"],
        "bar",
        true
      );
      if (x) {
        lazy.foo1.bar();
        lazy.foo2.bar();
      }
    `,
    // Test for top-level unconditional.
    `
      const lazy = {};
      XPCOMUtils.defineLazyGetter(lazy, "foo", () => {});
      if (x) { lazy.foo.bar(); }
      for (;;) { lazy.foo.bar(); }
      for (var x in y) { lazy.foo.bar(); }
      for (var x of y) { lazy.foo.bar(); }
      while (true) { lazy.foo.bar(); }
      do { lazy.foo.bar(); } while (true);
      switch (x) { case 1: lazy.foo.bar(); }
      try { lazy.foo.bar(); } catch (e) {}
      function f() { lazy.foo.bar(); }
      (function f() { lazy.foo.bar(); });
      () => { lazy.foo.bar(); };
      class C {
        constructor() { lazy.foo.bar(); }
        foo() { lazy.foo.bar(); }
        get x() { lazy.foo.bar(); }
        set x(v) { lazy.foo.bar(); }
        a = lazy.foo.bar();
        #b = lazy.foo.bar();
        static {
          lazy.foo.bar();
        }
      }
      a && lazy.foo.bar();
      a || lazy.foo.bar();
      a ?? lazy.foo.bar();
      a ? lazy.foo.bar() : b;
      a?.b[lazy.foo.bar()];
      a ||= lazy.foo.bar();
      a &&= lazy.foo.bar();
      a ??= lazy.foo.bar();
      var { x = lazy.foo.bar() } = {};
      var [ y = lazy.foo.bar() ] = [];
    `,
  ],
  invalid: [
    invalidCode("if (x) { lazy.bar; }", "bar", "unknownProperty"),
    invalidCode(
      `
        const lazy = {};
        XPCOMUtils.defineLazyGetter(lazy, "foo", "foo.jsm");
        XPCOMUtils.defineLazyGetter(lazy, "foo", "foo1.jsm");
        if (x) { lazy.foo.bar(); }
      `,
      "foo",
      "duplicateSymbol"
    ),
    invalidCode(
      `
        const lazy = {};
        XPCOMUtils.defineLazyModuleGetters(lazy, {
          "foo-bar": "foo.jsm",
        });
        if (x) { lazy["foo-bar"].bar(); }
      `,
      "foo-bar",
      "incorrectType"
    ),
    invalidCode(
      `const lazy = {};
       XPCOMUtils.defineLazyGetter(lazy, "foo", "foo.jsm");
      `,
      "foo",
      "unusedProperty"
    ),
    invalidCode(
      `const lazy = {};
      XPCOMUtils.defineLazyGetter(lazy, "foo1", () => {});
      lazy.foo1.bar();`,
      "foo1",
      "topLevelAndUnconditional"
    ),
    invalidCode(
      `const lazy = {};
      XPCOMUtils.defineLazyGetter(lazy, "foo1", () => {});
      { x = -f(1 + lazy.foo1.bar()); }`,
      "foo1",
      "topLevelAndUnconditional"
    ),
  ],
});