]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/id-match.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / id-match.md
1 ---
2 title: id-match
3 rule_type: suggestion
4 ---
5
6
7 > "There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton
8
9 Naming things consistently in a project is an often underestimated aspect of code creation.
10 When done correctly, it can save your team hours of unnecessary head scratching and misdirections.
11 This rule allows you to precisely define and enforce the variables and function names on your team should use.
12 No more limiting yourself to camelCase, snake_case, PascalCase or oHungarianNotation. Id-match has all your needs covered!
13
14 ## Rule Details
15
16 This rule requires identifiers in assignments and `function` definitions to match a specified regular expression.
17
18 ## Options
19
20 This rule has a string option for the specified regular expression.
21
22 For example, to enforce a camelcase naming convention:
23
24 ```json
25 {
26 "id-match": ["error", "^[a-z]+([A-Z][a-z]+)*$"]
27 }
28 ```
29
30 Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$"` option:
31
32 ::: incorrect
33
34 ```js
35 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/
36
37 var my_favorite_color = "#112C85";
38 var _myFavoriteColor = "#112C85";
39 var myFavoriteColor_ = "#112C85";
40 var MY_FAVORITE_COLOR = "#112C85";
41 function do_something() {
42 // ...
43 }
44
45 obj.do_something = function() {
46 // ...
47 };
48
49 class My_Class {}
50
51 class myClass {
52 do_something() {}
53 }
54
55 class myClass {
56 #do_something() {}
57 }
58 ```
59
60 :::
61
62 Examples of **correct** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$"` option:
63
64 ::: correct
65
66 ```js
67 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/
68
69 var myFavoriteColor = "#112C85";
70 var foo = bar.baz_boom;
71 var foo = { qux: bar.baz_boom };
72 do_something();
73 var obj = {
74 my_pref: 1
75 };
76
77 class myClass {}
78
79 class myClass {
80 doSomething() {}
81 }
82
83 class myClass {
84 #doSomething() {}
85 }
86 ```
87
88 :::
89
90 This rule has an object option:
91
92 * `"properties": false` (default) does not check object properties
93 * `"properties": true` requires object literal properties and member expression assignment properties to match the specified regular expression
94 * `"classFields": false` (default) does not check class field names
95 * `"classFields": true` requires class field names to match the specified regular expression
96 * `"onlyDeclarations": false` (default) requires all variable names to match the specified regular expression
97 * `"onlyDeclarations": true` requires only `var`, `const`, `let`, `function`, and `class` declarations to match the specified regular expression
98 * `"ignoreDestructuring": false` (default) enforces `id-match` for destructured identifiers
99 * `"ignoreDestructuring": true` does not check destructured identifiers
100
101 ### properties
102
103 Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "properties": true }` options:
104
105 ::: incorrect
106
107 ```js
108 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }]*/
109
110 var obj = {
111 my_pref: 1
112 };
113 ```
114
115 :::
116
117 ### classFields
118
119 Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "classFields": true }` options:
120
121 ::: incorrect
122
123 ```js
124 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }]*/
125
126 class myClass {
127 my_pref = 1;
128 }
129
130 class myClass {
131 #my_pref = 1;
132 }
133 ```
134
135 :::
136
137 ### onlyDeclarations
138
139 Examples of **correct** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }` options:
140
141 ::: correct
142
143 ```js
144 /*eslint id-match: [2, "^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }]*/
145
146 do_something(__dirname);
147 ```
148
149 :::
150
151 ### ignoreDestructuring: false
152
153 Examples of **incorrect** code for this rule with the default `"^[^_]+$", { "ignoreDestructuring": false }` option:
154
155 ::: incorrect
156
157 ```js
158 /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": false }]*/
159
160 var { category_id } = query;
161
162 var { category_id = 1 } = query;
163
164 var { category_id: category_id } = query;
165
166 var { category_id: category_alias } = query;
167
168 var { category_id: categoryId, ...other_props } = query;
169 ```
170
171 :::
172
173 ### ignoreDestructuring: true
174
175 Examples of **incorrect** code for this rule with the `"^[^_]+$", { "ignoreDestructuring": true }` option:
176
177 ::: incorrect
178
179 ```js
180 /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/
181
182 var { category_id: category_alias } = query;
183
184 var { category_id, ...other_props } = query;
185 ```
186
187 :::
188
189 Examples of **correct** code for this rule with the `"^[^_]+$", { "ignoreDestructuring": true }` option:
190
191 ::: correct
192
193 ```js
194 /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/
195
196 var { category_id } = query;
197
198 var { category_id = 1 } = query;
199
200 var { category_id: category_id } = query;
201 ```
202
203 :::
204
205 ## When Not To Use It
206
207 If you don't want to enforce any particular naming convention for all identifiers, or your naming convention is too complex to be enforced by configuring this rule, then you should not enable this rule.