Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create rule S6959: "Array.reduce()" calls should include an initial value #3818

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions rules/S6959/javascript/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"title": "\"Array.reduce()\" calls should include an initial value",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "2min"
},
"tags": [
"type-dependent"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6959",
"sqKey": "S6959",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "targeted",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
}
33 changes: 33 additions & 0 deletions rules/S6959/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
== Why is this an issue?

The `Array.prototype.reduce()` method in JavaScript is used to apply a function against an accumulator and each element in the array (from left to right) to reduce it to a single output value. It is a convenient method that can simplify logic in your code.

However, it's important to always provide an initial value as the second argument to `reduce()`. The initial value is used as the first argument to the first call of the callback function. If no initial value is supplied, JavaScript will use the first element of the array as the initial accumulator value and start iterating at the second element.

This can lead to runtime errors if the array is empty, as `reduce()` will throw a TypeError.

[source,javascript,diff-id=1,diff-type=noncompliant]
----
function sum(xs) {
return xs.reduce((acc, current) => acc + current); // Noncompliant
}
console.log(sum([1, 2, 3, 4, 5])); // Prints 15
console.log(sum([])); // TypeError: Reduce of empty array with no initial value
----

To fix this, always provide an initial value as the second argument to `reduce()`.

[source,javascript,diff-id=1,diff-type=compliant]
----
function sum(xs) {
return xs.reduce((acc, current) => acc + current, 0);
}
console.log(sum([1, 2, 3, 4, 5])); // Prints 15
console.log(sum([])); // Prints 0
----

== Resources
=== Documentation

* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce[Array.prototype.reduce()]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Reduce_of_empty_array_with_no_initial_value[TypeError: Reduce of empty array with no initial value]
2 changes: 2 additions & 0 deletions rules/S6959/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading