]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/func-name-matching.md
import 8.3.0 source
[pve-eslint.git] / eslint / docs / rules / func-name-matching.md
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
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).
6
7 Examples of **incorrect** code for this rule:
8
9 ```js
10 /*eslint func-name-matching: "error"*/
11
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() {}});
18
19 class C {
20 foo = function bar() {};
21 }
22 ```
23
24 ```js
25 /*eslint func-name-matching: ["error", "never"] */
26
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() {}});
33
34 class C {
35 foo = function foo() {};
36 }
37 ```
38
39 Examples 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
46 var foo = function foo() {};
47 var foo = function() {};
48 var foo = () => {};
49 foo = function foo() {};
50
51 obj.foo = function foo() {};
52 obj['foo'] = function foo() {};
53 obj['foo//bar'] = function foo() {};
54 obj[foo] = function bar() {};
55
56 var obj = {foo: function foo() {}};
57 var obj = {[foo]: function bar() {}};
58 var obj = {'foo//bar': function foo() {}};
59 var obj = {foo: function() {}};
60
61 obj['x' + 2] = function bar(){};
62 var [ bar ] = [ function bar(){} ];
63 ({[foo]: function bar() {}})
64
65 class C {
66 foo = function foo() {};
67 baz = function() {};
68 }
69
70 // private names are ignored
71 class D {
72 #foo = function foo() {};
73 #bar = function foo() {};
74 baz() {
75 this.#foo = function foo() {};
76 this.#foo = function bar() {};
77 }
78 }
79
80 module.exports = function foo(name) {};
81 module['exports'] = function foo(name) {};
82 ```
83
84 ```js
85 /*eslint func-name-matching: ["error", "never"] */
86 /*eslint-env es6*/
87
88 var foo = function bar() {};
89 var foo = function() {};
90 var foo = () => {};
91 foo = function bar() {};
92
93 obj.foo = function bar() {};
94 obj['foo'] = function bar() {};
95 obj['foo//bar'] = function foo() {};
96 obj[foo] = function foo() {};
97
98 var obj = {foo: function bar() {}};
99 var obj = {[foo]: function foo() {}};
100 var obj = {'foo//bar': function foo() {}};
101 var obj = {foo: function() {}};
102
103 obj['x' + 2] = function bar(){};
104 var [ bar ] = [ function bar(){} ];
105 ({[foo]: function bar() {}})
106
107 class C {
108 foo = function bar() {};
109 baz = function() {};
110 }
111
112 // private names are ignored
113 class D {
114 #foo = function foo() {};
115 #bar = function foo() {};
116 baz() {
117 this.#foo = function foo() {};
118 this.#foo = function bar() {};
119 }
120 }
121
122 module.exports = function foo(name) {};
123 module['exports'] = function foo(name) {};
124 ```
125
126 ## Options
127
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`.
129
130 ### considerPropertyDescriptor
131
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`.
133
134 Examples 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
139 var obj = {};
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() {}});
144 ```
145
146 Examples 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
151 var obj = {};
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() {}});
156 ```
157
158 ### includeCommonJSModuleExports
159
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.
161
162 Examples 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
168 module.exports = function foo(name) {};
169 module['exports'] = function foo(name) {};
170 ```
171
172 ## When Not To Use It
173
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.
175
176 ## Compatibility
177
178 * **JSCS**: [requireMatchingFunctionName](https://jscs-dev.github.io/rule/requireMatchingFunctionName)