]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/id-length.md
first commit
[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() {} }
33function foo(...x) { }
34function foo([x]) { }
35var [x] = arr;
36var { prop: [x]} = {};
37function foo({x}) { }
38var { x } = {};
39var { prop: a} = {};
40({ prop: obj.x } = {});
41```
42
43Examples 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
49var num = 5;
50function _f() { return 42; }
51function _func() { return 42; }
52obj.el = document.body;
53var foo = function (evt) { /* do stuff */ };
54try {
55 dangerousStuff();
56} catch (error) {
57 // ignore as many do
58}
59var myObj = { apple: 1 };
60(num) => { num * num };
61function foo(num = 0) { }
62class MyClass { }
63class Foo { method() {} }
64function foo(...args) { }
65function foo([longName]) { }
66var { prop } = {};
67var { prop: [longName] } = {};
68var [longName] = arr;
69function foo({ prop }) { }
70function foo({ a: prop }) { }
71var { prop } = {};
72var { a: prop } = {};
73({ prop: obj.longName } = {});
74var data = { "x": 1 }; // excused because of quotes
75data["y"] = 3; // excused because of calculated property access
76```
77
78This 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
86### min
87
88Examples of **incorrect** code for this rule with the `{ "min": 4 }` option:
89
90```js
91/*eslint id-length: ["error", { "min": 4 }]*/
92/*eslint-env es6*/
93
94var val = 5;
95obj.e = document.body;
96function foo (e) { };
97try {
98 dangerousStuff();
99} catch (e) {
100 // ignore as many do
101}
102var myObj = { a: 1 };
103(val) => { val * val };
104class x { }
105class Foo { x() {} }
106function foo(...x) { }
107var { x } = {};
108var { prop: a} = {};
109var [x] = arr;
110var { prop: [x]} = {};
111({ prop: obj.x } = {});
112```
113
114Examples of **correct** code for this rule with the `{ "min": 4 }` option:
115
116```js
117/*eslint id-length: ["error", { "min": 4 }]*/
118/*eslint-env es6*/
119
120var value = 5;
121function func() { return 42; }
122obj.element = document.body;
123var foobar = function (event) { /* do stuff */ };
124try {
125 dangerousStuff();
126} catch (error) {
127 // ignore as many do
128}
129var myObj = { apple: 1 };
130(value) => { value * value };
131function foobar(value = 0) { }
132class MyClass { }
133class Foobar { method() {} }
134function foobar(...args) { }
135var { prop } = {};
136var [longName] = foo;
137var { a: [prop] } = {};
138var { a: longName } = {};
139({ prop: obj.name } = {});
140var data = { "x": 1 }; // excused because of quotes
141data["y"] = 3; // excused because of calculated property access
142```
143
144### max
145
146Examples of **incorrect** code for this rule with the `{ "max": 10 }` option:
147
148```js
149/*eslint id-length: ["error", { "max": 10 }]*/
150/*eslint-env es6*/
151
152var reallyLongVarName = 5;
153function reallyLongFuncName() { return 42; }
154obj.reallyLongPropName = document.body;
155var foo = function (reallyLongArgName) { /* do stuff */ };
156try {
157 dangerousStuff();
158} catch (reallyLongErrorName) {
159 // ignore as many do
160}
161(reallyLongArgName) => { return !reallyLongArgName; };
162var [reallyLongFirstElementName] = arr;
163```
164
165Examples of **correct** code for this rule with the `{ "max": 10 }` option:
166
167```js
168/*eslint id-length: ["error", { "max": 10 }]*/
169/*eslint-env es6*/
170
171var varName = 5;
172function funcName() { return 42; }
173obj.propName = document.body;
174var foo = function (arg) { /* do stuff */ };
175try {
176 dangerousStuff();
177} catch (error) {
178 // ignore as many do
179}
180(arg) => { return !arg; };
181var [first] = arr;
182```
183
184### properties
185
186Examples of **correct** code for this rule with the `{ "properties": "never" }` option:
187
188```js
189/*eslint id-length: ["error", { "properties": "never" }]*/
190/*eslint-env es6*/
191
192var myObj = { a: 1 };
193({ a: obj.x.y.z } = {});
194({ prop: obj.i } = {});
195```
196
197### exceptions
198
199Examples of additional **correct** code for this rule with the `{ "exceptions": ["x"] }` option:
200
201```js
202/*eslint id-length: ["error", { "exceptions": ["x"] }]*/
203/*eslint-env es6*/
204
205var x = 5;
206function x() { return 42; }
207obj.x = document.body;
208var foo = function (x) { /* do stuff */ };
209try {
210 dangerousStuff();
211} catch (x) {
212 // ignore as many do
213}
214(x) => { return x * x; };
215var [x] = arr;
216const { x } = foo;
217const { a: x } = foo;
218```
219
220## Related Rules
221
222* [max-len](max-len.md)
223* [new-cap](new-cap.md)
224* [func-names](func-names.md)
225* [camelcase](camelcase.md)