]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-obj-calls.js
import 8.41.0 source
[pve-eslint.git] / eslint / tests / lib / rules / no-obj-calls.js
1 /**
2 * @fileoverview Tests for no-obj-calls rule.
3 * @author James Allardice
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-obj-calls"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-obj-calls", rule, {
22 valid: [
23 "var x = Math;",
24 "var x = Math.random();",
25 "var x = Math.PI;",
26 "var x = foo.Math();",
27 "var x = new foo.Math();",
28 "var x = new Math.foo;",
29 "var x = new Math.foo();",
30 "JSON.parse(foo)",
31 "new JSON.parse",
32 {
33 code: "Reflect.get(foo, 'x')",
34 env: { es6: true }
35 },
36 {
37 code: "new Reflect.foo(a, b)",
38 env: { es6: true }
39 },
40 {
41 code: "Atomics.load(foo, 0)",
42 env: { es2017: true }
43 },
44 {
45 code: "new Atomics.foo()",
46 env: { es2017: true }
47 },
48 {
49 code: "new Intl.Segmenter()",
50 env: { browser: true }
51 },
52 {
53 code: "Intl.foo()",
54 env: { browser: true }
55 },
56
57 { code: "globalThis.Math();", env: { es6: true } },
58 { code: "var x = globalThis.Math();", env: { es6: true } },
59 { code: "f(globalThis.Math());", env: { es6: true } },
60 { code: "globalThis.Math().foo;", env: { es6: true } },
61 { code: "var x = globalThis.JSON();", env: { es6: true } },
62 { code: "x = globalThis.JSON(str);", env: { es6: true } },
63 { code: "globalThis.Math( globalThis.JSON() );", env: { es6: true } },
64 { code: "var x = globalThis.Reflect();", env: { es6: true } },
65 { code: "var x = globalThis.Reflect();", env: { es2017: true } },
66 { code: "/*globals Reflect: true*/ globalThis.Reflect();", env: { es2017: true } },
67 { code: "var x = globalThis.Atomics();", env: { es2017: true } },
68 { code: "var x = globalThis.Atomics();", globals: { Atomics: false }, env: { es2017: true } },
69 { code: "var x = globalThis.Intl();", env: { browser: true } },
70 { code: "var x = globalThis.Intl();", globals: { Intl: false }, env: { browser: true } },
71
72 // non-existing variables
73 "/*globals Math: off*/ Math();",
74 "/*globals Math: off*/ new Math();",
75 {
76 code: "JSON();",
77 globals: { JSON: "off" }
78 },
79 {
80 code: "new JSON();",
81 globals: { JSON: "off" }
82 },
83 "Reflect();",
84 "Atomics();",
85 "new Reflect();",
86 "new Atomics();",
87 {
88 code: "Atomics();",
89 env: { es6: true }
90 },
91 "Intl()",
92 "new Intl()",
93
94 // shadowed variables
95 "var Math; Math();",
96 "var Math; new Math();",
97 {
98 code: "let JSON; JSON();",
99 parserOptions: { ecmaVersion: 2015 }
100 },
101 {
102 code: "let JSON; new JSON();",
103 parserOptions: { ecmaVersion: 2015 }
104 },
105 {
106 code: "if (foo) { const Reflect = 1; Reflect(); }",
107 parserOptions: { ecmaVersion: 2015 },
108 env: { es6: true }
109 },
110 {
111 code: "if (foo) { const Reflect = 1; new Reflect(); }",
112 parserOptions: { ecmaVersion: 2015 },
113 env: { es6: true }
114 },
115 "function foo(Math) { Math(); }",
116 "function foo(JSON) { new JSON(); }",
117 {
118 code: "function foo(Atomics) { Atomics(); }",
119 env: { es2017: true }
120 },
121 {
122 code: "function foo() { if (bar) { let Atomics; if (baz) { new Atomics(); } } }",
123 parserOptions: { ecmaVersion: 2015 },
124 env: { es2017: true }
125 },
126 "function foo() { var JSON; JSON(); }",
127 {
128 code: "function foo() { var Atomics = bar(); var baz = Atomics(5); }",
129 globals: { Atomics: false }
130 },
131 {
132 code: "var construct = typeof Reflect !== \"undefined\" ? Reflect.construct : undefined; construct();",
133 globals: { Reflect: false }
134 },
135 {
136 code: "function foo(Intl) { Intl(); }",
137 env: { browser: true }
138 },
139 {
140 code: "if (foo) { const Intl = 1; Intl(); }",
141 parserOptions: { ecmaVersion: 2015 },
142 env: { browser: true }
143 },
144 {
145 code: "if (foo) { const Intl = 1; new Intl(); }",
146 parserOptions: { ecmaVersion: 2015 },
147 env: { browser: true }
148 }
149 ],
150 invalid: [
151 {
152 code: "Math();",
153 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression" }]
154 },
155 {
156 code: "var x = Math();",
157 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression" }]
158 },
159 {
160 code: "f(Math());",
161 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 3, endColumn: 9 }]
162 },
163 {
164 code: "Math().foo;",
165 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 7 }]
166 },
167 {
168 code: "new Math;",
169 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
170 },
171 {
172 code: "new Math();",
173 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
174 },
175 {
176 code: "new Math(foo);",
177 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
178 },
179 {
180 code: "new Math().foo;",
181 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
182 },
183 {
184 code: "(new Math).foo();",
185 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
186 },
187 {
188 code: "var x = JSON();",
189 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
190 },
191 {
192 code: "x = JSON(str);",
193 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
194 },
195 {
196 code: "var x = new JSON();",
197 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "NewExpression" }]
198 },
199 {
200 code: "Math( JSON() );",
201 errors: [
202 { messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 15 },
203 { messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression", column: 7, endColumn: 13 }
204 ]
205 },
206 {
207 code: "var x = Reflect();",
208 env: { es6: true },
209 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
210 },
211 {
212 code: "var x = new Reflect();",
213 env: { es6: true },
214 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "NewExpression" }]
215 },
216 {
217 code: "var x = Reflect();",
218 env: { es2017: true },
219 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
220 },
221 {
222 code: "/*globals Reflect: true*/ Reflect();",
223 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
224 },
225 {
226 code: "/*globals Reflect: true*/ new Reflect();",
227 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "NewExpression" }]
228 },
229 {
230 code: "var x = Atomics();",
231 env: { es2017: true },
232 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
233 },
234 {
235 code: "var x = new Atomics();",
236 env: { es2017: true },
237 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "NewExpression" }]
238 },
239 {
240 code: "var x = Atomics();",
241 env: { es2020: true },
242 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
243 },
244 {
245 code: "var x = Atomics();",
246 globals: { Atomics: false },
247 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
248 },
249 {
250 code: "var x = new Atomics();",
251 globals: { Atomics: "writable" },
252 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "NewExpression" }]
253 },
254 {
255 code: "var x = Intl();",
256 env: { browser: true },
257 errors: [{ messageId: "unexpectedCall", data: { name: "Intl" }, type: "CallExpression" }]
258 },
259 {
260 code: "var x = new Intl();",
261 env: { browser: true },
262 errors: [{ messageId: "unexpectedCall", data: { name: "Intl" }, type: "NewExpression" }]
263 },
264 {
265 code: "/*globals Intl: true*/ Intl();",
266 errors: [{ messageId: "unexpectedCall", data: { name: "Intl" }, type: "CallExpression" }]
267 },
268 {
269 code: "/*globals Intl: true*/ new Intl();",
270 errors: [{ messageId: "unexpectedCall", data: { name: "Intl" }, type: "NewExpression" }]
271 },
272 {
273 code: "var x = globalThis.Math();",
274 env: { es2020: true },
275 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression" }]
276 },
277 {
278 code: "var x = new globalThis.Math();",
279 env: { es2020: true },
280 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
281 },
282 {
283 code: "f(globalThis.Math());",
284 env: { es2020: true },
285 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 3, endColumn: 20 }]
286 },
287 {
288 code: "globalThis.Math().foo;",
289 env: { es2020: true },
290 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 18 }]
291 },
292 {
293 code: "new globalThis.Math().foo;",
294 env: { es2020: true },
295 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression", column: 1, endColumn: 22 }]
296 },
297 {
298 code: "var x = globalThis.JSON();",
299 env: { es2020: true },
300 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
301 },
302 {
303 code: "x = globalThis.JSON(str);",
304 env: { es2020: true },
305 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
306 },
307 {
308 code: "globalThis.Math( globalThis.JSON() );",
309 env: { es2020: true },
310 errors: [
311 { messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 37 },
312 { messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression", column: 18, endColumn: 35 }
313 ]
314 },
315 {
316 code: "var x = globalThis.Reflect();",
317 env: { es2020: true },
318 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
319 },
320 {
321 code: "var x = new globalThis.Reflect;",
322 env: { es2020: true },
323 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "NewExpression" }]
324 },
325 {
326 code: "/*globals Reflect: true*/ Reflect();",
327 env: { es2020: true },
328 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
329 },
330 {
331 code: "var x = globalThis.Atomics();",
332 env: { es2020: true },
333 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
334 },
335 {
336 code: "var x = globalThis.Intl();",
337 env: { browser: true, es2020: true },
338 errors: [{ messageId: "unexpectedCall", data: { name: "Intl" }, type: "CallExpression" }]
339 },
340 {
341 code: "var x = new globalThis.Intl;",
342 env: { browser: true, es2020: true },
343 errors: [{ messageId: "unexpectedCall", data: { name: "Intl" }, type: "NewExpression" }]
344 },
345 {
346 code: "/*globals Intl: true*/ Intl();",
347 env: { browser: true, es2020: true },
348 errors: [{ messageId: "unexpectedCall", data: { name: "Intl" }, type: "CallExpression" }]
349 },
350 {
351 code: "var foo = bar ? baz: JSON; foo();",
352 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "JSON" }, type: "CallExpression" }]
353 },
354 {
355 code: "var foo = bar ? baz: JSON; new foo();",
356 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "JSON" }, type: "NewExpression" }]
357 },
358 {
359 code: "var foo = bar ? baz: globalThis.JSON; foo();",
360 env: { es2020: true },
361 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "JSON" }, type: "CallExpression" }]
362 },
363 {
364 code: "var foo = bar ? baz: globalThis.JSON; new foo();",
365 env: { es2020: true },
366 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "JSON" }, type: "NewExpression" }]
367 },
368 {
369 code: "var foo = window.Atomics; foo();",
370 env: { es2020: true, browser: true },
371 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "Atomics" }, type: "CallExpression" }]
372 },
373 {
374 code: "var foo = window.Atomics; new foo;",
375 env: { es2020: true, browser: true },
376 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "Atomics" }, type: "NewExpression" }]
377 },
378 {
379 code: "var foo = window.Intl; foo();",
380 env: { es2020: true, browser: true },
381 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "Intl" }, type: "CallExpression" }]
382 },
383 {
384 code: "var foo = window.Intl; new foo;",
385 env: { es2020: true, browser: true },
386 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "Intl" }, type: "NewExpression" }]
387 },
388
389 // Optional chaining
390 {
391 code: "var x = globalThis?.Reflect();",
392 env: { es2020: true },
393 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
394 },
395 {
396 code: "var x = (globalThis?.Reflect)();",
397 env: { es2020: true },
398 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
399 }
400 ]
401 });