no-empty-function
不允许空函数
此规则报告的一些问题可通过编辑器 建议 手动修复
空函数可能会降低可读性,因为读者需要猜测这是否是故意的。因此,为空函数写一个清楚的注释是一种好习惯。
🌐 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.
function foo() {
// do nothing.
}
尤其是,空的箭头函数块可能会让开发者感到困惑。它与空的对象字面量非常相似。
🌐 Especially, the empty block of arrow functions might be confusing developers. It’s very similar to an empty object literal.
list.map(() => {}); // This is a block, would return undefined.
list.map(() => ({})); // This is an empty object.
规则详情
🌐 Rule Details
此规则旨在消除空函数。如果函数中包含注释,则不视为问题。
🌐 This rule is aimed at eliminating empty functions. A function will not be considered a problem if it contains a comment.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint no-empty-function: "error"*/
function foo() {}
const bar = function() {};
const bar1 = () => {};
function* baz() {}
const bar2 = function*() {};
const obj = {
foo: function() {},
foo: function*() {},
foo() {},
*foo() {},
get foo() {},
set foo(value) {}
};
class A {
constructor() {}
foo() {}
*foo() {}
get foo() {}
set foo(value) {}
static foo() {}
static *foo() {}
static get foo() {}
static set foo(value) {}
}
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint no-empty-function: "error"*/
function foo() {
// do nothing.
}
const baz = function() {
// any clear comments.
};
const baz1 = () => {
bar();
};
function* foobar() {
// do nothing.
}
const baz2 = function*() {
// do nothing.
};
const obj = {
foo: function() {
// do nothing.
},
foo: function*() {
// do nothing.
},
foo() {
// do nothing.
},
*foo() {
// do nothing.
},
get foo() {
// do nothing.
},
set foo(value) {
// do nothing.
}
};
class A {
constructor() {
// do nothing.
}
foo() {
// do nothing.
}
*foo() {
// do nothing.
}
get foo() {
// do nothing.
}
set foo(value) {
// do nothing.
}
static foo() {
// do nothing.
}
static *foo() {
// do nothing.
}
static get foo() {
// do nothing.
}
static set foo(value) {
// do nothing.
}
}
选项
🌐 Options
此规则具有允许特定类型的函数为空的选项。
🌐 This rule has an option to allow specific kinds of functions to be empty.
allow(string[])- 允许空函数的类型列表。列表项是以下字符串中的一些。默认是一个空数组([])。"functions"- 普通函数"arrowFunctions"- 箭头函数。"generatorFunctions"- 生成器函数。"methods"- 对象字面的类方法和方法简写。"generatorMethods"- 带有生成器的对象字面的类方法和方法简写。"getters"- 获取器。"setters"- 设置器。"constructors"- 类构造函数。"asyncFunctions"- 异步函数。"asyncMethods"- 对象字面的异步类方法和方法简写。"privateConstructors"- 私有类构造函数。(仅限 TypeScript)"protectedConstructors"- 受保护的类构造函数。(仅限 TypeScript)"decoratedFunctions"- 带有装饰器的类方法。(仅限 TypeScript)"overrideMethods"- 使用 override 关键字的方法。(仅限 TypeScript)
允许:功能
🌐 allow: functions
适用于 { "allow": ["functions"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["functions"] } option:
/*eslint no-empty-function: ["error", { "allow": ["functions"] }]*/
function foo() {}
const bar = function() {};
const obj = {
foo: function() {}
};
允许:箭头函数
🌐 allow: arrowFunctions
适用于 { "allow": ["arrowFunctions"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["arrowFunctions"] } option:
/*eslint no-empty-function: ["error", { "allow": ["arrowFunctions"] }]*/
const foo = () => {};
允许:生成器函数
🌐 allow: generatorFunctions
适用于 { "allow": ["generatorFunctions"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["generatorFunctions"] } option:
/*eslint no-empty-function: ["error", { "allow": ["generatorFunctions"] }]*/
function* foo() {}
const bar = function*() {};
const obj = {
foo: function*() {}
};
允许:方法
🌐 allow: methods
适用于 { "allow": ["methods"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["methods"] } option:
/*eslint no-empty-function: ["error", { "allow": ["methods"] }]*/
const obj = {
foo() {}
};
class A {
foo() {}
static foo() {}
}
允许:生成器方法
🌐 allow: generatorMethods
适用于 { "allow": ["generatorMethods"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["generatorMethods"] } option:
/*eslint no-empty-function: ["error", { "allow": ["generatorMethods"] }]*/
const obj = {
*foo() {}
};
class A {
*foo() {}
static *foo() {}
}
允许:获取器
🌐 allow: getters
适用于 { "allow": ["getters"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["getters"] } option:
/*eslint no-empty-function: ["error", { "allow": ["getters"] }]*/
const obj = {
get foo() {}
};
class A {
get foo() {}
static get foo() {}
}
允许:设置器
🌐 allow: setters
适用于 { "allow": ["setters"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["setters"] } option:
/*eslint no-empty-function: ["error", { "allow": ["setters"] }]*/
const obj = {
set foo(value) {}
};
class A {
set foo(value) {}
static set foo(value) {}
}
允许:构造函数
🌐 allow: constructors
适用于 { "allow": ["constructors"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["constructors"] } option:
/*eslint no-empty-function: ["error", { "allow": ["constructors"] }]*/
class A {
constructor() {}
}
允许:异步函数
🌐 allow: asyncFunctions
适用于 { "allow": ["asyncFunctions"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["asyncFunctions"] } options:
/*eslint no-empty-function: ["error", { "allow": ["asyncFunctions"] }]*/
async function a(){}
允许:异步方法
🌐 allow: asyncMethods
适用于 { "allow": ["asyncMethods"] } 选项的正确代码示例:
🌐 Examples of correct code for the { "allow": ["asyncMethods"] } options:
/*eslint no-empty-function: ["error", { "allow": ["asyncMethods"] }]*/
const obj = {
async foo() {}
};
class A {
async foo() {}
static async foo() {}
}
允许:私有构造函数
🌐 allow: privateConstructors
{ “allow”: [“privateConstructors”] } 选项的正确 TypeScript 代码示例:
🌐 Examples of correct TypeScript code for the { "allow": ["privateConstructors"] } option:
/*eslint no-empty-function: ["error", { "allow": ["privateConstructors"] }]*/
class A {
private constructor() {}
}
允许:受保护的构造函数
🌐 allow: protectedConstructors
{ “allow”: [“protectedConstructors”] } 选项的正确 TypeScript 代码示例:
🌐 Examples of correct TypeScript code for the { "allow": ["protectedConstructors"] } option:
/*eslint no-empty-function: ["error", { "allow": ["protectedConstructors"] }]*/
class A {
protected constructor() {}
}
允许:装饰过的函数
🌐 allow: decoratedFunctions
{ “allow”: [“decoratedFunctions”] } 选项的正确 TypeScript 代码示例:
🌐 Examples of correct TypeScript code for the { "allow": ["decoratedFunctions"] } option:
/*eslint no-empty-function: ["error", { "allow": ["decoratedFunctions"] }]*/
class A {
@decorator
foo() {}
}
允许:覆盖方法
🌐 allow: overrideMethods
{ “allow”: [“overrideMethods”] } 选项的正确 TypeScript 代码示例:
🌐 Examples of correct TypeScript code for the { "allow": ["overrideMethods"] } option:
/*eslint no-empty-function: ["error", { "allow": ["overrideMethods"] }]*/
abstract class Base {
abstract method(): void;
}
class Derived extends Base {
override method() {}
}
何时不使用
🌐 When Not To Use It
如果你不想收到有关空函数的通知,那么禁用此规则是安全的。
🌐 If you don’t want to be notified about empty functions, then it’s safe to disable this rule.
相关规则
版本
此规则是在 ESLint v2.0.0 中引入。