]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/arrow-body-style.md
31c0cfe0eeb52b0ab0142f9ad9804f16f319910e
[pve-eslint.git] / eslint / docs / rules / arrow-body-style.md
1 # Require braces in arrow function body (arrow-body-style)
2
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.
4
5 ## Rule Details
6
7 This rule can enforce or disallow the use of braces around arrow function body.
8
9 ## Options
10
11 The rule takes one or two options. The first is a string, which can be:
12
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)
16
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.
18
19 ```json
20 "arrow-body-style": ["error", "always"]
21 ```
22
23 ### always
24
25 Examples of **incorrect** code for this rule with the `"always"` option:
26
27 ```js
28 /*eslint arrow-body-style: ["error", "always"]*/
29 /*eslint-env es6*/
30 let foo = () => 0;
31 ```
32
33 Examples of **correct** code for this rule with the `"always"` option:
34
35 ```js
36 let foo = () => {
37 return 0;
38 };
39 let foo = (retv, name) => {
40 retv[name] = true;
41 return retv;
42 };
43 ```
44
45 ### as-needed
46
47 Examples of **incorrect** code for this rule with the default `"as-needed"` option:
48
49 ```js
50 /*eslint arrow-body-style: ["error", "as-needed"]*/
51 /*eslint-env es6*/
52
53 let foo = () => {
54 return 0;
55 };
56 let foo = () => {
57 return {
58 bar: {
59 foo: 1,
60 bar: 2,
61 }
62 };
63 };
64 ```
65
66 Examples of **correct** code for this rule with the default `"as-needed"` option:
67
68 ```js
69 /*eslint arrow-body-style: ["error", "as-needed"]*/
70 /*eslint-env es6*/
71
72 let foo = () => 0;
73 let foo = (retv, name) => {
74 retv[name] = true;
75 return retv;
76 };
77 let foo = () => ({
78 bar: {
79 foo: 1,
80 bar: 2,
81 }
82 });
83 let foo = () => { bar(); };
84 let foo = () => {};
85 let foo = () => { /* do nothing */ };
86 let foo = () => {
87 // do nothing.
88 };
89 let foo = () => ({ bar: 0 });
90 ```
91
92 #### requireReturnForObjectLiteral
93
94 > This option is only applicable when used in conjunction with the `"as-needed"` option.
95
96 Examples of **incorrect** code for this rule with the `{ "requireReturnForObjectLiteral": true }` option:
97
98 ```js
99 /*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
100 /*eslint-env es6*/
101 let foo = () => ({});
102 let foo = () => ({ bar: 0 });
103 ```
104
105 Examples of **correct** code for this rule with the `{ "requireReturnForObjectLiteral": true }` option:
106
107 ```js
108 /*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
109 /*eslint-env es6*/
110
111 let foo = () => {};
112 let foo = () => { return { bar: 0 }; };
113 ```
114
115 ### never
116
117 Examples of **incorrect** code for this rule with the `"never"` option:
118
119 ```js
120 /*eslint arrow-body-style: ["error", "never"]*/
121 /*eslint-env es6*/
122
123 let foo = () => {
124 return 0;
125 };
126 let foo = (retv, name) => {
127 retv[name] = true;
128 return retv;
129 };
130 ```
131
132 Examples of **correct** code for this rule with the `"never"` option:
133
134 ```js
135 /*eslint arrow-body-style: ["error", "never"]*/
136 /*eslint-env es6*/
137
138 let foo = () => 0;
139 let foo = () => ({ foo: 0 });
140 ```