Skip to content

eslint/arrow-body-style Style

🚧 An auto-fix is still under development.

What it does

This rule can enforce or disallow the use of braces around arrow function body.

Why is this bad?

Arrow functions have two syntactic forms for their function bodies. They may be defined with a block body (denoted by curly braces) () => { ... } or with a single expression () => ..., whose value is implicitly returned.

Examples

Examples of incorrect code for this rule with the always option:

js
const foo = () => 0;

Examples of correct code for this rule with the always option:

js
const foo = () => {
  return 0;
};

Examples of incorrect code for this rule with the as-needed option:

js
const foo = () => {
  return 0;
};

Examples of correct code for this rule with the as-needed option:

js
const foo1 = () => 0;

const foo2 = (retv, name) => {
  retv[name] = true;
  return retv;
};

Examples of incorrect code for this rule with the { "requireReturnForObjectLiteral": true } option:

js
/* arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
const foo = () => ({});
const bar = () => ({ bar: 0 });

Examples of correct code for this rule with the { "requireReturnForObjectLiteral": true } option:

js
/* arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
const foo = () => {};
const bar = () => {
  return { bar: 0 };
};

Examples of incorrect code for this rule with the never option:

js
const foo = () => {
  return 0;
};

Examples of correct code for this rule with the never option:

js
const foo = () => 0;
const bar = () => ({ foo: 0 });

Options

The rule takes one or two options. The first is a string, which can be:

  • always enforces braces around the function body
  • never enforces no braces where they can be omitted (default)
  • as-needed enforces no braces around the function body (constrains arrow functions to the role of returning an expression)

The second one is an object for more fine-grained configuration when the first option is "as-needed". Currently, the only available option is requireReturnForObjectLiteral, a boolean property. It’s false by default. If set to true, it requires braces and an explicit return for object literals.

json
{
  "arrow-body-style": ["error", "as-needed", { "requireReturnForObjectLiteral": true }]
}

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny arrow-body-style
json
{
  "rules": {
    "arrow-body-style": "error"
  }
}

References

Released under the MIT License.