版本

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"*/
// equivalent to /*eslint func-name-matching: ["error", "always"]*/

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

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

一个布尔值,默认为 false。如果 considerPropertyDescriptor 设置为 true,则检查将考虑 Object.createObject.definePropertyObject.definePropertiesReflect.defineProperty 的使用。

{ considerPropertyDescriptor: true } 选项的正确代码示例

在在线运行中打开
/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
// equivalent to /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/
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() {}});

{ considerPropertyDescriptor: true } 选项的错误代码示例

在在线运行中打开
/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
// equivalent to /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/
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

一个布尔值,默认为 false。如果 includeCommonJSModuleExports 设置为 true,则此规则将检查 module.exportsmodule["exports"]

{ includeCommonJSModuleExports: true } 选项的错误代码示例

在在线运行中打开
/*eslint func-name-matching: ["error", { "includeCommonJSModuleExports": true }]*/
// equivalent to /*eslint func-name-matching: ["error", "always", { "includeCommonJSModuleExports": true }]*/

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

何时不使用它

如果您希望允许命名函数具有与分配给它们的变量或属性不同的名称,请不要使用此规则。

兼容性

版本

此规则在 ESLint v3.8.0 中引入。

资源

更改语言