@typescript-eslint/no-empty-function
Disallow functions with an empty body
An empty function is either unfinished or does nothing on purpose, and the reader can't tell which. If it's on purpose, say so in a comment in the body. A function with a comment in it isn't reported.
A constructor that declares properties in its parameters, such as constructor(private name: string) {}, isn't reported either.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-empty-function" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-empty-function" = ["error", {
allow = [], # list of strings
}]Examples#
Reported:
function onClose() {}
class Logger {
flush() {}
}Not reported:
function onClose() {
// Nothing to clean up.
}
class Logger {
constructor(private prefix: string) {}
flush() {
console.log(this.prefix);
}
}Options#
| Option | Default | What it does |
|---|---|---|
allow | [] | The kinds of functions that may be empty. |
In allow | What may be empty |
|---|---|
"functions" | function save() {} and function () {} |
"arrowFunctions" | () => {} and async () => {} |
"generatorFunctions" | function* items() {} |
"asyncFunctions" | async function load() {} |
"methods" | flush() {} in a class or an object |
"generatorMethods" | *items() {} |
"asyncMethods" | async load() {} |
"getters" | get size() {} |
"setters" | set size(value) {} |
"constructors" | Every constructor |
"private-constructors" | private constructor() {} |
"protected-constructors" | protected constructor() {} |
"decoratedFunctions" | Methods that have a decorator |
"overrideMethods" | Methods marked override |
An empty arrow function is a common way to write "do nothing":
const noop = () => {};
const wait = async () => {};A private constructor stops other code from calling new:
class Registry {
static shared = new Registry();
private constructor() {}
}An override with an empty body turns off what the base class does:
class Widget {
render() {
console.log("widget");
}
}
class Spacer extends Widget {
override render() {}
}