no-empty-function

禁止空函数

空函数会降低可读性,因为读者需要猜测它是否是故意的。所以为空函数写一个清晰的注释是一个好习惯。

function foo() {
    // do nothing.
}

特别是,箭头函数的空块可能会让开发人员感到困惑。它与空对象字面量非常相似。

list.map(() => {});   // This is a block, would return undefined.
list.map(() => ({})); // This is an empty object.

规则详情

该规则旨在消除空函数。如果函数包含注释,则不会将其视为问题。

此规则的错误代码示例:

/*eslint no-empty-function: "error"*/
/*eslint-env es6*/

function foo() {}

var foo = function() {};

var foo = () => {};

function* foo() {}

var foo = 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) {}
}

DzO9VpSFHJiTjGCgt1KSooNhAIn1o64th84QTOygwWSy7fBgj4TGxtI1vaW0J9Ta

/*eslint no-empty-function: "error"*/
/*eslint-env es6*/

function foo() {
    // do nothing.
}

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

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

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

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

选项

huASreMfncmlWoBkKTx2F2PcDngNBhMNkIdGSXij+KN5dbQKtKq+CsrhEdXW94Q3eDAO3bn8J7NjuG5SdBSBCg==

    Ke0/CgAdYa2vLeXN6RO4yHPlF4zZMEXHA4oipZY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efP4Fk4xDlnSk=

allow: functions

TMxSmpJeUWnJEmQJ68SlsYV6oRuVhgwA9vqbogKYzZV7ewMrVfJvARO79LQMqL5SQANClwCg50qdprly1aPtmoK7sPoMFoRfY/BhRiH222M=

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

function foo() {}

var foo = function() {};

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

allow: arrowFunctions

TMxSmpJeUWnJEmQJ68SlsdEySzIRNfl4M8vgU01bHxZjYyHwsDp24U2bKxIy/NoUh8EbTEwMqE4/PO6Eb/Bsz0yDYIXG49BGqzWzrrTAfyc=

/*eslint no-empty-function: ["error", { "allow": ["arrowFunctions"] }]*/
/*eslint-env es6*/

var foo = () => {};

allow: generatorFunctions

TMxSmpJeUWnJEmQJ68SlsRphfG7DXAKNeEOy/AETkfkTWxh4EgkK1elvV9l1t1S32WbHbZbpOlogFAdZKVHzC0+oVimVGMUxmnaQGvIa/uc=

/*eslint no-empty-function: ["error", { "allow": ["generatorFunctions"] }]*/
/*eslint-env es6*/

function* foo() {}

var foo = function*() {};

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

allow: methods

TMxSmpJeUWnJEmQJ68SlsbDDH14CuUypRsSPv/oinNy0UFeJz84ExTZ0bStuqPaiErezuKajoqgnYchYc2/HgbqBeS81t/Z/ZMtbfrUpX64=

/*eslint no-empty-function: ["error", { "allow": ["methods"] }]*/
/*eslint-env es6*/

var obj = {
    foo() {}
};

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

allow: generatorMethods

TMxSmpJeUWnJEmQJ68SlsRXPpfE1r4FcP86yU19MHVXKVJp/v43o0dxfmGhB2GDyg0QZd4yz75nyWfj07VNMMZDZYjeoXJGQANfo6OSH1PU=

/*eslint no-empty-function: ["error", { "allow": ["generatorMethods"] }]*/
/*eslint-env es6*/

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

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

allow: getters

TMxSmpJeUWnJEmQJ68SlsaNfMD9JGzi0D8zxYVJI6U1wyg5HFI0sx7AW6DmD8F7bDM9LfR3m6feMzTsBZSl/8Pvwp+bbAMYQms0pEpLRceA=

/*eslint no-empty-function: ["error", { "allow": ["getters"] }]*/
/*eslint-env es6*/

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

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

allow: setters

TMxSmpJeUWnJEmQJ68SlsX0SvzeofbJlzoDmfg9ZISh6Pu3wOrgOACWTVWKHgjeGjw2dw40P5uN4AClAEpiM782xIwphL6mFDT4dK/yiWjY=

/*eslint no-empty-function: ["error", { "allow": ["setters"] }]*/
/*eslint-env es6*/

var obj = {
    set foo(value) {}
};

class A {
    set foo(value) {}
    static set foo(value) {}
}

allow: constructors

TMxSmpJeUWnJEmQJ68SlsTLeDr27g7L+MTAU4VD/8GFpKEMC7wps3sabWee4ZhUjK0Va4WN1QUwYi+UtUj3++J//zG929rMTci1o/qg277Y=

/*eslint no-empty-function: ["error", { "allow": ["constructors"] }]*/
/*eslint-env es6*/

class A {
    constructor() {}
}

allow: asyncFunctions

TMxSmpJeUWnJEmQJ68SlsTscmU9yoCsFEuxYKS/0IzkfOD0yLBcpxKum/B3jQ4aMIa6bCQe7n7av5+5vx2Jac+ivk246/B/boW0uI5Onr5Q=

/*eslint no-empty-function: ["error", { "allow": ["asyncFunctions"] }]*/
/*eslint-env es2017*/

async function a(){}

allow: asyncMethods

TMxSmpJeUWnJEmQJ68Slse3qlSmeb9Q575ckfXmgFAHNV3U1QnJQFN7ZlysXqnIWCXqYKIZbbCa4lPzOR+1AdLZVbonPvkwvGOlK1jnicYU=

/*eslint no-empty-function: ["error", { "allow": ["asyncMethods"] }]*/
/*eslint-env es2017*/

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

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

何时不使用

7I3t+ntWvdCkAFy4U1wYe9dse3mtNtkmaH0CaHgYou0+uV8heuYFMrNnMeXz/vDatUdn/4LKUiXciuQe5Nm1/93trnr/UFMzyMbc9x/JRo8w/ujJC81ARnhben6NLeP+