]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: arrow-body-style | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | --- | |
eb39fafa DC |
5 | |
6 | 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 | ||
8 | ## Rule Details | |
9 | ||
10 | This rule can enforce or disallow the use of braces around arrow function body. | |
11 | ||
12 | ## Options | |
13 | ||
14 | The 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 | ||
20 | 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. | |
21 | ||
22 | ```json | |
23 | "arrow-body-style": ["error", "always"] | |
24 | ``` | |
25 | ||
26 | ### always | |
27 | ||
28 | Examples 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*/ | |
35 | let foo = () => 0; | |
36 | ``` | |
37 | ||
8f9d1d4d DC |
38 | ::: |
39 | ||
eb39fafa DC |
40 | Examples of **correct** code for this rule with the `"always"` option: |
41 | ||
8f9d1d4d DC |
42 | :::correct |
43 | ||
eb39fafa DC |
44 | ```js |
45 | let foo = () => { | |
46 | return 0; | |
47 | }; | |
48 | let foo = (retv, name) => { | |
49 | retv[name] = true; | |
50 | return retv; | |
51 | }; | |
52 | ``` | |
53 | ||
8f9d1d4d DC |
54 | ::: |
55 | ||
eb39fafa DC |
56 | ### as-needed |
57 | ||
58 | Examples 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 | ||
66 | let foo = () => { | |
67 | return 0; | |
68 | }; | |
69 | let foo = () => { | |
70 | return { | |
71 | bar: { | |
72 | foo: 1, | |
73 | bar: 2, | |
74 | } | |
75 | }; | |
76 | }; | |
77 | ``` | |
78 | ||
8f9d1d4d DC |
79 | ::: |
80 | ||
eb39fafa DC |
81 | Examples 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 | ||
89 | let foo = () => 0; | |
90 | let foo = (retv, name) => { | |
91 | retv[name] = true; | |
92 | return retv; | |
93 | }; | |
94 | let foo = () => ({ | |
95 | bar: { | |
96 | foo: 1, | |
97 | bar: 2, | |
98 | } | |
99 | }); | |
100 | let foo = () => { bar(); }; | |
101 | let foo = () => {}; | |
102 | let foo = () => { /* do nothing */ }; | |
103 | let foo = () => { | |
104 | // do nothing. | |
105 | }; | |
106 | let 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 | ||
115 | Examples 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*/ | |
122 | let foo = () => ({}); | |
123 | let foo = () => ({ bar: 0 }); | |
124 | ``` | |
125 | ||
8f9d1d4d DC |
126 | ::: |
127 | ||
eb39fafa DC |
128 | Examples 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 | ||
136 | let foo = () => {}; | |
137 | let foo = () => { return { bar: 0 }; }; | |
138 | ``` | |
139 | ||
8f9d1d4d DC |
140 | ::: |
141 | ||
eb39fafa DC |
142 | ### never |
143 | ||
144 | Examples 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 | ||
152 | let foo = () => { | |
153 | return 0; | |
154 | }; | |
155 | let foo = (retv, name) => { | |
156 | retv[name] = true; | |
157 | return retv; | |
158 | }; | |
159 | ``` | |
160 | ||
8f9d1d4d DC |
161 | ::: |
162 | ||
eb39fafa DC |
163 | Examples 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 | ||
171 | let foo = () => 0; | |
172 | let foo = () => ({ foo: 0 }); | |
173 | ``` | |
8f9d1d4d DC |
174 | |
175 | ::: |