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