useIterableCallbackReturn
Summary
Section titled Summary- 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 DescriptionEnforce 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.
Methods and Their Requirements
Section titled Methods and Their RequirementsThe 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 ExamplesInvalid
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 │
Valid
Section titled Valid[].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" } } }}