Linter · Rules
arrow-body-style
Enforce one style for the braces around arrow function bodies
x => x * 2 and x => { return x * 2; } do the same thing. By default, this rule reports the braces when the body is nothing but one return, so short functions stay short.
--fix removes or adds the braces and the return.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"arrow-body-style" = "error" # or "warn"
Every option, set to its default. The ones that are commented out are unset by default.
bunfig.toml
[lint.rules]
"arrow-body-style" = [
"error",
"as-needed", # "always", "never" or "as-needed"
{
# requireReturnForObjectLiteral = <true or false>,
},
]
Examples
Reported:
const double = items.map(item => {
return item * 2;
});
Not reported:
const double = items.map(item => item * 2);
const logged = items.map(item => {
console.log(item);
return item * 2;
});
Options
bunfig.toml
[lint.rules]
arrow-body-style = ["error", "always"]
| Option | What it does |
|---|---|
"as-needed" (default) | Report braces around a body that's only a return. |
"always" | Report every arrow function without braces. |
"never" | Report every arrow function with braces, no matter what's in them. --fix only rewrites a body that's a single return. |
const double = items.map(item => item * 2);
const logged = items.map(item => {
console.log(item);
return item * 2;
});
With "as-needed", a second option { "requireReturnForObjectLiteral": true } (default false) requires braces and a return when the function returns an object literal, instead of => ({ ... }).
const toPoint = item => ({ x: item.left, y: item.top });
const toPoint = item => {
return { x: item.left, y: item.top };
};