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