]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-obj-calls.js
first commit
[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: "globalThis.Math();", env: { es6: true } },
50 { code: "var x = globalThis.Math();", env: { es6: true } },
51 { code: "f(globalThis.Math());", env: { es6: true } },
52 { code: "globalThis.Math().foo;", env: { es6: true } },
53 { code: "var x = globalThis.JSON();", env: { es6: true } },
54 { code: "x = globalThis.JSON(str);", env: { es6: true } },
55 { code: "globalThis.Math( globalThis.JSON() );", env: { es6: true } },
56 { code: "var x = globalThis.Reflect();", env: { es6: true } },
57 { code: "var x = globalThis.Reflect();", env: { es2017: true } },
58 { code: "/*globals Reflect: true*/ globalThis.Reflect();", env: { es2017: true } },
59 { code: "var x = globalThis.Atomics();", env: { es2017: true } },
60 { code: "var x = globalThis.Atomics();", globals: { Atomics: false }, env: { es2017: true } },
61
62 // non-existing variables
63 "/*globals Math: off*/ Math();",
64 "/*globals Math: off*/ new Math();",
65 {
66 code: "JSON();",
67 globals: { JSON: "off" }
68 },
69 {
70 code: "new JSON();",
71 globals: { JSON: "off" }
72 },
73 "Reflect();",
74 "Atomics();",
75 "new Reflect();",
76 "new Atomics();",
77 {
78 code: "Atomics();",
79 env: { es6: true }
80 },
81
82 // shadowed variables
83 "var Math; Math();",
84 "var Math; new Math();",
85 {
86 code: "let JSON; JSON();",
87 parserOptions: { ecmaVersion: 2015 }
88 },
89 {
90 code: "let JSON; new JSON();",
91 parserOptions: { ecmaVersion: 2015 }
92 },
93 {
94 code: "if (foo) { const Reflect = 1; Reflect(); }",
95 parserOptions: { ecmaVersion: 2015 },
96 env: { es6: true }
97 },
98 {
99 code: "if (foo) { const Reflect = 1; new Reflect(); }",
100 parserOptions: { ecmaVersion: 2015 },
101 env: { es6: true }
102 },
103 "function foo(Math) { Math(); }",
104 "function foo(JSON) { new JSON(); }",
105 {
106 code: "function foo(Atomics) { Atomics(); }",
107 env: { es2017: true }
108 },
109 {
110 code: "function foo() { if (bar) { let Atomics; if (baz) { new Atomics(); } } }",
111 parserOptions: { ecmaVersion: 2015 },
112 env: { es2017: true }
113 },
114 "function foo() { var JSON; JSON(); }",
115 {
116 code: "function foo() { var Atomics = bar(); var baz = Atomics(5); }",
117 globals: { Atomics: false }
118 },
119 {
120 code: "var construct = typeof Reflect !== \"undefined\" ? Reflect.construct : undefined; construct();",
121 globals: { Reflect: false }
122 }
123 ],
124 invalid: [
125 {
126 code: "Math();",
127 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression" }]
128 },
129 {
130 code: "var x = Math();",
131 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression" }]
132 },
133 {
134 code: "f(Math());",
135 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 3, endColumn: 9 }]
136 },
137 {
138 code: "Math().foo;",
139 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 7 }]
140 },
141 {
142 code: "new Math;",
143 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
144 },
145 {
146 code: "new Math();",
147 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
148 },
149 {
150 code: "new Math(foo);",
151 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
152 },
153 {
154 code: "new Math().foo;",
155 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
156 },
157 {
158 code: "(new Math).foo();",
159 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
160 },
161 {
162 code: "var x = JSON();",
163 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
164 },
165 {
166 code: "x = JSON(str);",
167 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
168 },
169 {
170 code: "var x = new JSON();",
171 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "NewExpression" }]
172 },
173 {
174 code: "Math( JSON() );",
175 errors: [
176 { messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 15 },
177 { messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression", column: 7, endColumn: 13 }
178 ]
179 },
180 {
181 code: "var x = Reflect();",
182 env: { es6: true },
183 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
184 },
185 {
186 code: "var x = new Reflect();",
187 env: { es6: true },
188 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "NewExpression" }]
189 },
190 {
191 code: "var x = Reflect();",
192 env: { es2017: true },
193 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
194 },
195 {
196 code: "/*globals Reflect: true*/ Reflect();",
197 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
198 },
199 {
200 code: "/*globals Reflect: true*/ new Reflect();",
201 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "NewExpression" }]
202 },
203 {
204 code: "var x = Atomics();",
205 env: { es2017: true },
206 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
207 },
208 {
209 code: "var x = new Atomics();",
210 env: { es2017: true },
211 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "NewExpression" }]
212 },
213 {
214 code: "var x = Atomics();",
215 env: { es2020: true },
216 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
217 },
218 {
219 code: "var x = Atomics();",
220 globals: { Atomics: false },
221 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
222 },
223 {
224 code: "var x = new Atomics();",
225 globals: { Atomics: "writable" },
226 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "NewExpression" }]
227 },
228 {
229 code: "var x = globalThis.Math();",
230 env: { es2020: true },
231 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression" }]
232 },
233 {
234 code: "var x = new globalThis.Math();",
235 env: { es2020: true },
236 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression" }]
237 },
238 {
239 code: "f(globalThis.Math());",
240 env: { es2020: true },
241 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 3, endColumn: 20 }]
242 },
243 {
244 code: "globalThis.Math().foo;",
245 env: { es2020: true },
246 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 18 }]
247 },
248 {
249 code: "new globalThis.Math().foo;",
250 env: { es2020: true },
251 errors: [{ messageId: "unexpectedCall", data: { name: "Math" }, type: "NewExpression", column: 1, endColumn: 22 }]
252 },
253 {
254 code: "var x = globalThis.JSON();",
255 env: { es2020: true },
256 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
257 },
258 {
259 code: "x = globalThis.JSON(str);",
260 env: { es2020: true },
261 errors: [{ messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression" }]
262 },
263 {
264 code: "globalThis.Math( globalThis.JSON() );",
265 env: { es2020: true },
266 errors: [
267 { messageId: "unexpectedCall", data: { name: "Math" }, type: "CallExpression", column: 1, endColumn: 37 },
268 { messageId: "unexpectedCall", data: { name: "JSON" }, type: "CallExpression", column: 18, endColumn: 35 }
269 ]
270 },
271 {
272 code: "var x = globalThis.Reflect();",
273 env: { es2020: true },
274 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
275 },
276 {
277 code: "var x = new globalThis.Reflect;",
278 env: { es2020: true },
279 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "NewExpression" }]
280 },
281 {
282 code: "/*globals Reflect: true*/ Reflect();",
283 env: { es2020: true },
284 errors: [{ messageId: "unexpectedCall", data: { name: "Reflect" }, type: "CallExpression" }]
285 },
286 {
287 code: "var x = globalThis.Atomics();",
288 env: { es2020: true },
289 errors: [{ messageId: "unexpectedCall", data: { name: "Atomics" }, type: "CallExpression" }]
290 },
291 {
292 code: "var foo = bar ? baz: JSON; foo();",
293 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "JSON" }, type: "CallExpression" }]
294 },
295 {
296 code: "var foo = bar ? baz: JSON; new foo();",
297 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "JSON" }, type: "NewExpression" }]
298 },
299 {
300 code: "var foo = bar ? baz: globalThis.JSON; foo();",
301 env: { es2020: true },
302 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "JSON" }, type: "CallExpression" }]
303 },
304 {
305 code: "var foo = bar ? baz: globalThis.JSON; new foo();",
306 env: { es2020: true },
307 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "JSON" }, type: "NewExpression" }]
308 },
309 {
310 code: "var foo = window.Atomics; foo();",
311 env: { es2020: true, browser: true },
312 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "Atomics" }, type: "CallExpression" }]
313 },
314 {
315 code: "var foo = window.Atomics; new foo;",
316 env: { es2020: true, browser: true },
317 errors: [{ messageId: "unexpectedRefCall", data: { name: "foo", ref: "Atomics" }, type: "NewExpression" }]
318 }
319 ]
320 });