func-name-matching
要求函数名称与分配给它们的变量或属性的名称相匹配
规则详情
¥Rule Details
此规则要求函数名称与分配给它们的变量或属性的名称相匹配。该规则将忽略属性名称是在配置中指定的 ECMAScript 版本(默认 ES5)中不是有效标识符的字面的属性分配。
¥This rule requires function names to match the name of the variable or property to which they are assigned. The rule will ignore property assignments where the property name is a literal that is not a valid identifier in the ECMAScript version specified in your configuration (default ES5).
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*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() {};
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*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) {};
选项
¥Options
此规则接受一个可选字符串 "always"
或 "never"
(省略时,默认为 "always"
)和一个可选选项对象,该对象具有两个属性 considerPropertyDescriptor
和 includeCommonJSModuleExports
。
¥This rule takes an optional string of "always"
or "never"
(when omitted, it defaults to "always"
), and an optional options object with two properties considerPropertyDescriptor
and includeCommonJSModuleExports
.
considerPropertyDescriptor
默认为 false
的布尔值。如果 considerPropertyDescriptor
设置为 true,则检查将考虑 Object.create
、Object.defineProperty
、Object.defineProperties
和 Reflect.defineProperty
的使用。
¥A boolean value that defaults to false
. If considerPropertyDescriptor
is set to true, the check will take into account the use of Object.create
, Object.defineProperty
, Object.defineProperties
, and Reflect.defineProperty
.
{ considerPropertyDescriptor: true }
选项的正确代码示例:
¥Examples of correct code for the { considerPropertyDescriptor: true }
option:
/*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 }
选项的错误代码示例:
¥Examples of incorrect code for the { considerPropertyDescriptor: true }
option:
/*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.exports
和 module["exports"]
。
¥A boolean value that defaults to false
. If includeCommonJSModuleExports
is set to true, module.exports
and module["exports"]
will be checked by this rule.
{ includeCommonJSModuleExports: true }
选项的错误代码示例:
¥Examples of incorrect code for the { includeCommonJSModuleExports: true }
option:
/*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) {};
何时不使用
¥When Not To Use It
如果你希望命名函数与分配给它们的变量或属性具有不同的名称,请不要使用此规则。
¥Do not use this rule if you want to allow named functions to have different names from the variable or property to which they are assigned.
兼容性
¥Compatibility
版本
此规则是在 ESLint v3.8.0 中引入。