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