forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-else-return.js
213 lines (207 loc) · 10.4 KB
/
no-else-return.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @fileoverview Tests for no-else-return rule.
* @author Ian Christian Myers
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-else-return"),
RuleTester = require("../../../lib/testers/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-else-return", rule, {
valid: [
"function foo() { if (true) { if (false) { return x; } } else { return y; } }",
"function foo() { if (true) { return x; } return y; }",
"function foo() { if (true) { for (;;) { return x; } } else { return y; } }",
"function foo() { var x = true; if (x) { return x; } else if (x === false) { return false; } }",
"function foo() { if (true) notAReturn(); else return y; }",
"function foo() {if (x) { notAReturn(); } else if (y) { return true; } else { notAReturn(); } }",
"function foo() {if (x) { return true; } else if (y) { notAReturn() } else { notAReturn(); } }",
"if (0) { if (0) {} else {} } else {}",
`
function foo() {
if (foo)
if (bar) return;
else baz;
else qux;
}
`,
`
function foo() {
while (foo)
if (bar) return;
else baz;
}
`,
{
code: "function foo19() { if (true) { return x; } else if (false) { return y; } }",
options: [{ allowElseIf: true }]
},
{
code: "function foo20() {if (x) { return true; } else if (y) { notAReturn() } else { notAReturn(); } }",
options: [{ allowElseIf: true }]
},
{
code: "function foo21() { var x = true; if (x) { return x; } else if (x === false) { return false; } }",
options: [{ allowElseIf: true }]
}
],
invalid: [
{
code: "function foo1() { if (true) { return x; } else { return y; } }",
output: "function foo1() { if (true) { return x; } return y; }",
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo2() { if (true) { var x = bar; return x; } else { var y = baz; return y; } }",
output: "function foo2() { if (true) { var x = bar; return x; } var y = baz; return y; }",
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo3() { if (true) return x; else return y; }",
output: "function foo3() { if (true) return x; return y; }",
errors: [{ messageId: "unexpected", type: "ReturnStatement" }]
},
{
code: "function foo4() { if (true) { if (false) return x; else return y; } else { return z; } }",
output: "function foo4() { if (true) { if (false) return x; return y; } else { return z; } }", // Other case is fixed in the second pass.
errors: [{ messageId: "unexpected", type: "ReturnStatement" }, { messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo5() { if (true) { if (false) { if (true) return x; else { w = y; } } else { w = x; } } else { return z; } }",
output: "function foo5() { if (true) { if (false) { if (true) return x; w = y; } else { w = x; } } else { return z; } }",
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo6() { if (true) { if (false) { if (true) return x; else return y; } } else { return z; } }",
output: "function foo6() { if (true) { if (false) { if (true) return x; return y; } } else { return z; } }",
errors: [{ messageId: "unexpected", type: "ReturnStatement" }]
},
{
code: "function foo7() { if (true) { if (false) { if (true) return x; else return y; } return w; } else { return z; } }",
output: "function foo7() { if (true) { if (false) { if (true) return x; return y; } return w; } else { return z; } }", // Other case is fixed in the second pass.
errors: [
{ messageId: "unexpected", type: "ReturnStatement" },
{ messageId: "unexpected", type: "BlockStatement" }
]
},
{
code: "function foo8() { if (true) { if (false) { if (true) return x; else return y; } else { w = x; } } else { return z; } }",
output: "function foo8() { if (true) { if (false) { if (true) return x; return y; } else { w = x; } } else { return z; } }", // Other case is fixed in the second pass.
errors: [
{ messageId: "unexpected", type: "ReturnStatement" },
{ messageId: "unexpected", type: "BlockStatement" }
]
},
{
code: "function foo9() {if (x) { return true; } else if (y) { return true; } else { notAReturn(); } }",
output: "function foo9() {if (x) { return true; } else if (y) { return true; } notAReturn(); }",
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo9a() {if (x) { return true; } else if (y) { return true; } else { notAReturn(); } }",
output: "function foo9a() {if (x) { return true; } if (y) { return true; } else { notAReturn(); } }",
options: [{ allowElseIf: false }],
errors: [{ messageId: "unexpected", type: "IfStatement" }]
},
{
code: "function foo9b() {if (x) { return true; } if (y) { return true; } else { notAReturn(); } }",
output: "function foo9b() {if (x) { return true; } if (y) { return true; } notAReturn(); }",
options: [{ allowElseIf: false }],
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo10() { if (foo) return bar; else (foo).bar(); }",
output: "function foo10() { if (foo) return bar; (foo).bar(); }",
errors: [{ messageId: "unexpected", type: "ExpressionStatement" }]
},
{
code: "function foo11() { if (foo) return bar \nelse { [1, 2, 3].map(foo) } }",
output: null,
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo12() { if (foo) return bar \nelse { baz() } \n[1, 2, 3].map(foo) }",
output: null,
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo13() { if (foo) return bar; \nelse { [1, 2, 3].map(foo) } }",
output: "function foo13() { if (foo) return bar; \n [1, 2, 3].map(foo) }",
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo14() { if (foo) return bar \nelse { baz(); } \n[1, 2, 3].map(foo) }",
output: "function foo14() { if (foo) return bar \n baz(); \n[1, 2, 3].map(foo) }",
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo15() { if (foo) return bar; else { baz() } qaz() }",
output: null,
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo16() { if (foo) return bar \nelse { baz() } qaz() }",
output: null,
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo17() { if (foo) return bar \nelse { baz() } \nqaz() }",
output: "function foo17() { if (foo) return bar \n baz() \nqaz() }",
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo18() { if (foo) return function() {} \nelse [1, 2, 3].map(bar) }",
output: null,
errors: [{ messageId: "unexpected", type: "ExpressionStatement" }]
},
{
code: "function foo19() { if (true) { return x; } else if (false) { return y; } }",
output: "function foo19() { if (true) { return x; } if (false) { return y; } }",
options: [{ allowElseIf: false }],
errors: [{ messageId: "unexpected", type: "IfStatement" }]
},
{
code: "function foo20() {if (x) { return true; } else if (y) { notAReturn() } else { notAReturn(); } }",
output: "function foo20() {if (x) { return true; } if (y) { notAReturn() } else { notAReturn(); } }",
options: [{ allowElseIf: false }],
errors: [{ messageId: "unexpected", type: "IfStatement" }]
},
{
code: "function foo21() { var x = true; if (x) { return x; } else if (x === false) { return false; } }",
output: "function foo21() { var x = true; if (x) { return x; } if (x === false) { return false; } }",
options: [{ allowElseIf: false }],
errors: [{ messageId: "unexpected", type: "IfStatement" }]
},
{
code: "function foo() { if (true) { return bar; } else { const baz = 1; return baz; } }",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo() { if (true) { return bar; } else { let baz = 1; return baz; } }",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo() { let bar = true; if (baz) { return; } else { let bar = false; return; } }",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "BlockStatement" }]
},
{
code: "function foo() { let bar = true; if (baz) { return; } else { { let bar = false; } return; } }",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [
{ messageId: "unexpected", type: "BlockStatement" }
]
}
]
});