]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/arrow-body-style.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / arrow-body-style.md
CommitLineData
8f9d1d4d
DC
1---
2title: arrow-body-style
8f9d1d4d
DC
3rule_type: suggestion
4---
eb39fafa
DC
5
6Arrow 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
8## Rule Details
9
10This rule can enforce or disallow the use of braces around arrow function body.
11
12## Options
13
14The rule takes one or two options. The first is a string, which can be:
15
16* `"always"` enforces braces around the function body
17* `"as-needed"` enforces no braces where they can be omitted (default)
18* `"never"` enforces no braces around the function body (constrains arrow functions to the role of returning an expression)
19
20The 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.
21
22```json
23"arrow-body-style": ["error", "always"]
24```
25
26### always
27
28Examples of **incorrect** code for this rule with the `"always"` option:
29
8f9d1d4d
DC
30:::incorrect
31
eb39fafa
DC
32```js
33/*eslint arrow-body-style: ["error", "always"]*/
34/*eslint-env es6*/
35let foo = () => 0;
36```
37
8f9d1d4d
DC
38:::
39
eb39fafa
DC
40Examples of **correct** code for this rule with the `"always"` option:
41
8f9d1d4d
DC
42:::correct
43
eb39fafa
DC
44```js
45let foo = () => {
46 return 0;
47};
48let foo = (retv, name) => {
49 retv[name] = true;
50 return retv;
51};
52```
53
8f9d1d4d
DC
54:::
55
eb39fafa
DC
56### as-needed
57
58Examples of **incorrect** code for this rule with the default `"as-needed"` option:
59
8f9d1d4d
DC
60:::incorrect
61
eb39fafa
DC
62```js
63/*eslint arrow-body-style: ["error", "as-needed"]*/
64/*eslint-env es6*/
65
66let foo = () => {
67 return 0;
68};
69let foo = () => {
70 return {
71 bar: {
72 foo: 1,
73 bar: 2,
74 }
75 };
76};
77```
78
8f9d1d4d
DC
79:::
80
eb39fafa
DC
81Examples of **correct** code for this rule with the default `"as-needed"` option:
82
8f9d1d4d
DC
83:::correct
84
eb39fafa
DC
85```js
86/*eslint arrow-body-style: ["error", "as-needed"]*/
87/*eslint-env es6*/
88
89let foo = () => 0;
90let foo = (retv, name) => {
91 retv[name] = true;
92 return retv;
93};
94let foo = () => ({
95 bar: {
96 foo: 1,
97 bar: 2,
98 }
99});
100let foo = () => { bar(); };
101let foo = () => {};
102let foo = () => { /* do nothing */ };
103let foo = () => {
104 // do nothing.
105};
106let foo = () => ({ bar: 0 });
107```
108
8f9d1d4d
DC
109:::
110
eb39fafa
DC
111#### requireReturnForObjectLiteral
112
113> This option is only applicable when used in conjunction with the `"as-needed"` option.
114
115Examples of **incorrect** code for this rule with the `{ "requireReturnForObjectLiteral": true }` option:
116
8f9d1d4d
DC
117:::incorrect
118
eb39fafa
DC
119```js
120/*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
121/*eslint-env es6*/
122let foo = () => ({});
123let foo = () => ({ bar: 0 });
124```
125
8f9d1d4d
DC
126:::
127
eb39fafa
DC
128Examples of **correct** code for this rule with the `{ "requireReturnForObjectLiteral": true }` option:
129
8f9d1d4d
DC
130:::correct
131
eb39fafa
DC
132```js
133/*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
134/*eslint-env es6*/
135
136let foo = () => {};
137let foo = () => { return { bar: 0 }; };
138```
139
8f9d1d4d
DC
140:::
141
eb39fafa
DC
142### never
143
144Examples of **incorrect** code for this rule with the `"never"` option:
145
8f9d1d4d
DC
146:::incorrect
147
eb39fafa
DC
148```js
149/*eslint arrow-body-style: ["error", "never"]*/
150/*eslint-env es6*/
151
152let foo = () => {
153 return 0;
154};
155let foo = (retv, name) => {
156 retv[name] = true;
157 return retv;
158};
159```
160
8f9d1d4d
DC
161:::
162
eb39fafa
DC
163Examples of **correct** code for this rule with the `"never"` option:
164
8f9d1d4d
DC
165:::correct
166
eb39fafa
DC
167```js
168/*eslint arrow-body-style: ["error", "never"]*/
169/*eslint-env es6*/
170
171let foo = () => 0;
172let foo = () => ({ foo: 0 });
173```
8f9d1d4d
DC
174
175:::