Skip to content
← Back to rules

eslint/arrow-body-style Style

🛠️ An auto-fix is available for this rule.

What it does

This rule can enforce or disallow the use of braces around arrow function body. Arrow functions can use either:

  • a block body () => { ... }
  • or a concise body () => expression with an implicit return.

Why is this bad?

Inconsistent use of block vs. concise bodies makes code harder to read. Concise bodies are limited to a single expression, whose value is implicitly returned.

Examples

"never"

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

js
/* arrow-body-style: ["error", "never"] */

/* ✘ Bad: */
const foo = () => {
  return 0;
};

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

js
/* arrow-body-style: ["error", "never"] */

/* ✔ Good: */
const foo = () => 0;
const bar = () => ({ foo: 0 });

"always"

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

js
/* arrow-body-style: ["error", "always"] */

/* ✘ Bad: */
const foo = () => 0;

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

js
/* arrow-body-style: ["error", "always"] */

/* ✔ Good: */
const foo = () => {
  return 0;
};

"as-needed" (default)

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

js
/* arrow-body-style: ["error", "as-needed"] */

/* ✘ Bad: */
const foo = () => {
  return 0;
};

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

js
/* arrow-body-style: ["error", "as-needed"] */

/* ✔ Good: */
const foo1 = () => 0;

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

const foo3 = () => {
  bar();
};

"as-needed" with requireReturnForObjectLiteral

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

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

/* ✘ Bad: */
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 }] */

/* ✔ Good: */
const foo = () => {};
const bar = () => {
  return { bar: 0 };
};

Configuration

The 1st option

type: "as-needed" | "always" | "never"

"as-needed"

Enforces no braces where they can be omitted (default).

"always"

Enforces braces around the function body.

"never"

Enforces no braces around the function body (constrains arrow functions to the role of returning an expression).

The 2nd option

This option is an object with the following properties:

requireReturnForObjectLiteral

type: boolean

default: false

Requires braces and an explicit return for object literals. This option only applies when the first option is "as-needed".

How to use

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

json
{
  "rules": {
    "arrow-body-style": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "arrow-body-style": "error",
  },
});
bash
oxlint --deny arrow-body-style

Version

This rule was added in v1.4.0.

References