forked from eslint/eslint
/
max-statements-per-line.js
163 lines (157 loc) · 13.7 KB
/
max-statements-per-line.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
/**
* @fileoverview Tests for max-statements-per-line rule.
* @author Kenneth Williams
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/max-statements-per-line"),
RuleTester = require("../../../lib/testers/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("max-statements-per-line", rule, {
valid: [
{ code: "", options: [{ max: 0 }] },
{ code: "{ }", options: [{ max: 1 }] },
{ code: "var bar = 1;" },
{ code: "var bar = 1;", options: [{ max: 1 }] },
{ code: "var bar = 1;;"},
{ code: ";(function foo() {\n})()"},
{ code: "if (condition) var bar = 1;", options: [{ max: 1 }] },
{ code: "if (condition) { }", options: [{ max: 1 }] },
{ code: "if (condition) { } else { }", options: [{ max: 1 }] },
{ code: "if (condition) {\nvar bar = 1;\n} else {\nvar bar = 1;\n}", options: [{ max: 1 }] },
{ code: "for (var i = 0; i < length; ++i) { }", options: [{ max: 1 }] },
{ code: "for (var i = 0; i < length; ++i) {\nvar bar = 1;\n}", options: [{ max: 1 }] },
{ code: "switch (discriminant) { default: }", options: [{ max: 1 }] },
{ code: "switch (discriminant) {\ndefault: break;\n}", options: [{ max: 1 }] },
{ code: "function foo() { }", options: [{ max: 1 }] },
{ code: "function foo() {\nif (condition) var bar = 1;\n}", options: [{ max: 1 }] },
{ code: "function foo() {\nif (condition) {\nvar bar = 1;\n}\n}", options: [{ max: 1 }] },
{ code: "(function() { })();", options: [{ max: 1 }] },
{ code: "(function() {\nvar bar = 1;\n})();", options: [{ max: 1 }] },
{ code: "var foo = function foo() { };", options: [{ max: 1 }] },
{ code: "var foo = function foo() {\nvar bar = 1;\n};", options: [{ max: 1 }] },
{ code: "var foo = { prop: () => { } };", options: [{ max: 1 }], parserOptions: { ecmaVersion: 6 } },
{ code: "var bar = 1; var baz = 2;", options: [{ max: 2 }] },
{ code: "if (condition) { var bar = 1; }", options: [{ max: 2 }] },
{ code: "if (condition) {\nvar bar = 1; var baz = 2;\n} else {\nvar bar = 1; var baz = 2;\n}", options: [{ max: 2 }] },
{ code: "for (var i = 0; i < length; ++i) { var bar = 1; }", options: [{ max: 2 }] },
{ code: "for (var i = 0; i < length; ++i) {\nvar bar = 1; var baz = 2;\n}", options: [{ max: 2 }] },
{ code: "switch (discriminant) { default: break; }", options: [{ max: 2 }] },
{ code: "switch (discriminant) {\ncase 'test': var bar = 1; break;\ndefault: var bar = 1; break;\n}", options: [{ max: 2 }] },
{ code: "function foo() { var bar = 1; }", options: [{ max: 2 }] },
{ code: "function foo() {\nvar bar = 1; var baz = 2;\n}", options: [{ max: 2 }] },
{ code: "function foo() {\nif (condition) { var bar = 1; }\n}", options: [{ max: 2 }] },
{ code: "function foo() {\nif (condition) {\nvar bar = 1; var baz = 2;\n}\n}", options: [{ max: 2 }] },
{ code: "(function() { var bar = 1; })();", options: [{ max: 2 }] },
{ code: "(function() {\nvar bar = 1; var baz = 2;\n})();", options: [{ max: 2 }] },
{ code: "var foo = function foo() { var bar = 1; };", options: [{ max: 2 }] },
{ code: "var foo = function foo() {\nvar bar = 1; var baz = 2;\n};", options: [{ max: 2 }] },
{ code: "var foo = { prop: () => { var bar = 1; } };", options: [{ max: 2 }], parserOptions: { ecmaVersion: 6 } },
{ code: "var bar = 1; var baz = 2; var qux = 3;", options: [{ max: 3 }] },
{ code: "if (condition) { var bar = 1; var baz = 2; }", options: [{ max: 3 }] },
{ code: "if (condition) { var bar = 1; } else { var bar = 1; }", options: [{ max: 3 }] },
{ code: "switch (discriminant) { case 'test1': ; case 'test2': ; }", options: [{ max: 3 }] },
{ code: "let bar = bar => { a; }, baz = baz => { b; };", options: [{ max: 3 }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo({[bar => { a; }]: baz = qux => { b; }}) { }", options: [{ max: 3 }], parserOptions: { ecmaVersion: 6 } },
{ code: "bar => { a; }, baz => { b; }, qux => { c; };", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 } },
{ code: "[bar => { a; }, baz => { b; }, qux => { c; }];", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 } },
{ code: "foo(bar => { a; }, baz => { c; }, qux => { c; });", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 } },
{ code: "({ bar: bar => { a; }, baz: baz => { c; }, qux: qux => { ; }});", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 } },
{ code: "(bar => { a; }) ? (baz => { b; }) : (qux => { c; });", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 } },
{
code: [
"const name = 'ESLint'",
"",
";(function foo() {",
"})()"
].join("\n"),
options: [{max: 1}],
parserOptions: {ecmaVersion: 6}
},
{
code: [
"if (foo > 1)",
" foo--;",
"else",
" foo++;"
].join("\n")
},
{
code: "export default foo = 0;",
options: [{max: 1}],
parserOptions: {ecmaVersion: 6, sourceType: "module"}
},
{
code: [
"export default function foo() {",
" console.log('test');",
"}"
].join("\n"),
options: [{max: 1}],
parserOptions: {ecmaVersion: 6, sourceType: "module"}
},
{
code: "export let foo = 0;",
options: [{max: 1}],
parserOptions: {ecmaVersion: 6, sourceType: "module"}
},
{
code: [
"export function foo() {",
" console.log('test');",
"}"
].join("\n"),
options: [{max: 1}],
parserOptions: {ecmaVersion: 6, sourceType: "module"}
}
],
invalid: [
{ code: "{ }", options: [{ max: 0 }], errors: [{ message: "This line has an empty block, but the maximum allowed is 0." }] },
{ code: "var bar = 1;", options: [{ max: 0 }], errors: [{ message: "This line has 1 statement(s). Maximum allowed is 0." }] },
{ code: "var bar = 1; var baz = 2;", errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "var bar = 1; var baz = 2;", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) var bar = 1; if (condition) var baz = 2;", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) var bar = 1; else var baz = 1;", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) { } if (condition) { }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) { var bar = 1; } else { }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) { } else { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) { var bar = 1; } else { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 1." }] },
{ code: "for (var i = 0; i < length; ++i) { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "switch (discriminant) { default: break; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "function foo() { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "function foo() { if (condition) var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "function foo() { if (condition) { var bar = 1; } }", options: [{ max: 1 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 1." }] },
{ code: "(function() { var bar = 1; })();", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "var foo = function foo() { var bar = 1; };", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "var foo = { prop: () => { var bar = 1; } };", options: [{ max: 1 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "var bar = 1; var baz = 2; var qux = 3;", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "if (condition) { var bar = 1; } else { var bar = 1; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; } else { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has 5 statement(s). Maximum allowed is 2." }] },
{ code: "for (var i = 0; i < length; ++i) { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "switch (discriminant) { case 'test': break; default: break; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "function foo() { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "function foo() { if (condition) { var bar = 1; } }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "(function() { var bar = 1; var baz = 2; })();", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "var foo = function foo() { var bar = 1; var baz = 2; };", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "var foo = { prop: () => { var bar = 1; var baz = 2; } };", options: [{ max: 2 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "var bar = 1; var baz = 2; var qux = 3; var waldo = 4;", options: [{ max: 3 }], errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; var qux = 3; }", options: [{ max: 3 }], errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; } else { var bar = 1; var baz = 2; }", options: [{ max: 3 }], errors: [{ message: "This line has 5 statement(s). Maximum allowed is 3." }] },
{ code: "switch (discriminant) { case 'test': var bar = 1; break; default: var bar = 1; break; }", options: [{ max: 3 }], errors: [{ message: "This line has 5 statement(s). Maximum allowed is 3." }] },
{ code: "let bar = bar => { a; }, baz = baz => { b; }, qux = qux => { c; };", options: [{ max: 3 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "(bar => { a; }) ? (baz => { b; }) : (qux => { c; });", options: [{ max: 3 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "bar => { a; }, baz => { b; }, qux => { c; }, quux => { d; };", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 5 statement(s). Maximum allowed is 4." }] },
{ code: "[bar => { a; }, baz => { b; }, qux => { c; }, quux => { d; }];", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 5 statement(s). Maximum allowed is 4." }] },
{ code: "foo(bar => { a; }, baz => { b; }, qux => { c; }, quux => { d; });", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 5 statement(s). Maximum allowed is 4." }] },
{ code: "({ bar: bar => { a; }, baz: baz => { b; }, qux: qux => { c; }, quux: quux => { d; }});", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 5 statement(s). Maximum allowed is 4." }] },
{ code: "a; if (b) { c; d; }\nz;", options: [{ max: 2 }], errors: [{ message: "This line has 4 statement(s). Maximum allowed is 2." }] },
{ code: "export default function foo() { console.log('test') }", options: [{ max: 1 }], parserOptions: {ecmaVersion: 6, sourceType: "module"}, errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "export function foo() { console.log('test') }", options: [{ max: 1 }], parserOptions: {ecmaVersion: 6, sourceType: "module"}, errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] }
]
});