]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/max-statements.js
fa3d3d1f1bda2aa37af2306a41faa65c7bec754d
[pve-eslint.git] / eslint / tests / lib / rules / max-statements.js
1 /**
2 * @fileoverview Tests for max-statements rule.
3 * @author Ian Christian Myers
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/max-statements"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("max-statements", rule, {
22 valid: [
23 { code: "function foo() { var bar = 1; function qux () { var noCount = 2; } return 3; }", options: [3] },
24 { code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } else { quxx(); } return 3; }", options: [6] },
25 { code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }", options: [5] },
26 "function foo() { var a; var b; var c; var x; var y; var z; bar(); baz(); qux(); quxx(); }",
27 { code: "(function() { var bar = 1; return function () { return 42; }; })()", options: [1, { ignoreTopLevelFunctions: true }] },
28 { code: "function foo() { var bar = 1; var baz = 2; }", options: [1, { ignoreTopLevelFunctions: true }] },
29 { code: "define(['foo', 'qux'], function(foo, qux) { var bar = 1; var baz = 2; })", options: [1, { ignoreTopLevelFunctions: true }] },
30
31 // object property options
32 { code: "var foo = { thing: function() { var bar = 1; var baz = 2; } }", options: [2] },
33 { code: "var foo = { thing() { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
34 { code: "var foo = { ['thing']() { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
35 { code: "var foo = { thing: () => { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
36 { code: "var foo = { thing: function() { var bar = 1; var baz = 2; } }", options: [{ max: 2 }] }
37 ],
38 invalid: [
39 {
40 code: "function foo() { var bar = 1; var baz = 2; var qux = 3; }",
41 options: [2],
42 errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "3", max: 2 } }]
43 },
44 {
45 code: "var foo = () => { var bar = 1; var baz = 2; var qux = 3; };",
46 options: [2],
47 parserOptions: { ecmaVersion: 6 },
48 errors: [{ messageId: "exceed", data: { name: "Arrow function", count: "3", max: 2 } }]
49 },
50 {
51 code: "var foo = function() { var bar = 1; var baz = 2; var qux = 3; };",
52 options: [2],
53 errors: [{ messageId: "exceed", data: { name: "Function", count: "3", max: 2 } }]
54 },
55 {
56 code: "function foo() { var bar = 1; if (true) { while (false) { var qux = null; } } return 3; }",
57 options: [4],
58 errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "5", max: 4 } }]
59 },
60 {
61 code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } return 3; }",
62 options: [4],
63 errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "5", max: 4 } }]
64 },
65 {
66 code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } else { quxx(); } return 3; }",
67 options: [5],
68 errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "6", max: 5 } }]
69 },
70 {
71 code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }",
72 options: [3],
73 errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "5", max: 3 } }]
74 },
75 {
76 code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }",
77 options: [4],
78 errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "5", max: 4 } }]
79 },
80 {
81 code: ";(function() { var bar = 1; return function () { var z; return 42; }; })()",
82 options: [1, { ignoreTopLevelFunctions: true }],
83 errors: [{ messageId: "exceed", data: { name: "Function", count: "2", max: 1 } }]
84 },
85 {
86 code: ";(function() { var bar = 1; var baz = 2; })(); (function() { var bar = 1; var baz = 2; })()",
87 options: [1, { ignoreTopLevelFunctions: true }],
88 errors: [
89 { messageId: "exceed", data: { name: "Function", count: "2", max: 1 } },
90 { messageId: "exceed", data: { name: "Function", count: "2", max: 1 } }
91 ]
92 },
93 {
94 code: "define(['foo', 'qux'], function(foo, qux) { var bar = 1; var baz = 2; return function () { var z; return 42; }; })",
95 options: [1, { ignoreTopLevelFunctions: true }],
96 errors: [{ messageId: "exceed", data: { name: "Function", count: "2", max: 1 } }]
97 },
98 {
99 code: "function foo() { var a; var b; var c; var x; var y; var z; bar(); baz(); qux(); quxx(); foo(); }",
100 errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "11", max: 10 } }]
101 },
102
103 // object property options
104 {
105 code: "var foo = { thing: function() { var bar = 1; var baz = 2; var baz2; } }",
106 options: [2],
107 errors: [{ messageId: "exceed", data: { name: "Method 'thing'", count: "3", max: 2 } }]
108 },
109 {
110 code: "var foo = { thing() { var bar = 1; var baz = 2; var baz2; } }",
111 options: [2],
112 parserOptions: { ecmaVersion: 6 },
113 errors: [{ messageId: "exceed", data: { name: "Method 'thing'", count: "3", max: 2 } }]
114 },
115
116 /*
117 * TODO decide if we want this or not
118 * {
119 * code: "var foo = { ['thing']() { var bar = 1; var baz = 2; var baz2; } }",
120 * options: [2],
121 * parserOptions: { ecmaVersion: 6 },
122 * errors: [{ messageId: "exceed", data: {name: "Method ''thing''", count: "3", max: 2} }]
123 * },
124 */
125
126 {
127 code: "var foo = { thing: () => { var bar = 1; var baz = 2; var baz2; } }",
128 options: [2],
129 parserOptions: { ecmaVersion: 6 },
130 errors: [{ messageId: "exceed", data: { name: "Arrow function 'thing'", count: "3", max: 2 } }]
131 },
132 {
133 code: "var foo = { thing: function() { var bar = 1; var baz = 2; var baz2; } }",
134 options: [{ max: 2 }],
135 errors: [{ messageId: "exceed", data: { name: "Method 'thing'", count: "3", max: 2 } }]
136 },
137 {
138 code: "function foo() { 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; }",
139 options: [{}],
140 errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: 11, max: 10 } }]
141 },
142 {
143 code: "function foo() { 1; }",
144 options: [{ max: 0 }],
145 errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: 1, max: 0 } }]
146 }
147 ]
148 });