func-name-matching
需要函数名称来匹配它们被赋值的变量或属性的名称( func-name-matching )
规则细节
此规则要求函数名称与它们所分配的变量或属性的名称相匹配。该规则将忽略属性分配,其中属性名称是在配置中指定的 ECMAScript 版本(默认 ES5)中不是有效标识符的文字。
选项
该规则采用可选字符串 “always” 或 “never”(省略时默认为“always”),以及带有一个键includeCommonJSModuleExports
,和一个布尔值的可选选项对象。此选项默认为false
,这意味着module.exports
与module["exports"]
此规则将被忽略。如果includeCommonJSModuleExports
设置为 true ,module.exports
并且module["exports"]
将按照此规则进行检查。
此规则的错误
代码示例:
/*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() {}}
/*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) {};
/*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() {}}
此规则的正确
代码示例:
/*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() {}})
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() {}})
module.exports = function foo(name) {};
module['exports'] = function foo(name) {};
何时不使用它
如果要允许命名函数与它们分配的变量或属性具有不同的名称,请不要使用此规则。
兼容性
JSCS
: requireMatchingFunctionName VersionThis rule was introduced in ESLint 3.8.0.Resources