Skip to content

useConsistentArrowReturn

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"useConsistentArrowReturn": "error"
}
}
}
}

¥Description

强制箭头函数体保持一致。

¥Enforce consistent arrow function bodies.

此规则强制要求当函数体仅包含一个 return 语句时,必须使用不带函数体代码块的箭头函数。以下情况不报告:

¥This rule enforces the use of arrow functions with no body block when the function body consists of a single return statement. This rule does not report when:

  • 函数体包含指令(例如 "use strict"),或者

    ¥the function body contains directives (e.g. "use strict"), or

  • 正文(或其子元素)包含注释,或者

    ¥the body (or its descendants) contain comments, or

  • 单个 return 没有参数 (return;)。

    ¥the single return has no argument (return;).

该修复会在需要时(例如对象字面量和序列表达式)将表达式用括号括起来以确保正确性。

¥The fix wraps expressions in parentheses when required for correctness (e.g. object literals and sequence expressions).

¥Examples

¥Invalid

const bar = () => {
return {
bar: {
foo: 1,
bar: 2,
}
};
};
code-block.js:1:14 lint/nursery/useConsistentArrowReturn  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The body of this arrow function contains a single return statement.

> 1 │ const bar = () => {
^^^^^^^
> 2 │ return {
> 3 │ bar: {
> 4 │ foo: 1,
> 5 │ bar: 2,
> 6 │ }
> 7 │ };
> 8 │ };
^
9 │

Safe fix: Remove the return statement.

1 - ·const·bar·=·()·=>·{
2 - ·····return·{
1+ ·const·bar·=·()·=>·({
3 2 bar: {
4 3 foo: 1,
5 4 bar: 2,
6 5 }
7 - ·····};
8 - ·};
6+ ·····});
9 7

¥Valid

const foo = () => 0;
const bar = () => { "use strict"; return 1 }
const baz = () => { /* intentional */ return x }
const qux = () => ({ a: 1 }) // already concise with parens

¥Related links