Skip to main content

no-empty-function

Disallow empty functions.

🎨

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


此规则扩展了基本 eslint/no-empty-function 规则。 它增加了对处理 TypeScript 特定代码的支持,否则这些代码会触发规则。

英:This rule extends the base eslint/no-empty-function rule. It adds support for handling TypeScript specific code that would otherwise trigger the rule.

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

英: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.

选项

该规则添加了以下选项:

英: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: [],
};

允许:private-constructors

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

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

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

允许:protected-constructors

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

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

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

允许:decoratedFunctions

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

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

@decorator()
function foo() {}

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

允许: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

何时不使用它

如果你正在使用需要函数的外部 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.

如何使用

.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"no-empty-function": "off",
"@typescript-eslint/no-empty-function": "error"
}
};
在线运行试试这个规则 ↗

选项

参见 eslint/no-empty-function 选项

资源

摘自 ❤️ ESLint 内核