]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/new-cap.js
ec85c4bf6fd0eb2e878b89a55ac90c5a5648a84a
[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 invalid: [
77 {
78 code: "var x = new c();",
79 errors: [{
80 messageId: "lower",
81 type: "NewExpression",
82 line: 1,
83 column: 13,
84 endLine: 1,
85 endColumn: 14
86 }]
87 },
88 {
89 code: "var x = new φ;",
90 errors: [{
91 messageId: "lower",
92 type: "NewExpression",
93 line: 1,
94 column: 13,
95 endLine: 1,
96 endColumn: 14
97 }]
98 },
99 {
100 code: "var x = new a.b.c;",
101 errors: [{
102 messageId: "lower",
103 type: "NewExpression",
104 line: 1,
105 column: 17,
106 endLine: 1,
107 endColumn: 18
108 }]
109 },
110 {
111 code: "var x = new a.b['c'];",
112 errors: [{
113 messageId: "lower",
114 type: "NewExpression",
115 line: 1,
116 column: 17,
117 endLine: 1,
118 endColumn: 20
119 }]
120 },
121 {
122 code: "var b = Foo();",
123 errors: [{
124 messageId: "upper",
125 type: "CallExpression",
126 line: 1,
127 column: 9,
128 endLine: 1,
129 endColumn: 12
130 }]
131 },
132 {
133 code: "var b = a.Foo();",
134 errors: [{
135 messageId: "upper",
136 type: "CallExpression",
137 line: 1,
138 column: 11,
139 endLine: 1,
140 endColumn: 14
141 }]
142 },
143 {
144 code: "var b = a['Foo']();",
145 errors: [{
146 messageId: "upper",
147 type: "CallExpression",
148 line: 1,
149 column: 11,
150 endLine: 1,
151 endColumn: 16
152 }]
153 },
154 {
155 code: "var b = a.Date.UTC();",
156 errors: [{
157 messageId: "upper",
158 type: "CallExpression",
159 line: 1,
160 column: 16,
161 endLine: 1,
162 endColumn: 19
163 }]
164 },
165 {
166 code: "var b = UTC();",
167 errors: [{
168 messageId: "upper",
169 type: "CallExpression",
170 line: 1,
171 column: 9,
172 endLine: 1,
173 endColumn: 12
174 }]
175 },
176 {
177 code: "var a = B.C();",
178 errors: [
179 {
180 messageId: "upper",
181 type: "CallExpression",
182 line: 1,
183 column: 11,
184 endLine: 1,
185 endColumn: 12
186 }
187 ]
188 },
189 {
190 code: "var a = B\n.C();",
191 errors: [
192 {
193 messageId: "upper",
194 type: "CallExpression",
195 line: 2,
196 column: 2,
197 endLine: 2,
198 endColumn: 3
199 }
200 ]
201 },
202 {
203 code: "var a = new B.c();",
204 errors: [
205 {
206 messageId: "lower",
207 type: "NewExpression",
208 line: 1,
209 column: 15,
210 endLine: 1,
211 endColumn: 16
212 }
213 ]
214 },
215 {
216 code: "var a = new B.\nc();",
217 errors: [
218 {
219 messageId: "lower",
220 type: "NewExpression",
221 line: 2,
222 column: 1,
223 endLine: 2,
224 endColumn: 2
225 }
226 ]
227 },
228 {
229 code: "var a = new c();",
230 errors: [
231 {
232 messageId: "lower",
233 type: "NewExpression",
234 line: 1,
235 column: 13,
236 endLine: 1,
237 endColumn: 14
238 }
239 ]
240 },
241 {
242 code: "var a = new b[ ( 'foo' ) ]();",
243 parserOptions: { ecmaVersion: 6 },
244 errors: [
245 {
246 messageId: "lower",
247 type: "NewExpression",
248 line: 1,
249 column: 18,
250 endLine: 1,
251 endColumn: 23
252 }
253 ]
254 },
255 {
256 code: "var a = new b[`foo`];",
257 parserOptions: { ecmaVersion: 6 },
258 errors: [
259 {
260 messageId: "lower",
261 type: "NewExpression",
262 line: 1,
263 column: 15,
264 endLine: 1,
265 endColumn: 20
266 }
267 ]
268 },
269 {
270 code: "var a = b[`\\\nFoo`]();",
271 parserOptions: { ecmaVersion: 6 },
272 errors: [
273 {
274 messageId: "upper",
275 type: "CallExpression",
276 line: 1,
277 column: 11,
278 endLine: 2,
279 endColumn: 5
280 }
281 ]
282 },
283
284 {
285 code: "var x = Foo.Bar(42);",
286 options: [{ capIsNewExceptions: ["Foo"] }],
287 errors: [{ type: "CallExpression", messageId: "upper" }]
288 },
289 {
290 code: "var x = Bar.Foo(42);",
291
292 options: [{ capIsNewExceptionPattern: "^Foo\\.." }],
293 errors: [{ type: "CallExpression", messageId: "upper" }]
294 },
295 {
296 code: "var x = new foo.bar(42);",
297 options: [{ newIsCapExceptions: ["foo"] }],
298 errors: [{ type: "NewExpression", messageId: "lower" }]
299 },
300 {
301 code: "var x = new bar.foo(42);",
302
303 options: [{ newIsCapExceptionPattern: "^foo\\.." }],
304 errors: [{ type: "NewExpression", messageId: "lower" }]
305 }
306 ]
307 });