Skip to main content

no-empty-function

Disallow empty functions.

🎨

ESLint 配置 中扩展"plugin:@typescript-eslint/stylistic" 可启用此规则。

🧱

This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.

This rule extends the base no-empty-function rule from ESLint core. 它增加了对处理 TypeScript 特定代码的支持,否则这些代码会触发规则。

¥It adds support for handling TypeScript specific code that would otherwise trigger the rule.

一个有效的 TypeScript 特定代码示例,否则会触发 no-empty-function 规则,即在构造函数中使用 参数属性

¥One example of valid TypeScript specific code that would otherwise trigger the no-empty-function rule is the use of parameter properties in constructor functions.

如何使用

eslint.config.mjs
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"no-empty-function": "off",
"@typescript-eslint/no-empty-function": "error"
}
});

在线运行试试这个规则 ↗

选项

See eslint/no-empty-function's options.

¥Options

该规则添加了以下选项:

¥This rule adds the following options:

type AdditionalAllowOptionEntries =
| 'private-constructors'
| 'protected-constructors'
| 'decoratedFunctions'
| 'overrideMethods';

type AllowOptionEntries =
| BaseNoEmptyFunctionAllowOptionEntries
| AdditionalAllowOptionEntries;

interface Options extends BaseNoEmptyFunctionOptions {
allow?: Array<AllowOptionEntries>;
}
const defaultOptions: Options = {
...baseNoEmptyFunctionDefaultOptions,
allow: [],
};

allow:private-constructors

{ "allow": ["private-constructors"] } 选项的正确代码示例:

¥Examples of correct code for the { "allow": ["private-constructors"] } option:

class Foo {
private constructor() {}
}
Open in Playground

allow:protected-constructors

{ "allow": ["protected-constructors"] } 选项的正确代码示例:

¥Examples of correct code for the { "allow": ["protected-constructors"] } option:

class Foo {
protected constructor() {}
}
Open in Playground

allow:decoratedFunctions

{ "allow": ["decoratedFunctions"] } 选项的正确代码示例:

¥Examples of correct code for the { "allow": ["decoratedFunctions"] } option:

class Foo {
@decorator()
foo() {}
}
Open in Playground

allow:overrideMethods

{ "allow": ["overrideMethods"] } 选项的正确代码示例:

¥Examples of correct code for the { "allow": ["overrideMethods"] } option:

abstract class Base {
protected greet(): void {
console.log('Hello!');
}
}

class Foo extends Base {
protected override greet(): void {}
}
Open in Playground

何时不使用它

¥When Not To Use It

如果你正在使用需要函数的外部 API,即使它们不执行任何操作,那么你可能希望避免此规则。你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

¥If you are working with external APIs that require functions even if they do nothing, then you may want to avoid this rule. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

测试代码也经常违反此规则。如果你的测试设置不支持 "mock" 或 "spy" 函数(例如 jest.fn()sinon.spy()vi.fn()),你可能希望在测试文件中禁用此规则。同样,如果这些情况不是非常常见,你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是在测试文件中完全禁用此规则。

¥Test code often violates this rule as well. If your testing setup doesn't support "mock" or "spy" functions such as jest.fn(), sinon.spy(), or vi.fn(), you may wish to disable this rule in test files. Again, if those cases aren't extremely common, you might consider using ESLint disable comments for those specific situations instead of completely disabling this rule in test files.

'## 资源'

Taken with ❤️ from ESLint core.