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