]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/new-cap.js
5953c87c24aedcc0717634cfe623d0b911b3820e
[pve-eslint.git] / eslint / tests / lib / rules / new-cap.js
1 /**
2 * @fileoverview Tests for new-cap rule.
3 * @author Nicholas C. Zakas
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/new-cap"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("new-cap", rule, {
22 valid: [
23 "var x = new Constructor();",
24 "var x = new a.b.Constructor();",
25 "var x = new a.b['Constructor']();",
26 "var x = new a.b[Constructor]();",
27 "var x = new a.b[constructor]();",
28 "var x = new function(){};",
29 "var x = new _;",
30 "var x = new $;",
31 "var x = new Σ;",
32 "var x = new _x;",
33 "var x = new $x;",
34 "var x = new this;",
35 "var x = Array(42)",
36 "var x = Boolean(42)",
37 "var x = Date(42)",
38 "var x = Date.UTC(2000, 0)",
39 "var x = Error('error')",
40 "var x = Function('return 0')",
41 "var x = Number(42)",
42 "var x = Object(null)",
43 "var x = RegExp(42)",
44 "var x = String(42)",
45 "var x = Symbol('symbol')",
46 "var x = BigInt('1n')",
47 "var x = _();",
48 "var x = $();",
49 { code: "var x = Foo(42)", options: [{ capIsNew: false }] },
50 { code: "var x = bar.Foo(42)", options: [{ capIsNew: false }] },
51 { code: "var x = Foo.bar(42)", options: [{ capIsNew: false }] },
52 "var x = bar[Foo](42)",
53 { code: "var x = bar['Foo'](42)", options: [{ capIsNew: false }] },
54 "var x = Foo.bar(42)",
55 { code: "var x = new foo(42)", options: [{ newIsCap: false }] },
56 "var o = { 1: function() {} }; o[1]();",
57 "var o = { 1: function() {} }; new o[1]();",
58 { code: "var x = Foo(42);", options: [{ capIsNew: true, capIsNewExceptions: ["Foo"] }] },
59 { code: "var x = Foo(42);", options: [{ capIsNewExceptionPattern: "^Foo" }] },
60 { code: "var x = new foo(42);", options: [{ newIsCap: true, newIsCapExceptions: ["foo"] }] },
61 { code: "var x = new foo(42);", options: [{ newIsCapExceptionPattern: "^foo" }] },
62 { code: "var x = Object(42);", options: [{ capIsNewExceptions: ["Foo"] }] },
63
64 { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Bar"] }] },
65 { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Foo.Bar"] }] },
66
67 { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptionPattern: "^Foo\\.." }] },
68 { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["bar"] }] },
69 { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["foo.bar"] }] },
70
71 { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptionPattern: "^foo\\.." }] },
72 { code: "var x = new foo.bar(42);", options: [{ properties: false }] },
73 { code: "var x = Foo.bar(42);", options: [{ properties: false }] },
74 { code: "var x = foo.Bar(42);", options: [{ capIsNew: false, properties: false }] },
75
76 // Optional chaining
77 {
78 code: "foo?.bar();",
79 parserOptions: { ecmaVersion: 2020 }
80 },
81 {
82 code: "(foo?.bar)();",
83 parserOptions: { ecmaVersion: 2020 }
84 },
85 {
86 code: "new (foo?.Bar)();",
87 parserOptions: { ecmaVersion: 2020 }
88 },
89 {
90 code: "(foo?.Bar)();",
91 options: [{ properties: false }],
92 parserOptions: { ecmaVersion: 2020 }
93 },
94 {
95 code: "new (foo?.bar)();",
96 options: [{ properties: false }],
97 parserOptions: { ecmaVersion: 2020 }
98 },
99 {
100 code: "Date?.UTC();",
101 parserOptions: { ecmaVersion: 2020 }
102 },
103 {
104 code: "(Date?.UTC)();",
105 parserOptions: { ecmaVersion: 2020 }
106 }
107 ],
108 invalid: [
109 {
110 code: "var x = new c();",
111 errors: [{
112 messageId: "lower",
113 type: "NewExpression",
114 line: 1,
115 column: 13,
116 endLine: 1,
117 endColumn: 14
118 }]
119 },
120 {
121 code: "var x = new φ;",
122 errors: [{
123 messageId: "lower",
124 type: "NewExpression",
125 line: 1,
126 column: 13,
127 endLine: 1,
128 endColumn: 14
129 }]
130 },
131 {
132 code: "var x = new a.b.c;",
133 errors: [{
134 messageId: "lower",
135 type: "NewExpression",
136 line: 1,
137 column: 17,
138 endLine: 1,
139 endColumn: 18
140 }]
141 },
142 {
143 code: "var x = new a.b['c'];",
144 errors: [{
145 messageId: "lower",
146 type: "NewExpression",
147 line: 1,
148 column: 17,
149 endLine: 1,
150 endColumn: 20
151 }]
152 },
153 {
154 code: "var b = Foo();",
155 errors: [{
156 messageId: "upper",
157 type: "CallExpression",
158 line: 1,
159 column: 9,
160 endLine: 1,
161 endColumn: 12
162 }]
163 },
164 {
165 code: "var b = a.Foo();",
166 errors: [{
167 messageId: "upper",
168 type: "CallExpression",
169 line: 1,
170 column: 11,
171 endLine: 1,
172 endColumn: 14
173 }]
174 },
175 {
176 code: "var b = a['Foo']();",
177 errors: [{
178 messageId: "upper",
179 type: "CallExpression",
180 line: 1,
181 column: 11,
182 endLine: 1,
183 endColumn: 16
184 }]
185 },
186 {
187 code: "var b = a.Date.UTC();",
188 errors: [{
189 messageId: "upper",
190 type: "CallExpression",
191 line: 1,
192 column: 16,
193 endLine: 1,
194 endColumn: 19
195 }]
196 },
197 {
198 code: "var b = UTC();",
199 errors: [{
200 messageId: "upper",
201 type: "CallExpression",
202 line: 1,
203 column: 9,
204 endLine: 1,
205 endColumn: 12
206 }]
207 },
208 {
209 code: "var a = B.C();",
210 errors: [
211 {
212 messageId: "upper",
213 type: "CallExpression",
214 line: 1,
215 column: 11,
216 endLine: 1,
217 endColumn: 12
218 }
219 ]
220 },
221 {
222 code: "var a = B\n.C();",
223 errors: [
224 {
225 messageId: "upper",
226 type: "CallExpression",
227 line: 2,
228 column: 2,
229 endLine: 2,
230 endColumn: 3
231 }
232 ]
233 },
234 {
235 code: "var a = new B.c();",
236 errors: [
237 {
238 messageId: "lower",
239 type: "NewExpression",
240 line: 1,
241 column: 15,
242 endLine: 1,
243 endColumn: 16
244 }
245 ]
246 },
247 {
248 code: "var a = new B.\nc();",
249 errors: [
250 {
251 messageId: "lower",
252 type: "NewExpression",
253 line: 2,
254 column: 1,
255 endLine: 2,
256 endColumn: 2
257 }
258 ]
259 },
260 {
261 code: "var a = new c();",
262 errors: [
263 {
264 messageId: "lower",
265 type: "NewExpression",
266 line: 1,
267 column: 13,
268 endLine: 1,
269 endColumn: 14
270 }
271 ]
272 },
273 {
274 code: "var a = new b[ ( 'foo' ) ]();",
275 parserOptions: { ecmaVersion: 6 },
276 errors: [
277 {
278 messageId: "lower",
279 type: "NewExpression",
280 line: 1,
281 column: 18,
282 endLine: 1,
283 endColumn: 23
284 }
285 ]
286 },
287 {
288 code: "var a = new b[`foo`];",
289 parserOptions: { ecmaVersion: 6 },
290 errors: [
291 {
292 messageId: "lower",
293 type: "NewExpression",
294 line: 1,
295 column: 15,
296 endLine: 1,
297 endColumn: 20
298 }
299 ]
300 },
301 {
302 code: "var a = b[`\\\nFoo`]();",
303 parserOptions: { ecmaVersion: 6 },
304 errors: [
305 {
306 messageId: "upper",
307 type: "CallExpression",
308 line: 1,
309 column: 11,
310 endLine: 2,
311 endColumn: 5
312 }
313 ]
314 },
315
316 {
317 code: "var x = Foo.Bar(42);",
318 options: [{ capIsNewExceptions: ["Foo"] }],
319 errors: [{ type: "CallExpression", messageId: "upper" }]
320 },
321 {
322 code: "var x = Bar.Foo(42);",
323
324 options: [{ capIsNewExceptionPattern: "^Foo\\.." }],
325 errors: [{ type: "CallExpression", messageId: "upper" }]
326 },
327 {
328 code: "var x = new foo.bar(42);",
329 options: [{ newIsCapExceptions: ["foo"] }],
330 errors: [{ type: "NewExpression", messageId: "lower" }]
331 },
332 {
333 code: "var x = new bar.foo(42);",
334
335 options: [{ newIsCapExceptionPattern: "^foo\\.." }],
336 errors: [{ type: "NewExpression", messageId: "lower" }]
337 },
338
339 // Optional chaining
340 {
341 code: "new (foo?.bar)();",
342 parserOptions: { ecmaVersion: 2020 },
343 errors: [{ messageId: "lower", column: 11, endColumn: 14 }]
344 },
345 {
346 code: "foo?.Bar();",
347 parserOptions: { ecmaVersion: 2020 },
348 errors: [{ messageId: "upper", column: 6, endColumn: 9 }]
349 },
350 {
351 code: "(foo?.Bar)();",
352 parserOptions: { ecmaVersion: 2020 },
353 errors: [{ messageId: "upper", column: 7, endColumn: 10 }]
354 }
355 ]
356 });