eslint/no-empty-function Restriction
What it does
Disallows the usage of empty functions.
Why is this bad?
Empty functions can reduce readability because readers need to guess whether it's intentional or not. So writing a clear comment for empty functions is a good practice.
Examples
Examples of incorrect code for this rule:
function foo() {}
const bar = () => {};
class Foo {
constructor();
someMethod() {}
set bar(value) {}
}Examples of correct code for this rule:
function foo() {
// do nothing
}
function foo() {
return;
}
const add = (a, b) => a + b;
class Foo {
// constructor body is empty, but it declares a private property named
// `_name`
constructor(private _name: string) {}
public get name() {
return this._name;
}
}Configuration
This rule accepts a configuration object with the following properties:
allow
type: array
Types of functions that are allowed to be empty.
By default, no function kinds are allowed to be empty, but this option can be used to permit specific kinds of functions.
Example:
{
"no-empty-function": ["error", { "allow": ["constructors"] }]
}allow[n]
type: "functions" | "arrowFunctions" | "generatorFunctions" | "methods" | "generatorMethods" | "getters" | "setters" | "constructors" | "asyncFunctions" | "asyncMethods" | "privateConstructors" | "protectedConstructors" | "decoratedFunctions" | "overrideMethods"
Kinds of functions that can be allowed to be empty.
"functions"
Allow empty regular functions.
function foo() {}"arrowFunctions"
Allow empty arrow functions.
const foo = () => {};"generatorFunctions"
Allow empty generator functions.
function* foo() {}"methods"
Allow empty methods.
class Foo {
bar() {}
}"generatorMethods"
Allow empty generator methods.
class Foo {
*bar() {}
}"getters"
Allow empty getters.
class Foo {
get bar() {}
}"setters"
Allow empty setters.
class Foo {
set bar(value) {}
}"constructors"
Allow empty constructors.
class Foo {
constructor() {}
}"asyncFunctions"
Allow empty async functions.
async function foo() {}"asyncMethods"
Allow empty async methods.
class Foo {
async bar() {}
}"privateConstructors"
Allow empty private constructors.
class Foo {
private constructor() {}
}"protectedConstructors"
Allow empty protected constructors.
class Foo {
protected constructor() {}
}"decoratedFunctions"
Allow empty decorated functions.
class Foo {
@decorator()
bar() {}
}"overrideMethods"
Allow empty override methods.
class Foo extends Base {
override bar() {}
}How to use
To enable this rule using the config file or in the CLI, you can use:
{
"rules": {
"no-empty-function": "error"
}
}oxlint --deny no-empty-function