1 # require function names to match the name of the variable or property to which they are assigned (func-name-matching)
5 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).
7 Examples of **incorrect** code for this rule:
10 /*eslint func-name-matching: "error"*/
12 var foo = function bar() {};
13 foo = function bar() {};
14 obj.foo = function bar() {};
15 obj['foo'] = function bar() {};
16 var obj = {foo: function bar() {}};
17 ({['foo']: function bar() {}});
20 foo = function bar() {};
25 /*eslint func-name-matching: ["error", "never"] */
27 var foo = function foo() {};
28 foo = function foo() {};
29 obj.foo = function foo() {};
30 obj['foo'] = function foo() {};
31 var obj = {foo: function foo() {}};
32 ({['foo']: function foo() {}});
35 foo = function foo() {};
39 Examples of **correct** code for this rule:
42 /*eslint func-name-matching: "error"*/
43 /*eslint func-name-matching: ["error", "always"]*/ // these are equivalent
46 var foo = function foo() {};
47 var foo = function() {};
49 foo = function foo() {};
51 obj.foo = function foo() {};
52 obj['foo'] = function foo() {};
53 obj['foo//bar'] = function foo() {};
54 obj[foo] = function bar() {};
56 var obj = {foo: function foo() {}};
57 var obj = {[foo]: function bar() {}};
58 var obj = {'foo//bar': function foo() {}};
59 var obj = {foo: function() {}};
61 obj['x' + 2] = function bar(){};
62 var [ bar ] = [ function bar(){} ];
63 ({[foo]: function bar() {}})
66 foo = function foo() {};
70 // private names are ignored
72 #foo = function foo() {};
73 #bar = function foo() {};
75 this.#foo = function foo() {};
76 this.#foo = function bar() {};
80 module.exports = function foo(name) {};
81 module['exports'] = function foo(name) {};
85 /*eslint func-name-matching: ["error", "never"] */
88 var foo = function bar() {};
89 var foo = function() {};
91 foo = function bar() {};
93 obj.foo = function bar() {};
94 obj['foo'] = function bar() {};
95 obj['foo//bar'] = function foo() {};
96 obj[foo] = function foo() {};
98 var obj = {foo: function bar() {}};
99 var obj = {[foo]: function foo() {}};
100 var obj = {'foo//bar': function foo() {}};
101 var obj = {foo: function() {}};
103 obj['x' + 2] = function bar(){};
104 var [ bar ] = [ function bar(){} ];
105 ({[foo]: function bar() {}})
108 foo = function bar() {};
112 // private names are ignored
114 #foo = function foo() {};
115 #bar = function foo() {};
117 this.#foo = function foo() {};
118 this.#foo = function bar() {};
122 module.exports = function foo(name) {};
123 module['exports'] = function foo(name) {};
128 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`.
130 ### considerPropertyDescriptor
132 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`.
134 Examples of **correct** code for the `{ considerPropertyDescriptor: true }` option:
137 /*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
138 /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent
140 Object.create(obj, {foo:{value: function foo() {}}});
141 Object.defineProperty(obj, 'bar', {value: function bar() {}});
142 Object.defineProperties(obj, {baz:{value: function baz() {} }});
143 Reflect.defineProperty(obj, 'foo', {value: function foo() {}});
146 Examples of **incorrect** code for the `{ considerPropertyDescriptor: true }` option:
149 /*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
150 /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent
152 Object.create(obj, {foo:{value: function bar() {}}});
153 Object.defineProperty(obj, 'bar', {value: function baz() {}});
154 Object.defineProperties(obj, {baz:{value: function foo() {} }});
155 Reflect.defineProperty(obj, 'foo', {value: function value() {}});
158 ### includeCommonJSModuleExports
160 A boolean value that defaults to `false`. If `includeCommonJSModuleExports` is set to true, `module.exports` and `module["exports"]` will be checked by this rule.
162 Examples of **incorrect** code for the `{ includeCommonJSModuleExports: true }` option:
165 /*eslint func-name-matching: ["error", { "includeCommonJSModuleExports": true }]*/
166 /*eslint func-name-matching: ["error", "always", { "includeCommonJSModuleExports": true }]*/ // these are equivalent
168 module.exports = function foo(name) {};
169 module['exports'] = function foo(name) {};
172 ## When Not To Use It
174 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.
178 * **JSCS**: [requireMatchingFunctionName](https://jscs-dev.github.io/rule/requireMatchingFunctionName)