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.

  1. Array.from()
  2. Array.prototype.every()
  3. Array.prototype.filter()
  4. Array.prototype.find()
  5. Array.prototype.findIndex()
  6. Array.prototype.flatMap()
  7. Array.prototype.map()
  8. Array.prototype.reduce()
  9. Array.prototype.reduceRight()
  10. Array.prototype.some()
  11. 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 side
var 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 side
var 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.

Was this documentation helpful?