]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for consistent-return rule. | |
3 | * @author Raphael Pigulla | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | const rule = require("../../../lib/rules/consistent-return"), | |
12 | { RuleTester } = require("../../../lib/rule-tester"); | |
13 | ||
14 | //------------------------------------------------------------------------------ | |
15 | // Tests | |
16 | //------------------------------------------------------------------------------ | |
17 | ||
18 | const ruleTester = new RuleTester(); | |
19 | ||
20 | ruleTester.run("consistent-return", rule, { | |
21 | ||
22 | valid: [ | |
23 | "function foo() { return; }", | |
24 | "function foo() { if (true) return; }", | |
25 | "function foo() { if (true) return; else return; }", | |
26 | "function foo() { if (true) return true; else return false; }", | |
27 | "f(function() { return; })", | |
28 | "f(function() { if (true) return; })", | |
29 | "f(function() { if (true) return; else return; })", | |
30 | "f(function() { if (true) return true; else return false; })", | |
31 | "function foo() { function bar() { return true; } return; }", | |
32 | "function foo() { function bar() { return; } return false; }", | |
33 | "function Foo() { if (!(this instanceof Foo)) return new Foo(); }", | |
34 | { code: "function foo() { if (true) return; else return undefined; }", options: [{ treatUndefinedAsUnspecified: true }] }, | |
35 | { code: "function foo() { if (true) return; else return void 0; }", options: [{ treatUndefinedAsUnspecified: true }] }, | |
36 | { code: "function foo() { if (true) return undefined; else return; }", options: [{ treatUndefinedAsUnspecified: true }] }, | |
37 | { code: "function foo() { if (true) return undefined; else return void 0; }", options: [{ treatUndefinedAsUnspecified: true }] }, | |
38 | { code: "function foo() { if (true) return void 0; else return; }", options: [{ treatUndefinedAsUnspecified: true }] }, | |
39 | { code: "function foo() { if (true) return void 0; else return undefined; }", options: [{ treatUndefinedAsUnspecified: true }] }, | |
40 | { code: "var x = () => { return {}; };", parserOptions: { ecmaVersion: 6 } }, | |
41 | { code: "if (true) { return 1; } return 0;", parserOptions: { ecmaVersion: 6, ecmaFeatures: { globalReturn: true } } }, | |
42 | ||
43 | // https://github.com/eslint/eslint/issues/7790 | |
44 | { code: "class Foo { constructor() { if (true) return foo; } }", parserOptions: { ecmaVersion: 6 } }, | |
45 | { code: "var Foo = class { constructor() { if (true) return foo; } }", parserOptions: { ecmaVersion: 6 } } | |
46 | ], | |
47 | ||
48 | invalid: [ | |
49 | { | |
50 | code: "function foo() { if (true) return true; else return; }", | |
51 | errors: [ | |
52 | { | |
53 | messageId: "missingReturnValue", | |
54 | data: { name: "Function 'foo'" }, | |
609c276f TL |
55 | type: "ReturnStatement", |
56 | line: 1, | |
57 | column: 46, | |
58 | endLine: 1, | |
59 | endColumn: 53 | |
eb39fafa DC |
60 | } |
61 | ] | |
62 | }, | |
63 | { | |
64 | code: "var foo = () => { if (true) return true; else return; }", | |
65 | parserOptions: { ecmaVersion: 6 }, | |
66 | errors: [ | |
67 | { | |
68 | messageId: "missingReturnValue", | |
69 | data: { name: "Arrow function" }, | |
609c276f TL |
70 | type: "ReturnStatement", |
71 | line: 1, | |
72 | column: 47, | |
73 | endLine: 1, | |
74 | endColumn: 54 | |
eb39fafa DC |
75 | } |
76 | ] | |
77 | }, | |
78 | { | |
79 | code: "function foo() { if (true) return; else return false; }", | |
80 | errors: [ | |
81 | { | |
82 | messageId: "unexpectedReturnValue", | |
83 | data: { name: "Function 'foo'" }, | |
609c276f TL |
84 | type: "ReturnStatement", |
85 | line: 1, | |
86 | column: 41, | |
87 | endLine: 1, | |
88 | endColumn: 54 | |
eb39fafa DC |
89 | } |
90 | ] | |
91 | }, | |
92 | { | |
93 | code: "f(function() { if (true) return true; else return; })", | |
94 | errors: [ | |
95 | { | |
96 | messageId: "missingReturnValue", | |
97 | data: { name: "Function" }, | |
609c276f TL |
98 | type: "ReturnStatement", |
99 | line: 1, | |
100 | column: 44, | |
101 | endLine: 1, | |
102 | endColumn: 51 | |
eb39fafa DC |
103 | } |
104 | ] | |
105 | }, | |
106 | { | |
107 | code: "f(function() { if (true) return; else return false; })", | |
108 | errors: [ | |
109 | { | |
110 | messageId: "unexpectedReturnValue", | |
111 | data: { name: "Function" }, | |
609c276f TL |
112 | type: "ReturnStatement", |
113 | line: 1, | |
114 | column: 39, | |
115 | endLine: 1, | |
116 | endColumn: 52 | |
eb39fafa DC |
117 | } |
118 | ] | |
119 | }, | |
120 | { | |
121 | code: "f(a => { if (true) return; else return false; })", | |
122 | parserOptions: { ecmaVersion: 6 }, | |
123 | errors: [ | |
124 | { | |
125 | messageId: "unexpectedReturnValue", | |
126 | data: { name: "Arrow function" }, | |
609c276f TL |
127 | type: "ReturnStatement", |
128 | line: 1, | |
129 | column: 33, | |
130 | endLine: 1, | |
131 | endColumn: 46 | |
eb39fafa DC |
132 | } |
133 | ] | |
134 | }, | |
135 | { | |
136 | code: "function foo() { if (true) return true; return undefined; }", | |
137 | options: [{ treatUndefinedAsUnspecified: true }], | |
138 | errors: [ | |
139 | { | |
140 | messageId: "missingReturnValue", | |
141 | data: { name: "Function 'foo'" }, | |
142 | type: "ReturnStatement", | |
609c276f TL |
143 | line: 1, |
144 | column: 41, | |
145 | endLine: 1, | |
146 | endColumn: 58 | |
eb39fafa DC |
147 | } |
148 | ] | |
149 | }, | |
150 | { | |
151 | code: "function foo() { if (true) return true; return void 0; }", | |
152 | options: [{ treatUndefinedAsUnspecified: true }], | |
153 | errors: [ | |
154 | { | |
155 | messageId: "missingReturnValue", | |
156 | data: { name: "Function 'foo'" }, | |
157 | type: "ReturnStatement", | |
609c276f TL |
158 | line: 1, |
159 | column: 41, | |
160 | endLine: 1, | |
161 | endColumn: 55 | |
eb39fafa DC |
162 | } |
163 | ] | |
164 | }, | |
165 | { | |
166 | code: "function foo() { if (true) return undefined; return true; }", | |
167 | options: [{ treatUndefinedAsUnspecified: true }], | |
168 | errors: [ | |
169 | { | |
170 | messageId: "unexpectedReturnValue", | |
171 | data: { name: "Function 'foo'" }, | |
172 | type: "ReturnStatement", | |
609c276f TL |
173 | line: 1, |
174 | column: 46, | |
175 | endLine: 1, | |
176 | endColumn: 58 | |
eb39fafa DC |
177 | } |
178 | ] | |
179 | }, | |
180 | { | |
181 | code: "function foo() { if (true) return void 0; return true; }", | |
182 | options: [{ treatUndefinedAsUnspecified: true }], | |
183 | errors: [ | |
184 | { | |
185 | messageId: "unexpectedReturnValue", | |
186 | data: { name: "Function 'foo'" }, | |
187 | type: "ReturnStatement", | |
609c276f TL |
188 | line: 1, |
189 | column: 43, | |
190 | endLine: 1, | |
191 | endColumn: 55 | |
eb39fafa DC |
192 | } |
193 | ] | |
194 | }, | |
195 | { | |
196 | code: "if (true) { return 1; } return;", | |
197 | parserOptions: { ecmaFeatures: { globalReturn: true } }, | |
198 | errors: [ | |
199 | { | |
200 | messageId: "missingReturnValue", | |
201 | data: { name: "Program" }, | |
609c276f TL |
202 | type: "ReturnStatement", |
203 | line: 1, | |
204 | column: 25, | |
205 | endLine: 1, | |
206 | endColumn: 32 | |
eb39fafa DC |
207 | } |
208 | ] | |
209 | }, | |
210 | { | |
211 | code: "function foo() { if (a) return true; }", | |
212 | errors: [ | |
213 | { | |
214 | messageId: "missingReturn", | |
215 | data: { name: "function 'foo'" }, | |
216 | type: "FunctionDeclaration", | |
609c276f TL |
217 | line: 1, |
218 | column: 10, | |
219 | endLine: 1, | |
220 | endColumn: 13 | |
eb39fafa DC |
221 | } |
222 | ] | |
223 | }, | |
224 | { | |
225 | code: "function _foo() { if (a) return true; }", | |
226 | errors: [ | |
227 | { | |
228 | messageId: "missingReturn", | |
229 | data: { name: "function '_foo'" }, | |
230 | type: "FunctionDeclaration", | |
609c276f TL |
231 | line: 1, |
232 | column: 10, | |
233 | endLine: 1, | |
234 | endColumn: 14 | |
eb39fafa DC |
235 | } |
236 | ] | |
237 | }, | |
238 | { | |
239 | code: "f(function foo() { if (a) return true; });", | |
240 | errors: [ | |
241 | { | |
242 | messageId: "missingReturn", | |
243 | data: { name: "function 'foo'" }, | |
244 | type: "FunctionExpression", | |
609c276f TL |
245 | line: 1, |
246 | column: 12, | |
247 | endLine: 1, | |
248 | endColumn: 15 | |
eb39fafa DC |
249 | } |
250 | ] | |
251 | }, | |
252 | { | |
253 | code: "f(function() { if (a) return true; });", | |
254 | errors: [ | |
255 | { | |
256 | messageId: "missingReturn", | |
257 | data: { name: "function" }, | |
258 | type: "FunctionExpression", | |
609c276f TL |
259 | line: 1, |
260 | column: 3, | |
261 | endLine: 1, | |
262 | endColumn: 11 | |
eb39fafa DC |
263 | } |
264 | ] | |
265 | }, | |
266 | { | |
267 | code: "f(() => { if (a) return true; });", | |
268 | parserOptions: { ecmaVersion: 6 }, | |
269 | errors: [ | |
270 | { | |
271 | messageId: "missingReturn", | |
272 | data: { name: "arrow function" }, | |
273 | type: "ArrowFunctionExpression", | |
609c276f TL |
274 | line: 1, |
275 | column: 6, | |
276 | endLine: 1, | |
277 | endColumn: 8 | |
eb39fafa DC |
278 | } |
279 | ] | |
280 | }, | |
281 | { | |
282 | code: "var obj = {foo() { if (a) return true; }};", | |
283 | parserOptions: { ecmaVersion: 6 }, | |
284 | errors: [ | |
285 | { | |
286 | messageId: "missingReturn", | |
287 | data: { name: "method 'foo'" }, | |
288 | type: "FunctionExpression", | |
609c276f TL |
289 | line: 1, |
290 | column: 12, | |
291 | endLine: 1, | |
292 | endColumn: 15 | |
eb39fafa DC |
293 | } |
294 | ] | |
295 | }, | |
296 | { | |
297 | code: "class A {foo() { if (a) return true; }};", | |
298 | parserOptions: { ecmaVersion: 6 }, | |
299 | errors: [ | |
300 | { | |
301 | messageId: "missingReturn", | |
302 | data: { name: "method 'foo'" }, | |
303 | type: "FunctionExpression", | |
609c276f TL |
304 | line: 1, |
305 | column: 10, | |
306 | endLine: 1, | |
307 | endColumn: 13 | |
eb39fafa DC |
308 | } |
309 | ] | |
310 | }, | |
311 | { | |
312 | code: "if (a) return true;", | |
313 | parserOptions: { ecmaFeatures: { globalReturn: true } }, | |
314 | errors: [ | |
315 | { | |
316 | messageId: "missingReturn", | |
317 | data: { name: "program" }, | |
318 | type: "Program", | |
609c276f TL |
319 | line: 1, |
320 | column: 1, | |
321 | endLine: void 0, | |
322 | endColumn: void 0 | |
eb39fafa DC |
323 | } |
324 | ] | |
325 | }, | |
326 | { | |
327 | code: "class A { CapitalizedFunction() { if (a) return true; } }", | |
328 | parserOptions: { ecmaVersion: 6 }, | |
329 | errors: [ | |
330 | { | |
331 | messageId: "missingReturn", | |
332 | data: { name: "method 'CapitalizedFunction'" }, | |
333 | type: "FunctionExpression", | |
609c276f TL |
334 | line: 1, |
335 | column: 11, | |
336 | endLine: 1, | |
337 | endColumn: 30 | |
eb39fafa DC |
338 | } |
339 | ] | |
340 | }, | |
341 | { | |
342 | code: "({ constructor() { if (a) return true; } });", | |
343 | parserOptions: { ecmaVersion: 6 }, | |
344 | errors: [ | |
345 | { | |
346 | messageId: "missingReturn", | |
347 | data: { name: "method 'constructor'" }, | |
348 | type: "FunctionExpression", | |
609c276f TL |
349 | line: 1, |
350 | column: 4, | |
351 | endLine: 1, | |
352 | endColumn: 15 | |
eb39fafa DC |
353 | } |
354 | ] | |
355 | } | |
356 | ] | |
357 | }); |