no-empty-function
Disallow functions with an empty body
has suggestions
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#
example.js2 problems
function onClose() {}Unexpected empty function onClose.
const handlers = {
onError: () => {}Unexpected empty method onError.,
};example.jsNo problems
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:
example.tsNo problems
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:
example.jswith
{ "allow": ["arrowFunctions"] }No problemsconst noop = () => {};
const wait = async () => {};example.jswith
{ "allow": ["arrowFunctions"] }1 problemfunction noop() {}Unexpected empty function noop.A private constructor is a common way to stop new Registry() from outside the class:
example.tswith
{ "allow": ["privateConstructors"] }No problemsclass Registry {
static shared = new Registry();
private constructor() {}
}