]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/max-nested-callbacks.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / max-nested-callbacks.js
1 /**
2 * @fileoverview Tests for max-nested-callbacks rule.
3 * @author Ian Christian Myers
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/max-nested-callbacks"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Helpers
17 //------------------------------------------------------------------------------
18
19 const OPENING = "foo(function() {",
20 CLOSING = "});";
21
22 /**
23 * Generates a code string with the specified number of nested callbacks.
24 * @param {int} times The number of times to nest the callbacks.
25 * @returns {string} Code with the specified number of nested callbacks
26 * @private
27 */
28 function nestFunctions(times) {
29 let openings = "",
30 closings = "";
31
32 for (let i = 0; i < times; i++) {
33 openings += OPENING;
34 closings += CLOSING;
35 }
36 return openings + closings;
37 }
38
39 //------------------------------------------------------------------------------
40 // Tests
41 //------------------------------------------------------------------------------
42 const ruleTester = new RuleTester();
43
44 ruleTester.run("max-nested-callbacks", rule, {
45 valid: [
46 { code: "foo(function() { bar(thing, function(data) {}); });", options: [3] },
47 { code: "var foo = function() {}; bar(function(){ baz(function() { qux(foo); }) });", options: [2] },
48 { code: "fn(function(){}, function(){}, function(){});", options: [2] },
49 { code: "fn(() => {}, function(){}, function(){});", options: [2], parserOptions: { ecmaVersion: 6 } },
50 nestFunctions(10),
51
52 // object property options
53 { code: "foo(function() { bar(thing, function(data) {}); });", options: [{ max: 3 }] }
54 ],
55 invalid: [
56 {
57 code: "foo(function() { bar(thing, function(data) { baz(function() {}); }); });",
58 options: [2],
59 errors: [{ messageId: "exceed", data: { num: 3, max: 2 }, type: "FunctionExpression" }]
60 },
61 {
62 code: "foo(function() { bar(thing, (data) => { baz(function() {}); }); });",
63 options: [2],
64 parserOptions: { ecmaVersion: 6 },
65 errors: [{ messageId: "exceed", data: { num: 3, max: 2 }, type: "FunctionExpression" }]
66 },
67 {
68 code: "foo(() => { bar(thing, (data) => { baz( () => {}); }); });",
69 options: [2],
70 parserOptions: { ecmaVersion: 6 },
71 errors: [{ messageId: "exceed", data: { num: 3, max: 2 }, type: "ArrowFunctionExpression" }]
72 },
73 {
74 code: "foo(function() { if (isTrue) { bar(function(data) { baz(function() {}); }); } });",
75 options: [2],
76 errors: [{ messageId: "exceed", data: { num: 3, max: 2 }, type: "FunctionExpression" }]
77 },
78 {
79 code: nestFunctions(11),
80 errors: [{ messageId: "exceed", data: { num: 11, max: 10 }, type: "FunctionExpression" }]
81 },
82 {
83 code: nestFunctions(11),
84 options: [{}],
85 errors: [{ messageId: "exceed", data: { num: 11, max: 10 }, type: "FunctionExpression" }]
86 },
87 {
88 code: "foo(function() {})",
89 options: [{ max: 0 }],
90 errors: [{ messageId: "exceed", data: { num: 1, max: 0 } }]
91 },
92
93 // object property options
94 {
95 code: "foo(function() { bar(thing, function(data) { baz(function() {}); }); });",
96 options: [{ max: 2 }],
97 errors: [{ messageId: "exceed", data: { num: 3, max: 2 }, type: "FunctionExpression" }]
98 }
99 ]
100 });