# no-empty-function

> Disallow functions with an empty body

An empty function reads like something that was never finished. You can't tell whether it does nothing on purpose, like a default callback, or whether the body is missing.

A comment in the body answers that question, and the rule accepts it.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"no-empty-function" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"no-empty-function" = ["error", {
  allow = [], # list of strings
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```js
function onClose() {}

const handlers = {
  onError: () => {},
};
```

Not reported:

{/* good */}
```js
function onClose() {
  // Nothing to clean up.
}

const handlers = {
  onError: () => {
    // Errors are reported by the caller.
  },
};
```

In TypeScript, a constructor that declares fields in its parameters does its work without a body:

{/* good */}
```ts
class User {
  constructor(private name: string) {}
}
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `allow` | `[]` | The kinds of functions that can be empty. |

| Kind | Allows |
| --- | --- |
| `"functions"` | `function noop() {}` and `function () {}` |
| `"arrowFunctions"` | `() => {}`, even if it's `async` |
| `"generatorFunctions"` | `function* items() {}` |
| `"asyncFunctions"` | `async function load() {}` |
| `"methods"` | `render() {}` in a class or an object |
| `"generatorMethods"` | `*items() {}` |
| `"asyncMethods"` | `async load() {}` |
| `"getters"` | `get name() {}` |
| `"setters"` | `set name(value) {}` |
| `"constructors"` | `constructor() {}` |
| `"privateConstructors"` | `private constructor() {}` |
| `"protectedConstructors"` | `protected constructor() {}` |
| `"decoratedFunctions"` | a method with a decorator |
| `"overrideMethods"` | `override render() {}` |

Each kind only allows what its row shows. `"methods"` doesn't cover `async load() {}`, and `"arrowFunctions"` doesn't cover an empty `function`:

{/* good {"options": [{"allow": ["arrowFunctions"]}]} */}
```js
const noop = () => {};
const wait = async () => {};
```

{/* bad {"options": [{"allow": ["arrowFunctions"]}]} */}
```js
function noop() {}
```

A private constructor is a common way to stop `new Registry()` from outside the class:

{/* good {"options": [{"allow": ["privateConstructors"]}]} */}
```ts
class Registry {
  static shared = new Registry();
  private constructor() {}
}
```
