1 # Require braces in arrow function body (arrow-body-style)
3 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.
7 This rule can enforce or disallow the use of braces around arrow function body.
11 The rule takes one or two options. The first is a string, which can be:
13 * `"always"` enforces braces around the function body
14 * `"as-needed"` enforces no braces where they can be omitted (default)
15 * `"never"` enforces no braces around the function body (constrains arrow functions to the role of returning an expression)
17 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.
20 "arrow-body-style": ["error", "always"]
25 Examples of **incorrect** code for this rule with the `"always"` option:
28 /*eslint arrow-body-style: ["error", "always"]*/
33 Examples of **correct** code for this rule with the `"always"` option:
39 let foo = (retv, name) => {
47 Examples of **incorrect** code for this rule with the default `"as-needed"` option:
50 /*eslint arrow-body-style: ["error", "as-needed"]*/
66 Examples of **correct** code for this rule with the default `"as-needed"` option:
69 /*eslint arrow-body-style: ["error", "as-needed"]*/
73 let foo = (retv, name) => {
83 let foo = () => { bar(); };
85 let foo = () => { /* do nothing */ };
89 let foo = () => ({ bar: 0 });
92 #### requireReturnForObjectLiteral
94 > This option is only applicable when used in conjunction with the `"as-needed"` option.
96 Examples of **incorrect** code for this rule with the `{ "requireReturnForObjectLiteral": true }` option:
99 /*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
101 let foo = () => ({});
102 let foo = () => ({ bar: 0 });
105 Examples of **correct** code for this rule with the `{ "requireReturnForObjectLiteral": true }` option:
108 /*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
112 let foo = () => { return { bar: 0 }; };
117 Examples of **incorrect** code for this rule with the `"never"` option:
120 /*eslint arrow-body-style: ["error", "never"]*/
126 let foo = (retv, name) => {
132 Examples of **correct** code for this rule with the `"never"` option:
135 /*eslint arrow-body-style: ["error", "never"]*/
139 let foo = () => ({ foo: 0 });