useConsistentArrowReturn
Summary
Section titled “Summary”- Rule available since:
v2.2.3
- Diagnostic Category:
lint/nursery/useConsistentArrowReturn
- This rule has a safe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
arrow-body-style
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useConsistentArrowReturn": "error" } } }}
Description
Section titled “Description”Enforce consistent arrow function bodies.
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:
- the function body contains directives (e.g.
"use strict"
), or - the body (or its descendants) contain comments, or
- 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
Section titled “Examples”Invalid
Section titled “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 │
const foo = () => 0;const bar = () => { "use strict"; return 1 }const baz = () => { /* intentional */ return x }const qux = () => ({ a: 1 }) // already concise with parens
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.