@@ -16,74 +16,105 @@ const ruleTester = new RuleTester();
16
16
17
17
const allowImplicitOptions = [ { allowImplicit : true } ] ;
18
18
19
+ const checkForEachOptions = [ { checkForEach : true } ] ;
20
+
21
+ const allowImplicitCheckForEach = [ { allowImplicit : true , checkForEach : true } ] ;
22
+
19
23
ruleTester . run ( "array-callback-return" , rule , {
20
24
valid : [
21
25
22
- // options: { allowImplicit: false }
23
- "Array.from(x, function() { return true; })" ,
24
- "Int32Array.from(x, function() { return true; })" ,
25
- { code : "Array.from(x, function() { return true; })" , options : [ { allowImplicit : false } ] } ,
26
- { code : "Int32Array.from(x, function() { return true; })" , options : [ { allowImplicit : false } ] } ,
26
+ "foo.every(function(){}())" ,
27
+ "foo.every(function(){ return function() { return true; }; }())" ,
28
+ "foo.every(function(){ return function() { return; }; })" ,
27
29
28
- // options: { allowImplicit: true }
29
- { code : "Array.from(x, function() { return; })" , options : allowImplicitOptions } ,
30
- { code : "Int32Array.from(x, function() { return; })" , options : allowImplicitOptions } ,
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 } } ,
31
44
32
- "Arrow.from(x, function() {})" ,
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 } } ,
33
48
34
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 } ] } ,
35
52
"foo.every(function() { return true; })" ,
36
53
"foo.filter(function() { return true; })" ,
37
54
"foo.find(function() { return true; })" ,
38
55
"foo.findIndex(function() { return true; })" ,
39
56
"foo.flatMap(function() { return true; })" ,
57
+ "foo.forEach(function() { return; })" ,
40
58
"foo.map(function() { return true; })" ,
41
59
"foo.reduce(function() { return true; })" ,
42
60
"foo.reduceRight(function() { return true; })" ,
43
61
"foo.some(function() { return true; })" ,
44
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; } })" ,
45
68
46
69
// options: { allowImplicit: true }
70
+ { code : "Array.from(x, function() { return; })" , options : allowImplicitOptions } ,
71
+ { code : "Int32Array.from(x, function() { return; })" , options : allowImplicitOptions } ,
47
72
{ code : "foo.every(function() { return; })" , options : allowImplicitOptions } ,
48
73
{ code : "foo.filter(function() { return; })" , options : allowImplicitOptions } ,
49
74
{ code : "foo.find(function() { return; })" , options : allowImplicitOptions } ,
50
75
{ code : "foo.findIndex(function() { return; })" , options : allowImplicitOptions } ,
51
76
{ code : "foo.flatMap(function() { return; })" , options : allowImplicitOptions } ,
77
+ { code : "foo.forEach(function() { return; })" , options : allowImplicitOptions } ,
52
78
{ code : "foo.map(function() { return; })" , options : allowImplicitOptions } ,
53
79
{ code : "foo.reduce(function() { return; })" , options : allowImplicitOptions } ,
54
80
{ code : "foo.reduceRight(function() { return; })" , options : allowImplicitOptions } ,
55
81
{ code : "foo.some(function() { return; })" , options : allowImplicitOptions } ,
56
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 } ,
57
106
107
+ "Arrow.from(x, function() {})" ,
58
108
"foo.abc(function() {})" ,
59
109
"every(function() {})" ,
60
110
"foo[every](function() {})" ,
61
111
"var every = function() {}" ,
62
112
{ code : "foo[`${every}`](function() {})" , parserOptions : { ecmaVersion : 6 } } ,
63
- { code : "foo.every(() => true)" , parserOptions : { ecmaVersion : 6 } } ,
64
-
65
- // options: { allowImplicit: false }
66
- { code : "foo.every(() => { return true; })" , parserOptions : { ecmaVersion : 6 } } ,
67
- "foo.every(function() { if (a) return true; else return false; })" ,
68
- "foo.every(function() { switch (a) { case 0: bar(); default: return true; } })" ,
69
- "foo.every(function() { try { bar(); return true; } catch (err) { return false; } })" ,
70
- "foo.every(function() { try { bar(); } finally { return true; } })" ,
113
+ { code : "foo.every(() => true)" , parserOptions : { ecmaVersion : 6 } }
71
114
72
- // options: { allowImplicit: true }
73
- { code : "foo.every(() => { return; })" , options : allowImplicitOptions , parserOptions : { ecmaVersion : 6 } } ,
74
- { code : "foo.every(function() { if (a) return; else return a; })" , options : allowImplicitOptions } ,
75
- { code : "foo.every(function() { switch (a) { case 0: bar(); default: return; } })" , options : allowImplicitOptions } ,
76
- { code : "foo.every(function() { try { bar(); return; } catch (err) { return; } })" , options : allowImplicitOptions } ,
77
- { code : "foo.every(function() { try { bar(); } finally { return; } })" , options : allowImplicitOptions } ,
78
-
79
- "foo.every(function(){}())" ,
80
- "foo.every(function(){ return function() { return true; }; }())" ,
81
- "foo.every(function(){ return function() { return; }; })" ,
82
- { code : "foo.map(async function(){})" , parserOptions : { ecmaVersion : 8 } } ,
83
- { code : "foo.map(async () => {})" , parserOptions : { ecmaVersion : 8 } } ,
84
- { code : "foo.map(function* () {})" , parserOptions : { ecmaVersion : 6 } }
85
115
] ,
86
116
invalid : [
117
+
87
118
{ code : "Array.from(x, function() {})" , errors : [ { messageId : "expectedInside" , data : { name : "function" } } ] } ,
88
119
{ code : "Array.from(x, function foo() {})" , errors : [ { messageId : "expectedInside" , data : { name : "function 'foo'" } } ] } ,
89
120
{ code : "Int32Array.from(x, function() {})" , errors : [ { messageId : "expectedInside" , data : { name : "function" } } ] } ,
@@ -134,6 +165,40 @@ ruleTester.run("array-callback-return", rule, {
134
165
{ code : "foo.every(function(){ return function() {}; }())" , errors : [ { message : "Expected to return a value in function." , column : 30 } ] } ,
135
166
{ code : "foo.every(function(){ return function foo() {}; }())" , errors : [ { message : "Expected to return a value in function 'foo'." , column : 39 } ] } ,
136
167
{ code : "foo.every(() => {})" , options : [ { allowImplicit : false } ] , parserOptions : { ecmaVersion : 6 } , errors : [ { message : "Expected to return a value in arrow function." } ] } ,
137
- { code : "foo.every(() => {})" , options : [ { allowImplicit : true } ] , 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
+
138
203
]
139
204
} ) ;
0 commit comments