]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/arrow-body-style.md
first commit
[pve-eslint.git] / eslint / docs / rules / arrow-body-style.md
CommitLineData
eb39fafa
DC
1# Require braces in arrow function body (arrow-body-style)
2
3Arrow 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
7This rule can enforce or disallow the use of braces around arrow function body.
8
9## Options
10
11The 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
17The 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
25Examples of **incorrect** code for this rule with the `"always"` option:
26
27```js
28/*eslint arrow-body-style: ["error", "always"]*/
29/*eslint-env es6*/
30let foo = () => 0;
31```
32
33Examples of **correct** code for this rule with the `"always"` option:
34
35```js
36let foo = () => {
37 return 0;
38};
39let foo = (retv, name) => {
40 retv[name] = true;
41 return retv;
42};
43```
44
45### as-needed
46
47Examples 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
53let foo = () => {
54 return 0;
55};
56let foo = () => {
57 return {
58 bar: {
59 foo: 1,
60 bar: 2,
61 }
62 };
63};
64```
65
66Examples 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
72let foo = () => 0;
73let foo = (retv, name) => {
74 retv[name] = true;
75 return retv;
76};
77let foo = () => ({
78 bar: {
79 foo: 1,
80 bar: 2,
81 }
82});
83let foo = () => { bar(); };
84let foo = () => {};
85let foo = () => { /* do nothing */ };
86let foo = () => {
87 // do nothing.
88};
89let foo = () => ({ bar: 0 });
90```
91
92#### requireReturnForObjectLiteral
93
94> This option is only applicable when used in conjunction with the `"as-needed"` option.
95
96Examples 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*/
101let foo = () => ({});
102let foo = () => ({ bar: 0 });
103```
104
105Examples 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
111let foo = () => {};
112let foo = () => { return { bar: 0 }; };
113```
114
115### never
116
117Examples 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
123let foo = () => {
124 return 0;
125};
126let foo = (retv, name) => {
127 retv[name] = true;
128 return retv;
129};
130```
131
132Examples 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
138let foo = () => 0;
139let foo = () => ({ foo: 0 });
140```