@@ -16,32 +16,101 @@ const source = `https://github.com/oxc-project/oxc/blob/${ data }/crates/oxc_lin
16
16
### What it does
17
17
18
18
This rule can enforce or disallow the use of braces around arrow function body.
19
+ Arrow functions can use either:
20
+
21
+ - a block body ` () => { ... } `
22
+ - or a concise body ` () => expression ` with an implicit return.
19
23
20
24
### Why is this bad?
21
25
22
- Arrow functions have two syntactic forms for their function bodies.
23
- They may be defined with a block body (denoted by curly braces) () => { ... }
24
- or with a single expression () => ..., whose value is implicitly returned.
26
+ Inconsistent use of block vs. concise bodies makes code harder to read.
27
+ Concise bodies are limited to a single expression, whose value is implicitly returned.
28
+
29
+ ### Options
30
+
31
+ First option:
32
+
33
+ - Type: ` string `
34
+ - Enum: ` "always" ` , ` "as-needed" ` , ` "never" `
35
+ - Default: ` "never" `
36
+
37
+ Possible values:
38
+
39
+ - ` never ` enforces no braces where they can be omitted (default)
40
+ - ` always ` enforces braces around the function body
41
+ - ` as-needed ` enforces no braces around the function body (constrains arrow functions to the role of returning an expression)
42
+
43
+ Second option:
44
+
45
+ - Type: ` object `
46
+ - Properties:
47
+ - ` requireReturnForObjectLiteral ` : ` boolean ` (default: ` false ` ) - requires braces and an explicit return for object literals.
48
+
49
+ Note: This option only applies when the first option is ` "as-needed" ` .
50
+
51
+ Example configuration:
52
+
53
+ ``` json
54
+ {
55
+ "arrow-body-style" : [" error" , " as-needed" , { "requireReturnForObjectLiteral" : true }]
56
+ }
57
+ ```
25
58
26
59
### Examples
27
60
61
+ #### ` "never" ` (default)
62
+
63
+ Examples of ** incorrect** code for this rule with the ` never ` option:
64
+
65
+ ``` js
66
+ /* arrow-body-style: ["error", "never"] */
67
+
68
+ /* ✘ Bad: */
69
+ const foo = () => {
70
+ return 0 ;
71
+ };
72
+ ```
73
+
74
+ Examples of ** correct** code for this rule with the ` never ` option:
75
+
76
+ ``` js
77
+ /* arrow-body-style: ["error", "never"] */
78
+
79
+ /* ✔ Good: */
80
+ const foo = () => 0 ;
81
+ const bar = () => ({ foo: 0 });
82
+ ```
83
+
84
+ #### ` "always" `
85
+
28
86
Examples of ** incorrect** code for this rule with the ` always ` option:
29
87
30
88
``` js
89
+ /* arrow-body-style: ["error", "always"] */
90
+
91
+ /* ✘ Bad: */
31
92
const foo = () => 0 ;
32
93
```
33
94
34
95
Examples of ** correct** code for this rule with the ` always ` option:
35
96
36
97
``` js
98
+ /* arrow-body-style: ["error", "always"] */
99
+
100
+ /* ✔ Good: */
37
101
const foo = () => {
38
102
return 0 ;
39
103
};
40
104
```
41
105
106
+ #### ` "as-needed" `
107
+
42
108
Examples of ** incorrect** code for this rule with the ` as-needed ` option:
43
109
44
110
``` js
111
+ /* arrow-body-style: ["error", "as-needed"] */
112
+
113
+ /* ✘ Bad: */
45
114
const foo = () => {
46
115
return 0 ;
47
116
};
@@ -50,66 +119,45 @@ const foo = () => {
50
119
Examples of ** correct** code for this rule with the ` as-needed ` option:
51
120
52
121
``` js
122
+ /* arrow-body-style: ["error", "as-needed"] */
123
+
124
+ /* ✔ Good: */
53
125
const foo1 = () => 0 ;
54
126
55
127
const foo2 = (retv , name ) => {
56
128
retv[name] = true ;
57
129
return retv;
58
130
};
131
+
132
+ const foo3 = () => {
133
+ bar ();
134
+ };
59
135
```
60
136
61
- Examples of ** incorrect** code for this rule with the { "requireReturnForObjectLiteral": true } option:
137
+ #### ` "as-needed" ` with ` requireReturnForObjectLiteral `
138
+
139
+ Examples of ** incorrect** code for this rule with the ` { "requireReturnForObjectLiteral": true } ` option:
62
140
63
141
``` js
64
142
/* arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
143
+
144
+ /* ✘ Bad: */
65
145
const foo = () => ({});
66
146
const bar = () => ({ bar: 0 });
67
147
```
68
148
69
- Examples of ** correct** code for this rule with the { "requireReturnForObjectLiteral": true } option:
149
+ Examples of ** correct** code for this rule with the ` { "requireReturnForObjectLiteral": true } ` option:
70
150
71
151
``` js
72
152
/* arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
153
+
154
+ /* ✔ Good: */
73
155
const foo = () => {};
74
156
const bar = () => {
75
157
return { bar: 0 };
76
158
};
77
159
```
78
160
79
- Examples of ** incorrect** code for this rule with the ` never ` option:
80
-
81
- ``` js
82
- const foo = () => {
83
- return 0 ;
84
- };
85
- ```
86
-
87
- Examples of ** correct** code for this rule with the ` never ` option:
88
-
89
- ``` js
90
- const foo = () => 0 ;
91
- const bar = () => ({ foo: 0 });
92
- ```
93
-
94
- ### Options
95
-
96
- The rule takes one or two options. The first is a string, which can be:
97
-
98
- - ` always ` enforces braces around the function body
99
- - ` never ` enforces no braces where they can be omitted (default)
100
- - ` as-needed ` enforces no braces around the function body (constrains arrow functions to the role of returning an expression)
101
-
102
- The second one is an object for more fine-grained configuration
103
- when the first option is "as-needed". Currently,
104
- the only available option is requireReturnForObjectLiteral, a boolean property.
105
- It’s false by default. If set to true, it requires braces and an explicit return for object literals.
106
-
107
- ``` json
108
- {
109
- "arrow-body-style" : [" error" , " as-needed" , { "requireReturnForObjectLiteral" : true }]
110
- }
111
- ```
112
-
113
161
## How to use
114
162
115
163
To ** enable** this rule in the CLI or using the config file, you can use:
0 commit comments