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() {}

var bar = function() {};

var bar = () => {};

function* baz() {}

var bar = function*() {};

var 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.
}

var baz = function() {
    // any clear comments.
};

var baz = () => {
    bar();
};

function* foobar() {
    // do nothing.
}

var baz = function*() {
    // do nothing.
};

var 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[]) - 允许空函数的类型列表。列表项是以下一些字符串。默认为空数组 ([])。

    ¥allow (string[]) - A list of kind to allow empty functions. List items are some of the following strings. An empty array ([]) by default.

    • "functions" - 普通函数

      ¥"functions" - Normal functions.

    • "arrowFunctions" - 箭头函数。

      ¥"arrowFunctions" - Arrow functions.

    • "generatorFunctions" - 生成器函数。

      ¥"generatorFunctions" - Generator functions.

    • "methods" - 对象字面的类方法和方法简写。

      ¥"methods" - Class methods and method shorthands of object literals.

    • "generatorMethods" - 带有生成器的对象字面的类方法和方法简写。

      ¥"generatorMethods" - Class methods and method shorthands of object literals with generator.

    • "getters" - 获取器。

      ¥"getters" - Getters.

    • "setters" - 设置器。

      ¥"setters" - Setters.

    • "constructors" - 类构造函数。

      ¥"constructors" - Class constructors.

    • "asyncFunctions" - 异步函数。

      ¥"asyncFunctions" - Async functions.

    • "asyncMethods" - 对象字面的异步类方法和方法简写。

      ¥"asyncMethods" - Async class methods and method shorthands of object literals.

allow:functions

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

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

在线运行
/*eslint no-empty-function: ["error", { "allow": ["functions"] }]*/

function foo() {}

var bar = function() {};

var obj = {
    foo: function() {}
};

allow:arrowFunctions

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

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

在线运行
/*eslint no-empty-function: ["error", { "allow": ["arrowFunctions"] }]*/

var foo = () => {};

allow:generatorFunctions

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

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

在线运行
/*eslint no-empty-function: ["error", { "allow": ["generatorFunctions"] }]*/

function* foo() {}

var bar = function*() {};

var obj = {
    foo: function*() {}
};

allow:methods

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

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

在线运行
/*eslint no-empty-function: ["error", { "allow": ["methods"] }]*/

var 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"] }]*/

var 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"] }]*/

var 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"] }]*/

var 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"] }]*/

var obj = {
    async foo() {}
};

class A {
    async foo() {}
    static async foo() {}
}

何时不使用

¥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 中引入。

资源