/
max-statements.js
198 lines (187 loc) · 10.2 KB
/
max-statements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* @fileoverview Tests for max-statements rule.
* @author Ian Christian Myers
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/max-statements"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("max-statements", rule, {
valid: [
{ code: "function foo() { var bar = 1; function qux () { var noCount = 2; } return 3; }", options: [3] },
{ code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } else { quxx(); } return 3; }", options: [6] },
{ code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }", options: [5] },
"function foo() { var a; var b; var c; var x; var y; var z; bar(); baz(); qux(); quxx(); }",
{ code: "(function() { var bar = 1; return function () { return 42; }; })()", options: [1, { ignoreTopLevelFunctions: true }] },
{ code: "function foo() { var bar = 1; var baz = 2; }", options: [1, { ignoreTopLevelFunctions: true }] },
{ code: "define(['foo', 'qux'], function(foo, qux) { var bar = 1; var baz = 2; })", options: [1, { ignoreTopLevelFunctions: true }] },
// object property options
{ code: "var foo = { thing: function() { var bar = 1; var baz = 2; } }", options: [2] },
{ code: "var foo = { thing() { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = { ['thing']() { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = { thing: () => { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = { thing: function() { var bar = 1; var baz = 2; } }", options: [{ max: 2 }] },
// this rule does not apply to class static blocks, and statements in them should not count as statements in the enclosing function
{ code: "class C { static { one; two; three; { four; five; six; } } }", options: [2], parserOptions: { ecmaVersion: 2022 } },
{ code: "function foo() { class C { static { one; two; three; { four; five; six; } } } }", options: [2], parserOptions: { ecmaVersion: 2022 } },
{ code: "class C { static { one; two; three; function foo() { 1; 2; } four; five; six; } }", options: [2], parserOptions: { ecmaVersion: 2022 } },
{ code: "class C { static { { one; two; three; function foo() { 1; 2; } four; five; six; } } }", options: [2], parserOptions: { ecmaVersion: 2022 } },
{
code: "function top_level() { 1; /* 2 */ class C { static { one; two; three; { four; five; six; } } } 3;}",
options: [2, { ignoreTopLevelFunctions: true }],
parserOptions: { ecmaVersion: 2022 }
},
{
code: "function top_level() { 1; 2; } class C { static { one; two; three; { four; five; six; } } }",
options: [1, { ignoreTopLevelFunctions: true }],
parserOptions: { ecmaVersion: 2022 }
},
{
code: "class C { static { one; two; three; { four; five; six; } } } function top_level() { 1; 2; } ",
options: [1, { ignoreTopLevelFunctions: true }],
parserOptions: { ecmaVersion: 2022 }
},
{
code: "function foo() { let one; let two = class { static { let three; let four; let five; if (six) { let seven; let eight; let nine; } } }; }",
options: [2],
parserOptions: { ecmaVersion: 2022 }
}
],
invalid: [
{
code: "function foo() { var bar = 1; var baz = 2; var qux = 3; }",
options: [2],
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "3", max: 2 } }]
},
{
code: "var foo = () => { var bar = 1; var baz = 2; var qux = 3; };",
options: [2],
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "exceed", data: { name: "Arrow function", count: "3", max: 2 } }]
},
{
code: "var foo = function() { var bar = 1; var baz = 2; var qux = 3; };",
options: [2],
errors: [{ messageId: "exceed", data: { name: "Function", count: "3", max: 2 } }]
},
{
code: "function foo() { var bar = 1; if (true) { while (false) { var qux = null; } } return 3; }",
options: [4],
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "5", max: 4 } }]
},
{
code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } return 3; }",
options: [4],
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "5", max: 4 } }]
},
{
code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } else { quxx(); } return 3; }",
options: [5],
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "6", max: 5 } }]
},
{
code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }",
options: [3],
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "5", max: 3 } }]
},
{
code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }",
options: [4],
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "5", max: 4 } }]
},
{
code: ";(function() { var bar = 1; return function () { var z; return 42; }; })()",
options: [1, { ignoreTopLevelFunctions: true }],
errors: [{ messageId: "exceed", data: { name: "Function", count: "2", max: 1 } }]
},
{
code: ";(function() { var bar = 1; var baz = 2; })(); (function() { var bar = 1; var baz = 2; })()",
options: [1, { ignoreTopLevelFunctions: true }],
errors: [
{ messageId: "exceed", data: { name: "Function", count: "2", max: 1 } },
{ messageId: "exceed", data: { name: "Function", count: "2", max: 1 } }
]
},
{
code: "define(['foo', 'qux'], function(foo, qux) { var bar = 1; var baz = 2; return function () { var z; return 42; }; })",
options: [1, { ignoreTopLevelFunctions: true }],
errors: [{ messageId: "exceed", data: { name: "Function", count: "2", max: 1 } }]
},
{
code: "function foo() { var a; var b; var c; var x; var y; var z; bar(); baz(); qux(); quxx(); foo(); }",
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: "11", max: 10 } }]
},
// object property options
{
code: "var foo = { thing: function() { var bar = 1; var baz = 2; var baz2; } }",
options: [2],
errors: [{ messageId: "exceed", data: { name: "Method 'thing'", count: "3", max: 2 } }]
},
{
code: "var foo = { thing() { var bar = 1; var baz = 2; var baz2; } }",
options: [2],
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "exceed", data: { name: "Method 'thing'", count: "3", max: 2 } }]
},
/*
* TODO decide if we want this or not
* {
* code: "var foo = { ['thing']() { var bar = 1; var baz = 2; var baz2; } }",
* options: [2],
* parserOptions: { ecmaVersion: 6 },
* errors: [{ messageId: "exceed", data: {name: "Method ''thing''", count: "3", max: 2} }]
* },
*/
{
code: "var foo = { thing: () => { var bar = 1; var baz = 2; var baz2; } }",
options: [2],
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "exceed", data: { name: "Method 'thing'", count: "3", max: 2 } }]
},
{
code: "var foo = { thing: function() { var bar = 1; var baz = 2; var baz2; } }",
options: [{ max: 2 }],
errors: [{ messageId: "exceed", data: { name: "Method 'thing'", count: "3", max: 2 } }]
},
{
code: "function foo() { 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; }",
options: [{}],
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: 11, max: 10 } }]
},
{
code: "function foo() { 1; }",
options: [{ max: 0 }],
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: 1, max: 0 } }]
},
{
code: "function foo() { foo_1; /* foo_ 2 */ class C { static { one; two; three; four; { five; six; seven; eight; } } } foo_3 }",
options: [2],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "exceed", data: { name: "Function 'foo'", count: 3, max: 2 } }]
},
{
code: "class C { static { one; two; three; four; function not_top_level() { 1; 2; 3; } five; six; seven; eight; } }",
options: [2, { ignoreTopLevelFunctions: true }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "exceed", data: { name: "Function 'not_top_level'", count: 3, max: 2 } }]
},
{
code: "class C { static { { one; two; three; four; function not_top_level() { 1; 2; 3; } five; six; seven; eight; } } }",
options: [2, { ignoreTopLevelFunctions: true }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "exceed", data: { name: "Function 'not_top_level'", count: 3, max: 2 } }]
},
{
code: "class C { static { { one; two; three; four; } function not_top_level() { 1; 2; 3; } { five; six; seven; eight; } } }",
options: [2, { ignoreTopLevelFunctions: true }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "exceed", data: { name: "Function 'not_top_level'", count: 3, max: 2 } }]
}
]
});