]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/func-name-matching.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / func-name-matching.js
1 /**
2 * @fileoverview Tests for func-name-matching rule.
3 * @author Annie Zhang
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/func-name-matching"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("func-name-matching", rule, {
22 valid: [
23 "var foo;",
24 "var foo = function foo() {};",
25 { code: "var foo = function foo() {};", options: ["always"] },
26 { code: "var foo = function bar() {};", options: ["never"] },
27 "var foo = function() {}",
28 { code: "var foo = () => {}", parserOptions: { ecmaVersion: 6 } },
29 "foo = function foo() {};",
30 { code: "foo = function foo() {};", options: ["always"] },
31 { code: "foo = function bar() {};", options: ["never"] },
32 "obj.foo = function foo() {};",
33 { code: "obj.foo = function foo() {};", options: ["always"] },
34 { code: "obj.foo = function bar() {};", options: ["never"] },
35 "obj.foo = function() {};",
36 { code: "obj.foo = function() {};", options: ["always"] },
37 { code: "obj.foo = function() {};", options: ["never"] },
38 "obj.bar.foo = function foo() {};",
39 { code: "obj.bar.foo = function foo() {};", options: ["always"] },
40 { code: "obj.bar.foo = function baz() {};", options: ["never"] },
41 "obj['foo'] = function foo() {};",
42 { code: "obj['foo'] = function foo() {};", options: ["always"] },
43 { code: "obj['foo'] = function bar() {};", options: ["never"] },
44 "obj['foo//bar'] = function foo() {};",
45 { code: "obj['foo//bar'] = function foo() {};", options: ["always"] },
46 { code: "obj['foo//bar'] = function foo() {};", options: ["never"] },
47 "obj[foo] = function bar() {};",
48 { code: "obj[foo] = function bar() {};", options: ["always"] },
49 { code: "obj[foo] = function bar() {};", options: ["never"] },
50 "var obj = {foo: function foo() {}};",
51 { code: "var obj = {foo: function foo() {}};", options: ["always"] },
52 { code: "var obj = {foo: function bar() {}};", options: ["never"] },
53 "var obj = {'foo': function foo() {}};",
54 { code: "var obj = {'foo': function foo() {}};", options: ["always"] },
55 { code: "var obj = {'foo': function bar() {}};", options: ["never"] },
56 "var obj = {'foo//bar': function foo() {}};",
57 { code: "var obj = {'foo//bar': function foo() {}};", options: ["always"] },
58 { code: "var obj = {'foo//bar': function foo() {}};", options: ["never"] },
59 "var obj = {foo: function() {}};",
60 { code: "var obj = {foo: function() {}};", options: ["always"] },
61 { code: "var obj = {foo: function() {}};", options: ["never"] },
62 { code: "var obj = {[foo]: function bar() {}} ", parserOptions: { ecmaVersion: 6 } },
63 { code: "var obj = {['x' + 2]: function bar(){}};", parserOptions: { ecmaVersion: 6 } },
64 "obj['x' + 2] = function bar(){};",
65 { code: "var [ bar ] = [ function bar(){} ];", parserOptions: { ecmaVersion: 6 } },
66 { code: "function a(foo = function bar() {}) {}", parserOptions: { ecmaVersion: 6 } },
67 "module.exports = function foo(name) {};",
68 "module['exports'] = function foo(name) {};",
69 {
70 code: "module.exports = function foo(name) {};",
71 options: [{ includeCommonJSModuleExports: false }],
72 parserOptions: { ecmaVersion: 6 }
73 },
74 {
75 code: "module.exports = function foo(name) {};",
76 options: ["always", { includeCommonJSModuleExports: false }],
77 parserOptions: { ecmaVersion: 6 }
78 },
79 {
80 code: "module.exports = function foo(name) {};",
81 options: ["never", { includeCommonJSModuleExports: false }],
82 parserOptions: { ecmaVersion: 6 }
83 },
84 {
85 code: "module['exports'] = function foo(name) {};",
86 options: [{ includeCommonJSModuleExports: false }],
87 parserOptions: { ecmaVersion: 6 }
88 },
89 {
90 code: "module['exports'] = function foo(name) {};",
91 options: ["always", { includeCommonJSModuleExports: false }],
92 parserOptions: { ecmaVersion: 6 }
93 },
94 {
95 code: "module['exports'] = function foo(name) {};",
96 options: ["never", { includeCommonJSModuleExports: false }],
97 parserOptions: { ecmaVersion: 6 }
98 },
99 {
100 code: "({['foo']: function foo() {}})",
101 parserOptions: { ecmaVersion: 6 }
102 },
103 {
104 code: "({['foo']: function foo() {}})",
105 options: ["always"],
106 parserOptions: { ecmaVersion: 6 }
107 },
108 {
109 code: "({['foo']: function bar() {}})",
110 options: ["never"],
111 parserOptions: { ecmaVersion: 6 }
112 },
113 {
114 code: "({['❤']: function foo() {}})",
115 parserOptions: { ecmaVersion: 6 }
116 },
117 {
118 code: "({[foo]: function bar() {}})",
119 parserOptions: { ecmaVersion: 6 }
120 },
121 {
122 code: "({[null]: function foo() {}})",
123 parserOptions: { ecmaVersion: 6 }
124 },
125 {
126 code: "({[1]: function foo() {}})",
127 parserOptions: { ecmaVersion: 6 }
128 },
129 {
130 code: "({[true]: function foo() {}})",
131 parserOptions: { ecmaVersion: 6 }
132 },
133 {
134 code: "({[`x`]: function foo() {}})",
135 parserOptions: { ecmaVersion: 6 }
136 },
137 {
138 code: "({[/abc/]: function foo() {}})",
139 parserOptions: { ecmaVersion: 6 }
140 },
141 {
142 code: "({[[1, 2, 3]]: function foo() {}})",
143 parserOptions: { ecmaVersion: 6 }
144 },
145 {
146 code: "({[{x: 1}]: function foo() {}})",
147 parserOptions: { ecmaVersion: 6 }
148 },
149 {
150 code: "[] = function foo() {}",
151 parserOptions: { ecmaVersion: 6 }
152 },
153 {
154 code: "({} = function foo() {})",
155 parserOptions: { ecmaVersion: 6 }
156 },
157 {
158 code: "[a] = function foo() {}",
159 parserOptions: { ecmaVersion: 6 }
160 },
161 {
162 code: "({a} = function foo() {})",
163 parserOptions: { ecmaVersion: 6 }
164 },
165 {
166 code: "var [] = function foo() {}",
167 parserOptions: { ecmaVersion: 6 }
168 },
169 {
170 code: "var {} = function foo() {}",
171 parserOptions: { ecmaVersion: 6 }
172 },
173 {
174 code: "var [a] = function foo() {}",
175 parserOptions: { ecmaVersion: 6 }
176 },
177 {
178 code: "var {a} = function foo() {}",
179 parserOptions: { ecmaVersion: 6 }
180 },
181 {
182 code: "({ value: function value() {} })",
183 options: [{ considerPropertyDescriptor: true }]
184 },
185 {
186 code: "obj.foo = function foo() {};",
187 options: ["always", { considerPropertyDescriptor: true }]
188 },
189 {
190 code: "obj.bar.foo = function foo() {};",
191 options: ["always", { considerPropertyDescriptor: true }]
192 },
193 {
194 code: "var obj = {foo: function foo() {}};",
195 options: ["always", { considerPropertyDescriptor: true }]
196 },
197 {
198 code: "var obj = {foo: function() {}};",
199 options: ["always", { considerPropertyDescriptor: true }]
200 },
201 {
202 code: "var obj = { value: function value() {} }",
203 options: ["always", { considerPropertyDescriptor: true }]
204 },
205 {
206 code: "Object.defineProperty(foo, 'bar', { value: function bar() {} })",
207 options: ["always", { considerPropertyDescriptor: true }]
208 },
209 {
210 code: "Object.defineProperties(foo, { bar: { value: function bar() {} } })",
211 options: ["always", { considerPropertyDescriptor: true }]
212 },
213 {
214 code: "Object.create(proto, { bar: { value: function bar() {} } })",
215 options: ["always", { considerPropertyDescriptor: true }]
216 },
217 {
218 code: "Object.defineProperty(foo, 'b' + 'ar', { value: function bar() {} })",
219 options: ["always", { considerPropertyDescriptor: true }]
220 },
221 {
222 code: "Object.defineProperties(foo, { ['bar']: { value: function bar() {} } })",
223 options: ["always", { considerPropertyDescriptor: true }],
224 parserOptions: { ecmaVersion: 6 }
225 },
226 {
227 code: "Object.create(proto, { ['bar']: { value: function bar() {} } })",
228 options: ["always", { considerPropertyDescriptor: true }],
229 parserOptions: { ecmaVersion: 6 }
230 },
231 {
232 code: "Object.defineProperty(foo, 'bar', { value() {} })",
233 options: ["never", { considerPropertyDescriptor: true }],
234 parserOptions: { ecmaVersion: 6 }
235 },
236 {
237 code: "Object.defineProperties(foo, { bar: { value() {} } })",
238 options: ["never", { considerPropertyDescriptor: true }],
239 parserOptions: { ecmaVersion: 6 }
240 },
241 {
242 code: "Object.create(proto, { bar: { value() {} } })",
243 options: ["never", { considerPropertyDescriptor: true }],
244 parserOptions: { ecmaVersion: 6 }
245 },
246 {
247 code: "Reflect.defineProperty(foo, 'bar', { value: function bar() {} })",
248 options: ["always", { considerPropertyDescriptor: true }]
249 },
250 {
251 code: "Reflect.defineProperty(foo, 'b' + 'ar', { value: function baz() {} })",
252 options: ["always", { considerPropertyDescriptor: true }]
253 },
254 {
255 code: "Reflect.defineProperty(foo, 'bar', { value() {} })",
256 options: ["never", { considerPropertyDescriptor: true }],
257 parserOptions: { ecmaVersion: 6 }
258 },
259 {
260 code: "foo({ value: function value() {} })",
261 options: ["always", { considerPropertyDescriptor: true }]
262 }
263 ],
264 invalid: [
265 {
266 code: "let foo = function bar() {};",
267 options: ["always"],
268 parserOptions: { ecmaVersion: 6 },
269 errors: [
270 { messageId: "matchVariable", data: { funcName: "bar", name: "foo" } }
271 ]
272 },
273 {
274 code: "let foo = function bar() {};",
275 parserOptions: { ecmaVersion: 6 },
276 errors: [
277 { messageId: "matchVariable", data: { funcName: "bar", name: "foo" } }
278 ]
279 },
280 {
281 code: "foo = function bar() {};",
282 parserOptions: { ecmaVersion: 6 },
283 errors: [
284 { messageId: "matchVariable", data: { funcName: "bar", name: "foo" } }
285 ]
286 },
287 {
288 code: "obj.foo = function bar() {};",
289 parserOptions: { ecmaVersion: 6 },
290 errors: [
291 { messageId: "matchProperty", data: { funcName: "bar", name: "foo" } }
292 ]
293 },
294 {
295 code: "obj.bar.foo = function bar() {};",
296 parserOptions: { ecmaVersion: 6 },
297 errors: [
298 { messageId: "matchProperty", data: { funcName: "bar", name: "foo" } }
299 ]
300 },
301 {
302 code: "obj['foo'] = function bar() {};",
303 parserOptions: { ecmaVersion: 6 },
304 errors: [
305 { messageId: "matchProperty", data: { funcName: "bar", name: "foo" } }
306 ]
307 },
308 {
309 code: "let obj = {foo: function bar() {}};",
310 parserOptions: { ecmaVersion: 6 },
311 errors: [
312 { messageId: "matchProperty", data: { funcName: "bar", name: "foo" } }
313 ]
314 },
315 {
316 code: "let obj = {'foo': function bar() {}};",
317 parserOptions: { ecmaVersion: 6 },
318 errors: [
319 { messageId: "matchProperty", data: { funcName: "bar", name: "foo" } }
320 ]
321 },
322 {
323 code: "({['foo']: function bar() {}})",
324 parserOptions: { ecmaVersion: 6 },
325 errors: [
326 { messageId: "matchProperty", data: { funcName: "bar", name: "foo" } }
327 ]
328 },
329 {
330 code: "module.exports = function foo(name) {};",
331 options: [{ includeCommonJSModuleExports: true }],
332 parserOptions: { ecmaVersion: 6 },
333 errors: [
334 { messageId: "matchProperty", data: { funcName: "foo", name: "exports" } }
335 ]
336 },
337 {
338 code: "module.exports = function foo(name) {};",
339 options: ["always", { includeCommonJSModuleExports: true }],
340 parserOptions: { ecmaVersion: 6 },
341 errors: [
342 { messageId: "matchProperty", data: { funcName: "foo", name: "exports" } }
343 ]
344 },
345 {
346 code: "module.exports = function exports(name) {};",
347 options: ["never", { includeCommonJSModuleExports: true }],
348 parserOptions: { ecmaVersion: 6 },
349 errors: [
350 { messageId: "notMatchProperty", data: { funcName: "exports", name: "exports" } }
351 ]
352 },
353 {
354 code: "module['exports'] = function foo(name) {};",
355 options: [{ includeCommonJSModuleExports: true }],
356 parserOptions: { ecmaVersion: 6 },
357 errors: [
358 { messageId: "matchProperty", data: { funcName: "foo", name: "exports" } }
359 ]
360 },
361 {
362 code: "module['exports'] = function foo(name) {};",
363 options: ["always", { includeCommonJSModuleExports: true }],
364 parserOptions: { ecmaVersion: 6 },
365 errors: [
366 { messageId: "matchProperty", data: { funcName: "foo", name: "exports" } }
367 ]
368 },
369 {
370 code: "module['exports'] = function exports(name) {};",
371 options: ["never", { includeCommonJSModuleExports: true }],
372 parserOptions: { ecmaVersion: 6 },
373 errors: [
374 { messageId: "notMatchProperty", data: { funcName: "exports", name: "exports" } }
375 ]
376 },
377 {
378 code: "var foo = function foo(name) {};",
379 options: ["never"],
380 errors: [
381 { messageId: "notMatchVariable", data: { funcName: "foo", name: "foo" } }
382 ]
383 },
384 {
385 code: "obj.foo = function foo(name) {};",
386 options: ["never"],
387 errors: [
388 { messageId: "notMatchProperty", data: { funcName: "foo", name: "foo" } }
389 ]
390 },
391 {
392 code: "Object.defineProperty(foo, 'bar', { value: function baz() {} })",
393 options: ["always", { considerPropertyDescriptor: true }],
394 errors: [
395 { messageId: "matchProperty", data: { funcName: "baz", name: "bar" } }
396 ]
397 },
398 {
399 code: "Object.defineProperties(foo, { bar: { value: function baz() {} } })",
400 options: ["always", { considerPropertyDescriptor: true }],
401 errors: [
402 { messageId: "matchProperty", data: { funcName: "baz", name: "bar" } }
403 ]
404 },
405 {
406 code: "Object.create(proto, { bar: { value: function baz() {} } })",
407 options: ["always", { considerPropertyDescriptor: true }],
408 errors: [
409 { messageId: "matchProperty", data: { funcName: "baz", name: "bar" } }
410 ]
411 },
412 {
413 code: "var obj = { value: function foo(name) {} }",
414 options: ["always", { considerPropertyDescriptor: true }],
415 errors: [
416 { messageId: "matchProperty", data: { funcName: "foo", name: "value" } }
417 ]
418 },
419 {
420 code: "Object.defineProperty(foo, 'bar', { value: function bar() {} })",
421 options: ["never", { considerPropertyDescriptor: true }],
422 errors: [
423 { messageId: "notMatchProperty", data: { funcName: "bar", name: "bar" } }
424 ]
425 },
426 {
427 code: "Object.defineProperties(foo, { bar: { value: function bar() {} } })",
428 options: ["never", { considerPropertyDescriptor: true }],
429 errors: [
430 { messageId: "notMatchProperty", data: { funcName: "bar", name: "bar" } }
431 ]
432 },
433 {
434 code: "Object.create(proto, { bar: { value: function bar() {} } })",
435 options: ["never", { considerPropertyDescriptor: true }],
436 errors: [
437 { messageId: "notMatchProperty", data: { funcName: "bar", name: "bar" } }
438 ]
439 },
440 {
441 code: "Reflect.defineProperty(foo, 'bar', { value: function baz() {} })",
442 options: ["always", { considerPropertyDescriptor: true }],
443 errors: [
444 { messageId: "matchProperty", data: { funcName: "baz", name: "bar" } }
445 ]
446 },
447 {
448 code: "Reflect.defineProperty(foo, 'bar', { value: function bar() {} })",
449 options: ["never", { considerPropertyDescriptor: true }],
450 errors: [
451 { messageId: "notMatchProperty", data: { funcName: "bar", name: "bar" } }
452 ]
453 },
454 {
455 code: "foo({ value: function bar() {} })",
456 options: ["always", { considerPropertyDescriptor: true }],
457 errors: [
458 { messageId: "matchProperty", data: { funcName: "bar", name: "value" } }
459 ]
460 }
461 ]
462 });