69 lines
3.4 KiB
Plaintext
69 lines
3.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In JavaScript, many array methods take a callback function as an argument. These methods are designed to transform or filter arrays based on the logic provided in the callback function. The callback function is called sequentially, and the return value of the callback function is used to determine the return value of the `Array` method.
|
|
|
|
If the callback function does not return a value, the array method may not work as expected and is most likely a mistake.
|
|
|
|
This rule applies to the following methods of an array:
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from[``++Array.from++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every[``++Array.prototype.every++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter[``++Array.prototype.filter++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find[``++Array.prototype.find++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast[``++Array.prototype.findLast++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex[``++Array.prototype.findIndex++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex[``++Array.prototype.findLastIndex++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map[``++Array.prototype.map++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap[``++Array.prototype.flatMap++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce[``++Array.prototype.reduce++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight[``++Array.prototype.reduceRight++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some[``++Array.prototype.some++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort[``++Array.prototype.sort++``]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted[``++Array.prototype.toSorted++``]
|
|
|
|
If there is no `return`, the callback will implicitly return ``++undefined++``, which may cause unexpected behavior or errors in the code.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
let arr = ["a", "b", "c"];
|
|
let merged = arr.reduce(function(a, b) {
|
|
a.concat(b); // Noncompliant: No return statement, will result in TypeError
|
|
});
|
|
----
|
|
|
|
Always add a return statement to the callback function passed to the array method.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
let arr = ["a", "b", "c"];
|
|
let merged = arr.reduce(function(a, b) {
|
|
return a.concat(b);
|
|
});
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array[Array]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Add a "return" statement to this callback.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 9 Feb 2017, 14:54:23 Carlo Bottiglieri wrote:
|
|
Moved to Blocker severity, as the likehood is high, not low.
|
|
|
|
endif::env-github,rspecator-view[]
|