Callback function argument of Array
functions should have return
statement
- ARRAY_CALLBACK_RETURN_MISSING
- Code Quality
- Medium, Low
- No tags
This rule applies when a callback function argument of the following Array
functions does not have return
statement.
Array.from()
Array.prototype.every()
Array.prototype.filter()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.flatMap()
Array.prototype.map()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.some()
Array.prototype.sort()
Return value of the above functions with missing return
statement is always the same (an array filled with undefined in the case of Array.from
or Array.prototype.map
functions), and it is not likely to be a programmer's intent.
When the return value is not needed, it is recommended to use Array.prototype.forEach
function which does not make a new array as a return value.
Noncompliant Code Example
View with compliant examples side by sidevar memo = {}, arr = ["apple", "lemon", "orange"];
var ret1 = arr.map(function (curval, index) { // ARRAY_CALLBACK_RETURN_MISSING alarm because no value is returned in the callback function.
memo[curval] = index;
});
console.log(ret1); // 'ret1' is filled with undefined.
var ret2 = Array.from([1, 2, 3], function (x) { // ARRAY_CALLBACK_RETURN_MISSING alarm because no value is returned in the callback function.
x = x + 3;
});
console.log(ret2); // 'ret2' is filled with undefined.
Compliant Code Example
View with noncompliant examples side by sidevar memo = {}, arr = ["apple", "lemon", "orange"];
var ret1 = arr.map(function (curval, index) {
memo[curval] = index;
return memo[curval];
});
var ret2 = Array.from([1, 2, 3], function (x) {
x = x + 3;
return x;
});
Version
This rule was introduced in DeepScan 1.0.0-alpha.