]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/id-length.md
fb69db1850fc3d755af9ccc8cfadc2ebee7b00b4
[pve-eslint.git] / eslint / docs / src / rules / id-length.md
1 ---
2 title: id-length
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - max-len
7 - new-cap
8 - func-names
9 - camelcase
10 ---
11
12
13 Very short identifier names like `e`, `x`, `_t` or very long ones like `hashGeneratorResultOutputContainerObject` can make code harder to read and potentially less maintainable. To prevent this, one may enforce a minimum and/or maximum identifier length.
14
15 ```js
16 var x = 5; // too short; difficult to understand its purpose without context
17 ```
18
19 ## Rule Details
20
21 This rule enforces a minimum and/or maximum identifier length convention.
22
23 ## Options
24
25 Examples of **incorrect** code for this rule with the default options:
26
27 ::: incorrect
28
29 ```js
30 /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
31 /*eslint-env es6*/
32
33 var x = 5;
34 obj.e = document.body;
35 var foo = function (e) { };
36 try {
37 dangerousStuff();
38 } catch (e) {
39 // ignore as many do
40 }
41 var myObj = { a: 1 };
42 (a) => { a * a };
43 class x { }
44 class Foo { x() {} }
45 class Foo { #x() {} }
46 class Foo { x = 1 }
47 class Foo { #x = 1 }
48 function foo(...x) { }
49 function foo([x]) { }
50 var [x] = arr;
51 var { prop: [x]} = {};
52 function foo({x}) { }
53 var { x } = {};
54 var { prop: a} = {};
55 ({ prop: obj.x } = {});
56 ```
57
58 :::
59
60 Examples of **correct** code for this rule with the default options:
61
62 ::: correct
63
64 ```js
65 /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
66 /*eslint-env es6*/
67
68 var num = 5;
69 function _f() { return 42; }
70 function _func() { return 42; }
71 obj.el = document.body;
72 var foo = function (evt) { /* do stuff */ };
73 try {
74 dangerousStuff();
75 } catch (error) {
76 // ignore as many do
77 }
78 var myObj = { apple: 1 };
79 (num) => { num * num };
80 function foo(num = 0) { }
81 class MyClass { }
82 class Foo { method() {} }
83 class Foo { #method() {} }
84 class Foo { field = 1 }
85 class Foo { #field = 1 }
86 function foo(...args) { }
87 function foo([longName]) { }
88 var { prop } = {};
89 var { prop: [longName] } = {};
90 var [longName] = arr;
91 function foo({ prop }) { }
92 function foo({ a: prop }) { }
93 var { prop } = {};
94 var { a: prop } = {};
95 ({ prop: obj.longName } = {});
96 var data = { "x": 1 }; // excused because of quotes
97 data["y"] = 3; // excused because of calculated property access
98 ```
99
100 :::
101
102 This rule has an object option:
103
104 * `"min"` (default: 2) enforces a minimum identifier length
105 * `"max"` (default: Infinity) enforces a maximum identifier length
106 * `"properties": always` (default) enforces identifier length convention for property names
107 * `"properties": never` ignores identifier length convention for property names
108 * `"exceptions"` allows an array of specified identifier names
109 * `"exceptionPatterns"` array of strings representing regular expression patterns, allows identifiers that match any of the patterns.
110
111 ### min
112
113 Examples of **incorrect** code for this rule with the `{ "min": 4 }` option:
114
115 ::: incorrect
116
117 ```js
118 /*eslint id-length: ["error", { "min": 4 }]*/
119 /*eslint-env es6*/
120
121 var val = 5;
122 obj.e = document.body;
123 function foo (e) { };
124 try {
125 dangerousStuff();
126 } catch (e) {
127 // ignore as many do
128 }
129 var myObj = { a: 1 };
130 (val) => { val * val };
131 class x { }
132 class Foo { x() {} }
133 function foo(...x) { }
134 var { x } = {};
135 var { prop: a} = {};
136 var [x] = arr;
137 var { prop: [x]} = {};
138 ({ prop: obj.x } = {});
139 ```
140
141 :::
142
143 Examples of **correct** code for this rule with the `{ "min": 4 }` option:
144
145 ::: correct
146
147 ```js
148 /*eslint id-length: ["error", { "min": 4 }]*/
149 /*eslint-env es6*/
150
151 var value = 5;
152 function func() { return 42; }
153 obj.element = document.body;
154 var foobar = function (event) { /* do stuff */ };
155 try {
156 dangerousStuff();
157 } catch (error) {
158 // ignore as many do
159 }
160 var myObj = { apple: 1 };
161 (value) => { value * value };
162 function foobar(value = 0) { }
163 class MyClass { }
164 class Foobar { method() {} }
165 function foobar(...args) { }
166 var { prop } = {};
167 var [longName] = foo;
168 var { a: [prop] } = {};
169 var { a: longName } = {};
170 ({ prop: obj.name } = {});
171 var data = { "x": 1 }; // excused because of quotes
172 data["y"] = 3; // excused because of calculated property access
173 ```
174
175 :::
176
177 ### max
178
179 Examples of **incorrect** code for this rule with the `{ "max": 10 }` option:
180
181 ::: incorrect
182
183 ```js
184 /*eslint id-length: ["error", { "max": 10 }]*/
185 /*eslint-env es6*/
186
187 var reallyLongVarName = 5;
188 function reallyLongFuncName() { return 42; }
189 obj.reallyLongPropName = document.body;
190 var foo = function (reallyLongArgName) { /* do stuff */ };
191 try {
192 dangerousStuff();
193 } catch (reallyLongErrorName) {
194 // ignore as many do
195 }
196 (reallyLongArgName) => { return !reallyLongArgName; };
197 var [reallyLongFirstElementName] = arr;
198 ```
199
200 :::
201
202 Examples of **correct** code for this rule with the `{ "max": 10 }` option:
203
204 ::: correct
205
206 ```js
207 /*eslint id-length: ["error", { "max": 10 }]*/
208 /*eslint-env es6*/
209
210 var varName = 5;
211 function funcName() { return 42; }
212 obj.propName = document.body;
213 var foo = function (arg) { /* do stuff */ };
214 try {
215 dangerousStuff();
216 } catch (error) {
217 // ignore as many do
218 }
219 (arg) => { return !arg; };
220 var [first] = arr;
221 ```
222
223 :::
224
225 ### properties
226
227 Examples of **correct** code for this rule with the `{ "properties": "never" }` option:
228
229 ::: correct
230
231 ```js
232 /*eslint id-length: ["error", { "properties": "never" }]*/
233 /*eslint-env es6*/
234
235 var myObj = { a: 1 };
236 ({ a: obj.x.y.z } = {});
237 ({ prop: obj.i } = {});
238 ```
239
240 :::
241
242 ### exceptions
243
244 Examples of additional **correct** code for this rule with the `{ "exceptions": ["x"] }` option:
245
246 ::: correct
247
248 ```js
249 /*eslint id-length: ["error", { "exceptions": ["x"] }]*/
250 /*eslint-env es6*/
251
252 var x = 5;
253 function x() { return 42; }
254 obj.x = document.body;
255 var foo = function (x) { /* do stuff */ };
256 try {
257 dangerousStuff();
258 } catch (x) {
259 // ignore as many do
260 }
261 (x) => { return x * x; };
262 var [x] = arr;
263 const { x } = foo;
264 const { a: x } = foo;
265 ```
266
267 :::
268
269 ### exceptionPatterns
270
271 Examples of additional **correct** code for this rule with the `{ "exceptionPatterns": ["E|S", "[x-z]"] }` option:
272
273 ::: correct
274
275 ```js
276 /*eslint id-length: ["error", { "exceptionPatterns": ["E|S", "[x-z]"] }]*/
277 /*eslint-env es6*/
278
279 var E = 5;
280 function S() { return 42; }
281 obj.x = document.body;
282 var foo = function (x) { /* do stuff */ };
283 try {
284 dangerousStuff();
285 } catch (x) {
286 // ignore as many do
287 }
288 (y) => {return y * y};
289 var [E] = arr;
290 const { y } = foo;
291 const { a: z } = foo;
292 ```
293
294 :::