arrow-body-style
Enforce one style for the braces around arrow function bodies
fixable
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#
example.js1 problem
const double = items.map(item => {Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
return item * 2;
});example.jsNo problems
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. |
example.jswith
"always"1 problemconst double = items.map(item => item * 2Expected block statement surrounding arrow body.);example.jswith
"never"1 problemconst logged = items.map(item => {Unexpected block statement surrounding arrow body.
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 => ({ ... }).
example.jswith
"as-needed", { "requireReturnForObjectLiteral": true }1 problemconst toPoint = item => ({ x: item.left, y: item.top }Expected block statement surrounding arrow body.);example.jswith
"as-needed", { "requireReturnForObjectLiteral": true }No problemsconst toPoint = item => {
return { x: item.left, y: item.top };
};