func-name-matching

要求函数名称与分配给它们的变量或属性的名称相匹配

规则详情

此规则要求函数名称与分配给它们的变量或属性的名称相匹配。该规则将忽略属性名称是在配置中指定的 ECMAScript 版本(默认 ES5)中不是有效标识符的字面的属性分配。

此规则的错误代码示例:

/*eslint func-name-matching: "error"*/

var foo = function bar() {};
foo = function bar() {};
obj.foo = function bar() {};
obj['foo'] = function bar() {};
var obj = {foo: function bar() {}};
({['foo']: function bar() {}});

class C {
    foo = function bar() {};
}
/*eslint func-name-matching: ["error", "never"] */

var foo = function foo() {};
foo = function foo() {};
obj.foo = function foo() {};
obj['foo'] = function foo() {};
var obj = {foo: function foo() {}};
({['foo']: function foo() {}});

class C {
    foo = function foo() {};
}

此规则的正确代码示例:

/*eslint func-name-matching: "error"*/
/*eslint func-name-matching: ["error", "always"]*/ // these are equivalent
/*eslint-env es6*/

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

obj.foo = function foo() {};
obj['foo'] = function foo() {};
obj['foo//bar'] = function foo() {};
obj[foo] = function bar() {};

var obj = {foo: function foo() {}};
var obj = {[foo]: function bar() {}};
var obj = {'foo//bar': function foo() {}};
var obj = {foo: function() {}};

obj['x' + 2] = function bar(){};
var [ bar ] = [ function bar(){} ];
({[foo]: function bar() {}})

class C {
    foo = function foo() {};
    baz = function() {};
}

// private names are ignored
class D {
    #foo = function foo() {};
    #bar = function foo() {};
    baz() {
        this.#foo = function foo() {};
        this.#foo = function bar() {};
    }
}

module.exports = function foo(name) {};
module['exports'] = function foo(name) {};
/*eslint func-name-matching: ["error", "never"] */
/*eslint-env es6*/

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

obj.foo = function bar() {};
obj['foo'] = function bar() {};
obj['foo//bar'] = function foo() {};
obj[foo] = function foo() {};

var obj = {foo: function bar() {}};
var obj = {[foo]: function foo() {}};
var obj = {'foo//bar': function foo() {}};
var obj = {foo: function() {}};

obj['x' + 2] = function bar(){};
var [ bar ] = [ function bar(){} ];
({[foo]: function bar() {}})

class C {
    foo = function bar() {};
    baz = function() {};
}

// private names are ignored
class D {
    #foo = function foo() {};
    #bar = function foo() {};
    baz() {
        this.#foo = function foo() {};
        this.#foo = function bar() {};
    }
}

module.exports = function foo(name) {};
module['exports'] = function foo(name) {};

选项

此规则接受一个可选字符串 "always" 或 "never"(省略时,默认为 "always")和一个可选选项对象,该对象具有两个属性 considerPropertyDescriptorincludeCommonJSModuleExports

considerPropertyDescriptor

EYAGts5BGHuk5gPFyZFcxXzjaxZuVyqbSt7cFbSd/VioYeFNlq1gHuPYo1D2jCRXHW46POg5zTo0D2bCwVli5YHJcjYSfce9a40JIQ58qIi5cGMZzI+tHHeFVtVO0zTJXLJbn1eP8aFy/5NJ/iGeXR9LBJO5DxZOBetrRWqGy0HutupvTTsYTK5pD7h/9SVShbwzLYGSuHK1QJq4eL3MjKcUcYyzQ6I16+HAQq0DzmUCOBPq+hYAGupgx2Q0Zmcu6eYdaAFsHrlhero2H2iF8cAbRFsEDA9Y09cx8rLL8UHL9JJZFg1Qenu4iS6xuzDZcZYYJ9oyM6647CmJIxFpnR31tcFdGpr9vSWFPy9EUGpywe2gKtCecdawJM2xqVLi

lx9kLxQveMHf09UnSz3zXZhVoTjcEdSVB+td5d5qkSoraCWr5f4LrxaJrxbVTkq6gWru6n/8bnFCyBIgcaKJ53kmwx4NCXILsM2eYmLwKZy6/9GoAE4AQb4FpK08xy1t

/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
/*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent
var obj = {};
Object.create(obj, {foo:{value: function foo() {}}});
Object.defineProperty(obj, 'bar', {value: function bar() {}});
Object.defineProperties(obj, {baz:{value: function baz() {} }});
Reflect.defineProperty(obj, 'foo', {value: function foo() {}});

lx9kLxQveMHf09UnSz3zXZhVoTjcEdSVB+td5d5qkSoraCWr5f4LrxaJrxbVTkq62QVdkcA0VSTwACnlyxL7ECFWFWKkegSLQtI71naAczyYwPrnwsgVEI5M/5qHj9/J

/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
/*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent
var obj = {};
Object.create(obj, {foo:{value: function bar() {}}});
Object.defineProperty(obj, 'bar', {value: function baz() {}});
Object.defineProperties(obj, {baz:{value: function foo() {} }});
Reflect.defineProperty(obj, 'foo', {value: function value() {}});

includeCommonJSModuleExports

EYAGts5BGHuk5gPFyZFcxXzjaxZuVyqbSt7cFbSd/VioYeFNlq1gHuPYo1D2jCRXp0ZW8KWcyzyslsy7qvVSUSwFetQHs9JXpzqNHAjbADHZTUIHbXDf+4Vy4GLkKEy/d8Jc83K7W41A3h1L/4wP/9ry3/IZWRqUV+bwLR0Y20J7csHK1poyb9BoJimhESnH04uR/gZdEgycY5NC9wprY9iHiDmGE4G4IF4M5rS4cVARsGqMyyRSXYfpxMTXxL1mTBqOZFG+7jwG64b/Ei7bqg==

xsNONTBK/8jgffN4QNTCUl6U0QytMZoBSP+N1Yyu4v3HeDjI+Pjo1gm/llT2rin4yVrgQwtz8ALZPC7cLFTRP6z0On5ooBNdBZdS997pHhzYXOKWmdeYnZQcUb21uvSf

/*eslint func-name-matching: ["error", { "includeCommonJSModuleExports": true }]*/
/*eslint func-name-matching: ["error", "always", { "includeCommonJSModuleExports": true }]*/ // these are equivalent

module.exports = function foo(name) {};
module['exports'] = function foo(name) {};

何时不使用

N4vmgLhc1ZV/Md8JFy8PdAiyeMiTO+8FwUaPmOq5VbeOluZqjtUAwZfk30IqFJbzlZ8TrXqE9PYd1n4NzShe7FNQmowrh6yQXta5KGMZs5VzZsmnN7u+eA9t0+joVz2IQOvIT5oVdFhDsoVgciCLwo8e34xUkAE1yJQfz4tBwfA=

兼容性

    X4iACkt+wLVCLgSMHE/ZxeyfEwDgXr0IUSlx72v3J3nZT1s17vNNCDIqCv9VKhaSL3QvXlVPq20R6Z59jY2g6a/34D/jlJ/zbTjQg7wD3KqZhPMi1e64AMZKAzfYScoFG6gcZL/jI9e2EsLaaSpf2w==