]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/array-callback-return.js
71731de587987f8665e13c0258a9a5648ead3edb
[pve-eslint.git] / eslint / tests / lib / rules / array-callback-return.js
1 /**
2 * @fileoverview Tests for array-callback-return rule.
3 * @author Toru Nagashima
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/array-callback-return"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 const ruleTester = new RuleTester();
16
17 const allowImplicitOptions = [{ allowImplicit: true }];
18
19 const checkForEachOptions = [{ checkForEach: true }];
20
21 const allowImplicitCheckForEach = [{ allowImplicit: true, checkForEach: true }];
22
23 ruleTester.run("array-callback-return", rule, {
24 valid: [
25
26 "foo.every(function(){}())",
27 "foo.every(function(){ return function() { return true; }; }())",
28 "foo.every(function(){ return function() { return; }; })",
29
30 "foo.forEach(bar || function(x) { var a=0; })",
31 "foo.forEach(bar || function(x) { return a; })",
32 "foo.forEach(function() {return function() { var a = 0;}}())",
33 "foo.forEach(function(x) { var a=0; })",
34 "foo.forEach(function(x) { return a;})",
35 "foo.forEach(function(x) { return; })",
36 "foo.forEach(function(x) { if (a === b) { return;} var a=0; })",
37 "foo.forEach(function(x) { if (a === b) { return x;} var a=0; })",
38 "foo.bar().forEach(function(x) { return; })",
39 "[\"foo\",\"bar\",\"baz\"].forEach(function(x) { return x; })",
40 { code: "foo.forEach(x => { var a=0; })", parserOptions: { ecmaVersion: 6 } },
41 { code: "foo.forEach(x => { if (a === b) { return;} var a=0; })", parserOptions: { ecmaVersion: 6 } },
42 { code: "foo.forEach(x => x)", parserOptions: { ecmaVersion: 6 } },
43 { code: "foo.forEach(val => y += val)", parserOptions: { ecmaVersion: 6 } },
44
45 { code: "foo.map(async function(){})", parserOptions: { ecmaVersion: 8 } },
46 { code: "foo.map(async () => {})", parserOptions: { ecmaVersion: 8 } },
47 { code: "foo.map(function* () {})", parserOptions: { ecmaVersion: 6 } },
48
49 // options: { allowImplicit: false }
50 { code: "Array.from(x, function() { return true; })", options: [{ allowImplicit: false }] },
51 { code: "Int32Array.from(x, function() { return true; })", options: [{ allowImplicit: false }] },
52 "foo.every(function() { return true; })",
53 "foo.filter(function() { return true; })",
54 "foo.find(function() { return true; })",
55 "foo.findIndex(function() { return true; })",
56 "foo.flatMap(function() { return true; })",
57 "foo.forEach(function() { return; })",
58 "foo.map(function() { return true; })",
59 "foo.reduce(function() { return true; })",
60 "foo.reduceRight(function() { return true; })",
61 "foo.some(function() { return true; })",
62 "foo.sort(function() { return 0; })",
63 { code: "foo.every(() => { return true; })", parserOptions: { ecmaVersion: 6 } },
64 "foo.every(function() { if (a) return true; else return false; })",
65 "foo.every(function() { switch (a) { case 0: bar(); default: return true; } })",
66 "foo.every(function() { try { bar(); return true; } catch (err) { return false; } })",
67 "foo.every(function() { try { bar(); } finally { return true; } })",
68
69 // options: { allowImplicit: true }
70 { code: "Array.from(x, function() { return; })", options: allowImplicitOptions },
71 { code: "Int32Array.from(x, function() { return; })", options: allowImplicitOptions },
72 { code: "foo.every(function() { return; })", options: allowImplicitOptions },
73 { code: "foo.filter(function() { return; })", options: allowImplicitOptions },
74 { code: "foo.find(function() { return; })", options: allowImplicitOptions },
75 { code: "foo.findIndex(function() { return; })", options: allowImplicitOptions },
76 { code: "foo.flatMap(function() { return; })", options: allowImplicitOptions },
77 { code: "foo.forEach(function() { return; })", options: allowImplicitOptions },
78 { code: "foo.map(function() { return; })", options: allowImplicitOptions },
79 { code: "foo.reduce(function() { return; })", options: allowImplicitOptions },
80 { code: "foo.reduceRight(function() { return; })", options: allowImplicitOptions },
81 { code: "foo.some(function() { return; })", options: allowImplicitOptions },
82 { code: "foo.sort(function() { return; })", options: allowImplicitOptions },
83 { code: "foo.every(() => { return; })", options: allowImplicitOptions, parserOptions: { ecmaVersion: 6 } },
84 { code: "foo.every(function() { if (a) return; else return a; })", options: allowImplicitOptions },
85 { code: "foo.every(function() { switch (a) { case 0: bar(); default: return; } })", options: allowImplicitOptions },
86 { code: "foo.every(function() { try { bar(); return; } catch (err) { return; } })", options: allowImplicitOptions },
87 { code: "foo.every(function() { try { bar(); } finally { return; } })", options: allowImplicitOptions },
88
89 // options: { checkForEach: true }
90 { code: "foo.forEach(function(x) { return; })", options: checkForEachOptions },
91 { code: "foo.forEach(function(x) { var a=0; })", options: checkForEachOptions },
92 { code: "foo.forEach(function(x) { if (a === b) { return;} var a=0; })", options: checkForEachOptions },
93 { code: "foo.forEach(function() {return function() { if (a == b) { return; }}}())", options: checkForEachOptions },
94 { code: "foo.forEach(x => { var a=0; })", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 } },
95 { code: "foo.forEach(x => { if (a === b) { return;} var a=0; })", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 } },
96 { code: "foo.forEach(x => { x })", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 } },
97 { code: "foo.forEach(bar || function(x) { return; })", options: checkForEachOptions },
98 { code: "Array.from(x, function() { return true; })", options: checkForEachOptions },
99 { code: "Int32Array.from(x, function() { return true; })", options: checkForEachOptions },
100 { code: "foo.every(() => { return true; })", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 } },
101 { code: "foo.every(function() { if (a) return 1; else return a; })", options: checkForEachOptions },
102 { code: "foo.every(function() { switch (a) { case 0: return bar(); default: return a; } })", options: checkForEachOptions },
103 { code: "foo.every(function() { try { bar(); return 1; } catch (err) { return err; } })", options: checkForEachOptions },
104 { code: "foo.every(function() { try { bar(); } finally { return 1; } })", options: checkForEachOptions },
105 { code: "foo.every(function() { return; })", options: allowImplicitCheckForEach },
106
107 "Arrow.from(x, function() {})",
108 "foo.abc(function() {})",
109 "every(function() {})",
110 "foo[every](function() {})",
111 "var every = function() {}",
112 { code: "foo[`${every}`](function() {})", parserOptions: { ecmaVersion: 6 } },
113 { code: "foo.every(() => true)", parserOptions: { ecmaVersion: 6 } }
114
115 ],
116 invalid: [
117
118 { code: "Array.from(x, function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
119 { code: "Array.from(x, function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
120 { code: "Int32Array.from(x, function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
121 { code: "Int32Array.from(x, function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
122 { code: "foo.every(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
123 { code: "foo.every(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
124 { code: "foo.filter(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
125 { code: "foo.filter(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
126 { code: "foo.find(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
127 { code: "foo.find(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
128 { code: "foo.findIndex(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
129 { code: "foo.findIndex(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
130 { code: "foo.flatMap(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
131 { code: "foo.flatMap(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
132 { code: "foo.map(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
133 { code: "foo.map(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
134 { code: "foo.reduce(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
135 { code: "foo.reduce(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
136 { code: "foo.reduceRight(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
137 { code: "foo.reduceRight(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
138 { code: "foo.some(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
139 { code: "foo.some(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
140 { code: "foo.sort(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
141 { code: "foo.sort(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
142 { code: "foo.bar.baz.every(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
143 { code: "foo.bar.baz.every(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
144 { code: "foo[\"every\"](function() {})", errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
145 { code: "foo[\"every\"](function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
146 { code: "foo[`every`](function() {})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
147 { code: "foo[`every`](function foo() {})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
148 { code: "foo.every(() => {})", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected to return a value in arrow function.", column: 14 }] },
149 { code: "foo.every(function() { if (a) return true; })", errors: [{ message: "Expected to return a value at the end of function.", column: 11 }] },
150 { code: "foo.every(function cb() { if (a) return true; })", errors: [{ message: "Expected to return a value at the end of function 'cb'.", column: 20 }] },
151 { code: "foo.every(function() { switch (a) { case 0: break; default: return true; } })", errors: [{ messageId: "expectedAtEnd", data: { name: "function" } }] },
152 { code: "foo.every(function foo() { switch (a) { case 0: break; default: return true; } })", errors: [{ messageId: "expectedAtEnd", data: { name: "function 'foo'" } }] },
153 { code: "foo.every(function() { try { bar(); } catch (err) { return true; } })", errors: [{ messageId: "expectedAtEnd", data: { name: "function" } }] },
154 { code: "foo.every(function foo() { try { bar(); } catch (err) { return true; } })", errors: [{ messageId: "expectedAtEnd", data: { name: "function 'foo'" } }] },
155 { code: "foo.every(function() { return; })", errors: [{ messageId: "expectedReturnValue", data: { name: "Function" } }] },
156 { code: "foo.every(function foo() { return; })", errors: [{ messageId: "expectedReturnValue", data: { name: "Function 'foo'" } }] },
157 { code: "foo.every(function() { if (a) return; })", errors: ["Expected to return a value at the end of function.", { messageId: "expectedReturnValue", data: { name: "Function" } }] },
158 { code: "foo.every(function foo() { if (a) return; })", errors: ["Expected to return a value at the end of function 'foo'.", { messageId: "expectedReturnValue", data: { name: "Function 'foo'" } }] },
159 { code: "foo.every(function() { if (a) return; else return; })", errors: [{ messageId: "expectedReturnValue", data: { name: "Function" } }, { messageId: "expectedReturnValue", data: { name: "Function" } }] },
160 { code: "foo.every(function foo() { if (a) return; else return; })", errors: [{ messageId: "expectedReturnValue", data: { name: "Function 'foo'" } }, { messageId: "expectedReturnValue", data: { name: "Function 'foo'" } }] },
161 { code: "foo.every(cb || function() {})", errors: ["Expected to return a value in function."] },
162 { code: "foo.every(cb || function foo() {})", errors: ["Expected to return a value in function 'foo'."] },
163 { code: "foo.every(a ? function() {} : function() {})", errors: ["Expected to return a value in function.", "Expected to return a value in function."] },
164 { code: "foo.every(a ? function foo() {} : function bar() {})", errors: ["Expected to return a value in function 'foo'.", "Expected to return a value in function 'bar'."] },
165 { code: "foo.every(function(){ return function() {}; }())", errors: [{ message: "Expected to return a value in function.", column: 30 }] },
166 { code: "foo.every(function(){ return function foo() {}; }())", errors: [{ message: "Expected to return a value in function 'foo'.", column: 39 }] },
167 { code: "foo.every(() => {})", options: [{ allowImplicit: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected to return a value in arrow function." }] },
168 { code: "foo.every(() => {})", options: [{ allowImplicit: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected to return a value in arrow function." }] },
169
170 // options: { allowImplicit: true }
171 { code: "Array.from(x, function() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
172 { code: "foo.every(function() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
173 { code: "foo.filter(function foo() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
174 { code: "foo.find(function foo() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
175 { code: "foo.map(function() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
176 { code: "foo.reduce(function() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
177 { code: "foo.reduceRight(function() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
178 { code: "foo.bar.baz.every(function foo() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
179 { code: "foo.every(cb || function() {})", options: allowImplicitOptions, errors: ["Expected to return a value in function."] },
180 { code: "[\"foo\",\"bar\"].sort(function foo() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
181 { code: "foo.forEach(x => x)", options: allowImplicitCheckForEach, parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Arrow function" } }] },
182 { code: "foo.forEach(function(x) { if (a == b) {return x;}})", options: allowImplicitCheckForEach, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Function" } }] },
183 { code: "foo.forEach(function bar(x) { return x;})", options: allowImplicitCheckForEach, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Function 'bar'" } }] },
184
185 // // options: { checkForEach: true }
186 { code: "foo.forEach(x => x)", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Arrow function" } }] },
187 { code: "foo.forEach(val => y += val)", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Arrow function" } }] },
188 { code: "[\"foo\",\"bar\"].forEach(x => ++x)", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Arrow function" } }] },
189 { code: "foo.bar().forEach(x => x === y)", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Arrow function" } }] },
190 { code: "foo.forEach(function() {return function() { if (a == b) { return a; }}}())", options: checkForEachOptions, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Function" } }] },
191 { code: "foo.forEach(function(x) { if (a == b) {return x;}})", options: checkForEachOptions, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Function" } }] },
192 { code: "foo.forEach(function(x) { if (a == b) {return undefined;}})", options: checkForEachOptions, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Function" } }] },
193 { code: "foo.forEach(function bar(x) { return x;})", options: checkForEachOptions, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Function 'bar'" } }] },
194 { code: "foo.bar().forEach(function bar(x) { return x;})", options: checkForEachOptions, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Function 'bar'" } }] },
195 { code: "[\"foo\",\"bar\"].forEach(function bar(x) { return x;})", options: checkForEachOptions, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Function 'bar'" } }] },
196 { code: "foo.forEach((x) => { return x;})", options: checkForEachOptions, parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedNoReturnValue", data: { name: "Arrow function" } }] },
197 { code: "Array.from(x, function() {})", options: checkForEachOptions, errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
198 { code: "foo.every(function() {})", options: checkForEachOptions, errors: [{ messageId: "expectedInside", data: { name: "function" } }] },
199 { code: "foo.filter(function foo() {})", options: checkForEachOptions, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'" } }] },
200 { code: "foo.filter(function foo() { return; })", options: checkForEachOptions, errors: [{ messageId: "expectedReturnValue", data: { name: "Function 'foo'" } }] },
201 { code: "foo.every(cb || function() {})", options: checkForEachOptions, errors: ["Expected to return a value in function."] }
202
203 ]
204 });