]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Disallow redundant return statements | |
3 | * @author Teddy Katz | |
4 | */ | |
5 | "use strict"; | |
6 | ||
7 | //------------------------------------------------------------------------------ | |
8 | // Requirements | |
9 | //------------------------------------------------------------------------------ | |
10 | ||
11 | const rule = require("../../../lib/rules/no-useless-return"), | |
12 | { RuleTester } = require("../../../lib/rule-tester"); | |
13 | ||
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | ||
21 | ruleTester.run("no-useless-return", rule, { | |
22 | ||
23 | valid: [ | |
24 | "function foo() { return 5; }", | |
25 | "function foo() { return null; }", | |
26 | "function foo() { return doSomething(); }", | |
27 | ` | |
28 | function foo() { | |
29 | if (bar) { | |
30 | doSomething(); | |
31 | return; | |
32 | } else { | |
33 | doSomethingElse(); | |
34 | } | |
35 | qux(); | |
36 | } | |
37 | `, | |
38 | ` | |
39 | function foo() { | |
40 | switch (bar) { | |
41 | case 1: | |
42 | doSomething(); | |
43 | return; | |
44 | default: | |
45 | doSomethingElse(); | |
46 | } | |
47 | } | |
48 | `, | |
49 | ` | |
50 | function foo() { | |
51 | switch (bar) { | |
52 | default: | |
53 | doSomething(); | |
54 | return; | |
55 | case 1: | |
56 | doSomethingElse(); | |
57 | } | |
58 | } | |
59 | `, | |
60 | ` | |
61 | function foo() { | |
62 | switch (bar) { | |
63 | case 1: | |
64 | if (a) { | |
65 | doSomething(); | |
66 | return; | |
67 | } else { | |
68 | doSomething(); | |
69 | return; | |
70 | } | |
71 | default: | |
72 | doSomethingElse(); | |
73 | } | |
74 | } | |
75 | `, | |
76 | ` | |
77 | function foo() { | |
78 | for (var foo = 0; foo < 10; foo++) { | |
79 | return; | |
80 | } | |
81 | } | |
82 | `, | |
83 | ` | |
84 | function foo() { | |
85 | for (var foo in bar) { | |
86 | return; | |
87 | } | |
88 | } | |
89 | `, | |
90 | ` | |
91 | function foo() { | |
92 | try { | |
93 | return 5; | |
94 | } finally { | |
95 | return; // This is allowed because it can override the returned value of 5 | |
96 | } | |
97 | } | |
98 | `, | |
99 | ` | |
100 | function foo() { | |
101 | return; | |
102 | doSomething(); | |
103 | } | |
104 | `, | |
105 | { | |
106 | code: ` | |
107 | function foo() { | |
108 | for (var foo of bar) return; | |
109 | } | |
110 | `, | |
111 | parserOptions: { ecmaVersion: 6 } | |
112 | }, | |
113 | { | |
114 | code: "() => { if (foo) return; bar(); }", | |
115 | parserOptions: { ecmaVersion: 6 } | |
116 | }, | |
117 | { | |
118 | code: "() => 5", | |
119 | parserOptions: { ecmaVersion: 6 } | |
120 | }, | |
121 | { | |
122 | code: "() => { return; doSomething(); }", | |
123 | parserOptions: { ecmaVersion: 6 } | |
124 | }, | |
125 | { | |
126 | code: "if (foo) { return; } doSomething();", | |
127 | parserOptions: { ecmaFeatures: { globalReturn: true } } | |
128 | }, | |
129 | ||
130 | // https://github.com/eslint/eslint/issues/7477 | |
131 | ` | |
132 | function foo() { | |
133 | if (bar) return; | |
134 | return baz; | |
135 | } | |
136 | `, | |
137 | ` | |
138 | function foo() { | |
139 | if (bar) { | |
140 | return; | |
141 | } | |
142 | return baz; | |
143 | } | |
144 | `, | |
145 | ` | |
146 | function foo() { | |
147 | if (bar) baz(); | |
148 | else return; | |
149 | return 5; | |
150 | } | |
151 | `, | |
152 | ||
153 | // https://github.com/eslint/eslint/issues/7583 | |
154 | ` | |
155 | function foo() { | |
156 | return; | |
157 | while (foo) return; | |
158 | foo; | |
159 | } | |
160 | `, | |
161 | ||
162 | // https://github.com/eslint/eslint/issues/7855 | |
163 | ` | |
164 | try { | |
165 | throw new Error('foo'); | |
166 | while (false); | |
167 | } catch (err) {} | |
168 | `, | |
169 | ||
170 | // https://github.com/eslint/eslint/issues/11647 | |
171 | ` | |
172 | function foo(arg) { | |
173 | throw new Error("Debugging..."); | |
174 | if (!arg) { | |
175 | return; | |
176 | } | |
177 | console.log(arg); | |
178 | } | |
179 | ` | |
180 | ], | |
181 | ||
182 | invalid: [ | |
183 | { | |
184 | code: "function foo() { return; }", | |
185 | output: "function foo() { }" | |
186 | }, | |
187 | { | |
188 | code: "function foo() { doSomething(); return; }", | |
189 | output: "function foo() { doSomething(); }" | |
190 | }, | |
191 | { | |
192 | code: "function foo() { if (condition) { bar(); return; } else { baz(); } }", | |
193 | output: "function foo() { if (condition) { bar(); } else { baz(); } }" | |
194 | }, | |
195 | { | |
196 | code: "function foo() { if (foo) return; }", | |
197 | output: "function foo() { if (foo) return; }" | |
198 | }, | |
199 | { | |
200 | code: "function foo() { bar(); return/**/; }", | |
201 | output: null | |
202 | }, | |
203 | { | |
204 | code: "function foo() { bar(); return//\n; }", | |
205 | output: null | |
206 | }, | |
207 | { | |
208 | code: "foo(); return;", | |
209 | output: "foo(); ", | |
210 | parserOptions: { ecmaFeatures: { globalReturn: true } } | |
211 | }, | |
212 | { | |
213 | code: "if (foo) { bar(); return; } else { baz(); }", | |
214 | output: "if (foo) { bar(); } else { baz(); }", | |
215 | parserOptions: { ecmaFeatures: { globalReturn: true } } | |
216 | }, | |
217 | { | |
218 | code: ` | |
219 | function foo() { | |
220 | if (foo) { | |
221 | return; | |
222 | } | |
223 | return; | |
224 | } | |
225 | `, | |
226 | output: ` | |
227 | function foo() { | |
228 | if (foo) { | |
229 | ||
230 | } | |
231 | return; | |
232 | } | |
233 | `, // Other case is fixed in the second pass. | |
234 | errors: [ | |
235 | { messageId: "unnecessaryReturn", type: "ReturnStatement" }, | |
236 | { messageId: "unnecessaryReturn", type: "ReturnStatement" } | |
237 | ] | |
238 | }, | |
239 | { | |
240 | code: ` | |
241 | function foo() { | |
242 | switch (bar) { | |
243 | case 1: | |
244 | doSomething(); | |
245 | default: | |
246 | doSomethingElse(); | |
247 | return; | |
248 | } | |
249 | } | |
250 | `, | |
251 | output: ` | |
252 | function foo() { | |
253 | switch (bar) { | |
254 | case 1: | |
255 | doSomething(); | |
256 | default: | |
257 | doSomethingElse(); | |
258 | ||
259 | } | |
260 | } | |
261 | ` | |
262 | }, | |
263 | { | |
264 | code: ` | |
265 | function foo() { | |
266 | switch (bar) { | |
267 | default: | |
268 | doSomething(); | |
269 | case 1: | |
270 | doSomething(); | |
271 | return; | |
272 | } | |
273 | } | |
274 | `, | |
275 | output: ` | |
276 | function foo() { | |
277 | switch (bar) { | |
278 | default: | |
279 | doSomething(); | |
280 | case 1: | |
281 | doSomething(); | |
282 | ||
283 | } | |
284 | } | |
285 | ` | |
286 | }, | |
287 | { | |
288 | code: ` | |
289 | function foo() { | |
290 | switch (bar) { | |
291 | case 1: | |
292 | if (a) { | |
293 | doSomething(); | |
294 | return; | |
295 | } | |
296 | break; | |
297 | default: | |
298 | doSomethingElse(); | |
299 | } | |
300 | } | |
301 | `, | |
302 | output: ` | |
303 | function foo() { | |
304 | switch (bar) { | |
305 | case 1: | |
306 | if (a) { | |
307 | doSomething(); | |
308 | ||
309 | } | |
310 | break; | |
311 | default: | |
312 | doSomethingElse(); | |
313 | } | |
314 | } | |
315 | ` | |
316 | }, | |
317 | { | |
318 | code: ` | |
319 | function foo() { | |
320 | switch (bar) { | |
321 | case 1: | |
322 | if (a) { | |
323 | doSomething(); | |
324 | return; | |
325 | } else { | |
326 | doSomething(); | |
327 | } | |
328 | break; | |
329 | default: | |
330 | doSomethingElse(); | |
331 | } | |
332 | } | |
333 | `, | |
334 | output: ` | |
335 | function foo() { | |
336 | switch (bar) { | |
337 | case 1: | |
338 | if (a) { | |
339 | doSomething(); | |
340 | ||
341 | } else { | |
342 | doSomething(); | |
343 | } | |
344 | break; | |
345 | default: | |
346 | doSomethingElse(); | |
347 | } | |
348 | } | |
349 | ` | |
350 | }, | |
351 | { | |
352 | code: ` | |
353 | function foo() { | |
354 | switch (bar) { | |
355 | case 1: | |
356 | if (a) { | |
357 | doSomething(); | |
358 | return; | |
359 | } | |
360 | default: | |
361 | } | |
362 | } | |
363 | `, | |
364 | output: ` | |
365 | function foo() { | |
366 | switch (bar) { | |
367 | case 1: | |
368 | if (a) { | |
369 | doSomething(); | |
370 | ||
371 | } | |
372 | default: | |
373 | } | |
374 | } | |
375 | ` | |
376 | }, | |
377 | { | |
378 | code: ` | |
379 | function foo() { | |
380 | try {} catch (err) { return; } | |
381 | } | |
382 | `, | |
383 | output: ` | |
384 | function foo() { | |
385 | try {} catch (err) { } | |
386 | } | |
387 | ` | |
388 | }, | |
389 | ||
390 | /* | |
391 | * FIXME: Re-add this case (removed due to https://github.com/eslint/eslint/issues/7481): | |
392 | * https://github.com/eslint/eslint/blob/261d7287820253408ec87c344beccdba2fe829a4/tests/lib/rules/no-useless-return.js#L308-L329 | |
393 | */ | |
394 | ||
395 | { | |
396 | code: ` | |
397 | function foo() { | |
398 | try {} finally {} | |
399 | return; | |
400 | } | |
401 | `, | |
402 | output: ` | |
403 | function foo() { | |
404 | try {} finally {} | |
405 | ||
406 | } | |
407 | ` | |
408 | }, | |
409 | { | |
410 | code: ` | |
411 | function foo() { | |
412 | try { | |
413 | return 5; | |
414 | } finally { | |
415 | function bar() { | |
416 | return; | |
417 | } | |
418 | } | |
419 | } | |
420 | `, | |
421 | output: ` | |
422 | function foo() { | |
423 | try { | |
424 | return 5; | |
425 | } finally { | |
426 | function bar() { | |
427 | ||
428 | } | |
429 | } | |
430 | } | |
431 | ` | |
432 | }, | |
433 | { | |
434 | code: "() => { return; }", | |
435 | output: "() => { }", | |
436 | parserOptions: { ecmaVersion: 6 } | |
437 | }, | |
438 | { | |
439 | code: "function foo() { return; return; }", | |
440 | output: "function foo() { return; }", | |
441 | errors: [{ | |
442 | messageId: "unnecessaryReturn", | |
443 | type: "ReturnStatement", | |
444 | column: 18 | |
445 | }] | |
446 | } | |
447 | ].map(invalidCase => Object.assign({ errors: [{ messageId: "unnecessaryReturn", type: "ReturnStatement" }] }, invalidCase)) | |
448 | }); |