Back to Biomejs

useIterableCallbackReturn

src/content/docs/linter/rules/use-iterable-callback-return.mdx

latest5.8 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/suspicious/useIterableCallbackReturn`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`array-callback-return`](https://eslint.org/docs/latest/rules/array-callback-return)

How to configure

json
{
	"linter": {
		"rules": {
			"suspicious": {
				"useIterableCallbackReturn": "error"
			}
		}
	}
}

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

The following methods require a return in their callback:

  • every
  • filter
  • find
  • findIndex
  • findLast
  • findLastIndex
  • flatMap
  • map
  • reduce
  • reduceRight
  • some
  • sort
  • toSortedfrom (when called on Array)

A return value is disallowed in the method forEach.

Examples

Invalid

js
[].map(() => {
    // Missing return value
});
<pre class="language-text"><code class="language-text">code-block.js:1:4 <a href="https://biomejs.dev/linter/rules/use-iterable-callback-return">lint/suspicious/useIterableCallbackReturn</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">This </span><span style="color: Tomato;"><strong>callback</strong></span><span style="color: Tomato;"> passed to </span><span style="color: Tomato;"><strong>map() iterable method</strong></span><span style="color: Tomato;"> should always </span><span style="color: Tomato;"><strong>return</strong></span><span style="color: Tomato;"> a value.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>[].map(() =&gt; &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> // Missing return value <strong>3 │ </strong>&#125;); <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Add a </span><span style="color: lightgreen;"><strong>return</strong></span><span style="color: lightgreen;"> with a value to this callback.</span> </code></pre>
js
[].forEach(() => {
    // No return value, which is correct
});
js
[].filter(() => {
    // Missing required return value
});
<pre class="language-text"><code class="language-text">code-block.js:1:4 <a href="https://biomejs.dev/linter/rules/use-iterable-callback-return">lint/suspicious/useIterableCallbackReturn</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">This </span><span style="color: Tomato;"><strong>callback</strong></span><span style="color: Tomato;"> passed to </span><span style="color: Tomato;"><strong>filter() iterable method</strong></span><span style="color: Tomato;"> should always </span><span style="color: Tomato;"><strong>return</strong></span><span style="color: Tomato;"> a value.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>[].filter(() =&gt; &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> // Missing required return value <strong>3 │ </strong>&#125;); <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Add a </span><span style="color: lightgreen;"><strong>return</strong></span><span style="color: lightgreen;"> with a value to this callback.</span> </code></pre>

Valid

js
[].map(() => {
    return 1; // Correctly returns a value
});
js
[].forEach(() => void null); // Void return value, which doesn't trigger the rule

Options

checkForEach

Since `v2.4.0

Default: true

When set to false, the rule will skip forEach callbacks that return a value.

Examples

json
{
	"linter": {
		"rules": {
			"suspicious": {
				"useIterableCallbackReturn": {
					"options": {
						"checkForEach": false
					}
				}
			}
		}
	}
}

js
[1, 2, 3].forEach((el) => {
    return el * 2;
});

When checkForEach is false (default), the above code will not trigger any diagnostic.

</TabItem> </Tabs>