useIterableCallbackReturn
Summary
Section titled “Summary”- Rule available since:
v2.0.0
- Diagnostic Category:
lint/nursery/useIterableCallbackReturn
- This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Same as
array-callback-return
- Same as
Description
Section titled “Description”Enforce consistent return values in iterable callbacks.
This rule ensures that callbacks passed to certain iterable methods either always return a value or never return a value, depending on the method’s requirements.
Note that async and generator callbacks are ignored as they always return Promise
or
Generator
respectively.
Methods and Their Requirements
Section titled “Methods and Their Requirements”The following methods require a return in their callback:
every
filter
find
findIndex
findLast
findLastIndex
flatMap
map
reduce
reduceRight
some
sort
toSorted
—from
(when called onArray
)
A return value is disallowed in the method forEach
.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”[].map(() => { // Missing return value});
code-block.js:1:4 lint/nursery/useIterableCallbackReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This callback passed to map() iterable method should always return a value.
> 1 │ [].map(() => {
│ ^^^
2 │ // Missing return value
3 │ });
ℹ Add a return with a value to this callback.
[].forEach(() => { return 1; // Should not return a value});
code-block.js:1:4 lint/nursery/useIterableCallbackReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This callback passed to forEach() iterable method should not return a value.
> 1 │ [].forEach(() => {
│ ^^^^^^^
2 │ return 1; // Should not return a value
3 │ });
ℹ Either remove this return or remove the returned value.
> 1 │ [].forEach(() => {
│
> 2 │ return 1; // Should not return a value
│ ^^^^^^^
3 │ });
4 │
[].map(() => { return 1; // Correctly returns a value});
[].forEach(() => { // No return value, which is correct});
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useIterableCallbackReturn": "error" } } }}