]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-empty-function.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-empty-function.md
CommitLineData
eb39fafa
DC
1# Disallow empty functions (no-empty-function)
2
3Empty functions can reduce readability because readers need to guess whether it's intentional or not.
4So writing a clear comment for empty functions is a good practice.
5
6```js
7function foo() {
8 // do nothing.
9}
10```
11
12Especially, the empty block of arrow functions might be confusing developers.
13It's very similar to an empty object literal.
14
15```js
16list.map(() => {}); // This is a block, would return undefined.
17list.map(() => ({})); // This is an empty object.
18```
19
20## Rule Details
21
22This rule is aimed at eliminating empty functions.
23A function will not be considered a problem if it contains a comment.
24
25Examples of **incorrect** code for this rule:
26
27```js
28/*eslint no-empty-function: "error"*/
29/*eslint-env es6*/
30
31function foo() {}
32
33var foo = function() {};
34
35var foo = () => {};
36
37function* foo() {}
38
39var foo = function*() {};
40
41var obj = {
42 foo: function() {},
43
44 foo: function*() {},
45
46 foo() {},
47
48 *foo() {},
49
50 get foo() {},
51
52 set foo(value) {}
53};
54
55class A {
56 constructor() {}
57
58 foo() {}
59
60 *foo() {}
61
62 get foo() {}
63
64 set foo(value) {}
65
66 static foo() {}
67
68 static *foo() {}
69
70 static get foo() {}
71
72 static set foo(value) {}
73}
74```
75
76Examples of **correct** code for this rule:
77
78```js
79/*eslint no-empty-function: "error"*/
80/*eslint-env es6*/
81
82function foo() {
83 // do nothing.
84}
85
86var foo = function() {
87 // any clear comments.
88};
89
90var foo = () => {
91 bar();
92};
93
94function* foo() {
95 // do nothing.
96}
97
98var foo = function*() {
99 // do nothing.
100};
101
102var obj = {
103 foo: function() {
104 // do nothing.
105 },
106
107 foo: function*() {
108 // do nothing.
109 },
110
111 foo() {
112 // do nothing.
113 },
114
115 *foo() {
116 // do nothing.
117 },
118
119 get foo() {
120 // do nothing.
121 },
122
123 set foo(value) {
124 // do nothing.
125 }
126};
127
128class A {
129 constructor() {
130 // do nothing.
131 }
132
133 foo() {
134 // do nothing.
135 }
136
137 *foo() {
138 // do nothing.
139 }
140
141 get foo() {
142 // do nothing.
143 }
144
145 set foo(value) {
146 // do nothing.
147 }
148
149 static foo() {
150 // do nothing.
151 }
152
153 static *foo() {
154 // do nothing.
155 }
156
157 static get foo() {
158 // do nothing.
159 }
160
161 static set foo(value) {
162 // do nothing.
163 }
164}
165```
166
167## Options
168
169This rule has an option to allow specific kinds of functions to be empty.
170
171* `allow` (`string[]`) - A list of kind to allow empty functions. List items are some of the following strings. An empty array (`[]`) by default.
172 * `"functions"` - Normal functions.
173 * `"arrowFunctions"` - Arrow functions.
174 * `"generatorFunctions"` - Generator functions.
175 * `"methods"` - Class methods and method shorthands of object literals.
176 * `"generatorMethods"` - Class methods and method shorthands of object literals with generator.
177 * `"getters"` - Getters.
178 * `"setters"` - Setters.
179 * `"constructors"` - Class constructors.
180 * `"asyncFunctions"` - Async functions.
181 * `"asyncMethods"` - Async class methods and method shorthands of object literals.
182
34eeec05 183### allow: functions
eb39fafa
DC
184
185Examples of **correct** code for the `{ "allow": ["functions"] }` option:
186
187```js
188/*eslint no-empty-function: ["error", { "allow": ["functions"] }]*/
189
190function foo() {}
191
192var foo = function() {};
193
194var obj = {
195 foo: function() {}
196};
197```
198
34eeec05 199### allow: arrowFunctions
eb39fafa
DC
200
201Examples of **correct** code for the `{ "allow": ["arrowFunctions"] }` option:
202
203```js
204/*eslint no-empty-function: ["error", { "allow": ["arrowFunctions"] }]*/
205/*eslint-env es6*/
206
207var foo = () => {};
208```
209
34eeec05 210### allow: generatorFunctions
eb39fafa
DC
211
212Examples of **correct** code for the `{ "allow": ["generatorFunctions"] }` option:
213
214```js
215/*eslint no-empty-function: ["error", { "allow": ["generatorFunctions"] }]*/
216/*eslint-env es6*/
217
218function* foo() {}
219
220var foo = function*() {};
221
222var obj = {
223 foo: function*() {}
224};
225```
226
34eeec05 227### allow: methods
eb39fafa
DC
228
229Examples of **correct** code for the `{ "allow": ["methods"] }` option:
230
231```js
232/*eslint no-empty-function: ["error", { "allow": ["methods"] }]*/
233/*eslint-env es6*/
234
235var obj = {
236 foo() {}
237};
238
239class A {
240 foo() {}
241 static foo() {}
242}
243```
244
34eeec05 245### allow: generatorMethods
eb39fafa
DC
246
247Examples of **correct** code for the `{ "allow": ["generatorMethods"] }` option:
248
249```js
250/*eslint no-empty-function: ["error", { "allow": ["generatorMethods"] }]*/
251/*eslint-env es6*/
252
253var obj = {
254 *foo() {}
255};
256
257class A {
258 *foo() {}
259 static *foo() {}
260}
261```
262
34eeec05 263### allow: getters
eb39fafa
DC
264
265Examples of **correct** code for the `{ "allow": ["getters"] }` option:
266
267```js
268/*eslint no-empty-function: ["error", { "allow": ["getters"] }]*/
269/*eslint-env es6*/
270
271var obj = {
272 get foo() {}
273};
274
275class A {
276 get foo() {}
277 static get foo() {}
278}
279```
280
34eeec05 281### allow: setters
eb39fafa
DC
282
283Examples of **correct** code for the `{ "allow": ["setters"] }` option:
284
285```js
286/*eslint no-empty-function: ["error", { "allow": ["setters"] }]*/
287/*eslint-env es6*/
288
289var obj = {
290 set foo(value) {}
291};
292
293class A {
294 set foo(value) {}
295 static set foo(value) {}
296}
297```
298
34eeec05 299### allow: constructors
eb39fafa
DC
300
301Examples of **correct** code for the `{ "allow": ["constructors"] }` option:
302
303```js
304/*eslint no-empty-function: ["error", { "allow": ["constructors"] }]*/
305/*eslint-env es6*/
306
307class A {
308 constructor() {}
309}
310```
311
34eeec05 312### allow: asyncFunctions
eb39fafa
DC
313
314Examples of **correct** code for the `{ "allow": ["asyncFunctions"] }` options:
315
316```js
317/*eslint no-empty-function: ["error", { "allow": ["asyncFunctions"] }]*/
318/*eslint-env es2017*/
319
320async function a(){}
321```
322
34eeec05 323### allow: asyncMethods
eb39fafa
DC
324
325Examples of **correct** code for the `{ "allow": ["asyncMethods"] }` options:
326
327```js
328/*eslint no-empty-function: ["error", { "allow": ["asyncMethods"] }]*/
329/*eslint-env es2017*/
330
331var obj = {
332 async foo() {}
333};
334
335class A {
336 async foo() {}
337 static async foo() {}
338}
339```
340
341## When Not To Use It
342
343If you don't want to be notified about empty functions, then it's safe to disable this rule.
344
345## Related Rules
346
347* [no-empty](./no-empty.md)