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