]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/consistent-return.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / consistent-return.js
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'" },
55 type: "ReturnStatement"
56 }
57 ]
58 },
59 {
60 code: "var foo = () => { if (true) return true; else return; }",
61 parserOptions: { ecmaVersion: 6 },
62 errors: [
63 {
64 messageId: "missingReturnValue",
65 data: { name: "Arrow function" },
66 type: "ReturnStatement"
67 }
68 ]
69 },
70 {
71 code: "function foo() { if (true) return; else return false; }",
72 errors: [
73 {
74 messageId: "unexpectedReturnValue",
75 data: { name: "Function 'foo'" },
76 type: "ReturnStatement"
77 }
78 ]
79 },
80 {
81 code: "f(function() { if (true) return true; else return; })",
82 errors: [
83 {
84 messageId: "missingReturnValue",
85 data: { name: "Function" },
86 type: "ReturnStatement"
87 }
88 ]
89 },
90 {
91 code: "f(function() { if (true) return; else return false; })",
92 errors: [
93 {
94 messageId: "unexpectedReturnValue",
95 data: { name: "Function" },
96 type: "ReturnStatement"
97 }
98 ]
99 },
100 {
101 code: "f(a => { if (true) return; else return false; })",
102 parserOptions: { ecmaVersion: 6 },
103 errors: [
104 {
105 messageId: "unexpectedReturnValue",
106 data: { name: "Arrow function" },
107 type: "ReturnStatement"
108 }
109 ]
110 },
111 {
112 code: "function foo() { if (true) return true; return undefined; }",
113 options: [{ treatUndefinedAsUnspecified: true }],
114 errors: [
115 {
116 messageId: "missingReturnValue",
117 data: { name: "Function 'foo'" },
118 type: "ReturnStatement",
119 column: 41
120 }
121 ]
122 },
123 {
124 code: "function foo() { if (true) return true; return void 0; }",
125 options: [{ treatUndefinedAsUnspecified: true }],
126 errors: [
127 {
128 messageId: "missingReturnValue",
129 data: { name: "Function 'foo'" },
130 type: "ReturnStatement",
131 column: 41
132 }
133 ]
134 },
135 {
136 code: "function foo() { if (true) return undefined; return true; }",
137 options: [{ treatUndefinedAsUnspecified: true }],
138 errors: [
139 {
140 messageId: "unexpectedReturnValue",
141 data: { name: "Function 'foo'" },
142 type: "ReturnStatement",
143 column: 46
144 }
145 ]
146 },
147 {
148 code: "function foo() { if (true) return void 0; return true; }",
149 options: [{ treatUndefinedAsUnspecified: true }],
150 errors: [
151 {
152 messageId: "unexpectedReturnValue",
153 data: { name: "Function 'foo'" },
154 type: "ReturnStatement",
155 column: 43
156 }
157 ]
158 },
159 {
160 code: "if (true) { return 1; } return;",
161 parserOptions: { ecmaFeatures: { globalReturn: true } },
162 errors: [
163 {
164 messageId: "missingReturnValue",
165 data: { name: "Program" },
166 type: "ReturnStatement"
167 }
168 ]
169 },
170 {
171 code: "function foo() { if (a) return true; }",
172 errors: [
173 {
174 messageId: "missingReturn",
175 data: { name: "function 'foo'" },
176 type: "FunctionDeclaration",
177 column: 10
178 }
179 ]
180 },
181 {
182 code: "function _foo() { if (a) return true; }",
183 errors: [
184 {
185 messageId: "missingReturn",
186 data: { name: "function '_foo'" },
187 type: "FunctionDeclaration",
188 column: 10
189 }
190 ]
191 },
192 {
193 code: "f(function foo() { if (a) return true; });",
194 errors: [
195 {
196 messageId: "missingReturn",
197 data: { name: "function 'foo'" },
198 type: "FunctionExpression",
199 column: 12
200 }
201 ]
202 },
203 {
204 code: "f(function() { if (a) return true; });",
205 errors: [
206 {
207 messageId: "missingReturn",
208 data: { name: "function" },
209 type: "FunctionExpression",
210 column: 3
211 }
212 ]
213 },
214 {
215 code: "f(() => { if (a) return true; });",
216 parserOptions: { ecmaVersion: 6 },
217 errors: [
218 {
219 messageId: "missingReturn",
220 data: { name: "arrow function" },
221 type: "ArrowFunctionExpression",
222 column: 6
223 }
224 ]
225 },
226 {
227 code: "var obj = {foo() { if (a) return true; }};",
228 parserOptions: { ecmaVersion: 6 },
229 errors: [
230 {
231 messageId: "missingReturn",
232 data: { name: "method 'foo'" },
233 type: "FunctionExpression",
234 column: 12
235 }
236 ]
237 },
238 {
239 code: "class A {foo() { if (a) return true; }};",
240 parserOptions: { ecmaVersion: 6 },
241 errors: [
242 {
243 messageId: "missingReturn",
244 data: { name: "method 'foo'" },
245 type: "FunctionExpression",
246 column: 10
247 }
248 ]
249 },
250 {
251 code: "if (a) return true;",
252 parserOptions: { ecmaFeatures: { globalReturn: true } },
253 errors: [
254 {
255 messageId: "missingReturn",
256 data: { name: "program" },
257 type: "Program",
258 column: 1
259 }
260 ]
261 },
262 {
263 code: "class A { CapitalizedFunction() { if (a) return true; } }",
264 parserOptions: { ecmaVersion: 6 },
265 errors: [
266 {
267 messageId: "missingReturn",
268 data: { name: "method 'CapitalizedFunction'" },
269 type: "FunctionExpression",
270 column: 11
271 }
272 ]
273 },
274 {
275 code: "({ constructor() { if (a) return true; } });",
276 parserOptions: { ecmaVersion: 6 },
277 errors: [
278 {
279 messageId: "missingReturn",
280 data: { name: "method 'constructor'" },
281 type: "FunctionExpression",
282 column: 4
283 }
284 ]
285 }
286 ]
287 });