Linter · Rules
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.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-empty-function" = "error" # or "warn"
Every option, set to its default:
bunfig.toml
[lint.rules]
"no-empty-function" = ["error", {
allow = [], # list of strings
}]
Examples
Reported:
function onClose() {}
const handlers = {
onError: () => {},
};
Not reported:
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:
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:
const noop = () => {};
const wait = async () => {};
function noop() {}
A private constructor is a common way to stop new Registry() from outside the class:
class Registry {
static shared = new Registry();
private constructor() {}
}