]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/func-name-matching.md
import 8.3.0 source
[pve-eslint.git] / eslint / docs / rules / func-name-matching.md
CommitLineData
eb39fafa
DC
1# require function names to match the name of the variable or property to which they are assigned (func-name-matching)
2
3## Rule Details
4
5This 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).
6
7Examples of **incorrect** code for this rule:
8
9```js
10/*eslint func-name-matching: "error"*/
11
12var foo = function bar() {};
13foo = function bar() {};
14obj.foo = function bar() {};
15obj['foo'] = function bar() {};
16var obj = {foo: function bar() {}};
17({['foo']: function bar() {}});
609c276f
TL
18
19class C {
20 foo = function bar() {};
21}
eb39fafa
DC
22```
23
24```js
25/*eslint func-name-matching: ["error", "never"] */
26
27var foo = function foo() {};
28foo = function foo() {};
29obj.foo = function foo() {};
30obj['foo'] = function foo() {};
31var obj = {foo: function foo() {}};
32({['foo']: function foo() {}});
609c276f
TL
33
34class C {
35 foo = function foo() {};
36}
eb39fafa
DC
37```
38
39Examples of **correct** code for this rule:
40
41```js
42/*eslint func-name-matching: "error"*/
43/*eslint func-name-matching: ["error", "always"]*/ // these are equivalent
44/*eslint-env es6*/
45
46var foo = function foo() {};
47var foo = function() {};
48var foo = () => {};
49foo = function foo() {};
50
51obj.foo = function foo() {};
52obj['foo'] = function foo() {};
53obj['foo//bar'] = function foo() {};
54obj[foo] = function bar() {};
55
56var obj = {foo: function foo() {}};
57var obj = {[foo]: function bar() {}};
58var obj = {'foo//bar': function foo() {}};
59var obj = {foo: function() {}};
60
61obj['x' + 2] = function bar(){};
62var [ bar ] = [ function bar(){} ];
63({[foo]: function bar() {}})
64
609c276f
TL
65class C {
66 foo = function foo() {};
67 baz = function() {};
68}
69
70// private names are ignored
71class D {
72 #foo = function foo() {};
73 #bar = function foo() {};
74 baz() {
75 this.#foo = function foo() {};
76 this.#foo = function bar() {};
77 }
78}
79
eb39fafa
DC
80module.exports = function foo(name) {};
81module['exports'] = function foo(name) {};
82```
83
84```js
85/*eslint func-name-matching: ["error", "never"] */
86/*eslint-env es6*/
87
88var foo = function bar() {};
89var foo = function() {};
90var foo = () => {};
91foo = function bar() {};
92
93obj.foo = function bar() {};
94obj['foo'] = function bar() {};
95obj['foo//bar'] = function foo() {};
96obj[foo] = function foo() {};
97
98var obj = {foo: function bar() {}};
99var obj = {[foo]: function foo() {}};
100var obj = {'foo//bar': function foo() {}};
101var obj = {foo: function() {}};
102
103obj['x' + 2] = function bar(){};
104var [ bar ] = [ function bar(){} ];
105({[foo]: function bar() {}})
106
609c276f
TL
107class C {
108 foo = function bar() {};
109 baz = function() {};
110}
111
112// private names are ignored
113class D {
114 #foo = function foo() {};
115 #bar = function foo() {};
116 baz() {
117 this.#foo = function foo() {};
118 this.#foo = function bar() {};
119 }
120}
121
eb39fafa
DC
122module.exports = function foo(name) {};
123module['exports'] = function foo(name) {};
124```
125
126## Options
127
128This 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`.
129
130### considerPropertyDescriptor
131
132A 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`.
133
134Examples of **correct** code for the `{ considerPropertyDescriptor: true }` option:
135
136```js
137/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
138/*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent
139var obj = {};
140Object.create(obj, {foo:{value: function foo() {}}});
141Object.defineProperty(obj, 'bar', {value: function bar() {}});
142Object.defineProperties(obj, {baz:{value: function baz() {} }});
143Reflect.defineProperty(obj, 'foo', {value: function foo() {}});
144```
145
146Examples of **incorrect** code for the `{ considerPropertyDescriptor: true }` option:
147
148```js
149/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
150/*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent
151var obj = {};
152Object.create(obj, {foo:{value: function bar() {}}});
153Object.defineProperty(obj, 'bar', {value: function baz() {}});
154Object.defineProperties(obj, {baz:{value: function foo() {} }});
155Reflect.defineProperty(obj, 'foo', {value: function value() {}});
156```
157
158### includeCommonJSModuleExports
159
160A boolean value that defaults to `false`. If `includeCommonJSModuleExports` is set to true, `module.exports` and `module["exports"]` will be checked by this rule.
161
162Examples of **incorrect** code for the `{ includeCommonJSModuleExports: true }` option:
163
164```js
165/*eslint func-name-matching: ["error", { "includeCommonJSModuleExports": true }]*/
166/*eslint func-name-matching: ["error", "always", { "includeCommonJSModuleExports": true }]*/ // these are equivalent
167
168module.exports = function foo(name) {};
169module['exports'] = function foo(name) {};
170```
171
172## When Not To Use It
173
174Do 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.
175
176## Compatibility
177
178* **JSCS**: [requireMatchingFunctionName](https://jscs-dev.github.io/rule/requireMatchingFunctionName)