]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
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() {} } | |
609c276f TL |
33 | class Foo { #x() {} } |
34 | class Foo { x = 1 } | |
35 | class Foo { #x = 1 } | |
eb39fafa DC |
36 | function foo(...x) { } |
37 | function foo([x]) { } | |
38 | var [x] = arr; | |
39 | var { prop: [x]} = {}; | |
40 | function foo({x}) { } | |
41 | var { x } = {}; | |
42 | var { prop: a} = {}; | |
43 | ({ prop: obj.x } = {}); | |
44 | ``` | |
45 | ||
46 | Examples 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 | ||
52 | var num = 5; | |
53 | function _f() { return 42; } | |
54 | function _func() { return 42; } | |
55 | obj.el = document.body; | |
56 | var foo = function (evt) { /* do stuff */ }; | |
57 | try { | |
58 | dangerousStuff(); | |
59 | } catch (error) { | |
60 | // ignore as many do | |
61 | } | |
62 | var myObj = { apple: 1 }; | |
63 | (num) => { num * num }; | |
64 | function foo(num = 0) { } | |
65 | class MyClass { } | |
66 | class Foo { method() {} } | |
609c276f TL |
67 | class Foo { #method() {} } |
68 | class Foo { field = 1 } | |
69 | class Foo { #field = 1 } | |
eb39fafa DC |
70 | function foo(...args) { } |
71 | function foo([longName]) { } | |
72 | var { prop } = {}; | |
73 | var { prop: [longName] } = {}; | |
74 | var [longName] = arr; | |
75 | function foo({ prop }) { } | |
76 | function foo({ a: prop }) { } | |
77 | var { prop } = {}; | |
78 | var { a: prop } = {}; | |
79 | ({ prop: obj.longName } = {}); | |
80 | var data = { "x": 1 }; // excused because of quotes | |
81 | data["y"] = 3; // excused because of calculated property access | |
82 | ``` | |
83 | ||
84 | This 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 | ||
95 | Examples 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 | ||
101 | var val = 5; | |
102 | obj.e = document.body; | |
103 | function foo (e) { }; | |
104 | try { | |
105 | dangerousStuff(); | |
106 | } catch (e) { | |
107 | // ignore as many do | |
108 | } | |
109 | var myObj = { a: 1 }; | |
110 | (val) => { val * val }; | |
111 | class x { } | |
112 | class Foo { x() {} } | |
113 | function foo(...x) { } | |
114 | var { x } = {}; | |
115 | var { prop: a} = {}; | |
116 | var [x] = arr; | |
117 | var { prop: [x]} = {}; | |
118 | ({ prop: obj.x } = {}); | |
119 | ``` | |
120 | ||
121 | Examples 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 | ||
127 | var value = 5; | |
128 | function func() { return 42; } | |
129 | obj.element = document.body; | |
130 | var foobar = function (event) { /* do stuff */ }; | |
131 | try { | |
132 | dangerousStuff(); | |
133 | } catch (error) { | |
134 | // ignore as many do | |
135 | } | |
136 | var myObj = { apple: 1 }; | |
137 | (value) => { value * value }; | |
138 | function foobar(value = 0) { } | |
139 | class MyClass { } | |
140 | class Foobar { method() {} } | |
141 | function foobar(...args) { } | |
142 | var { prop } = {}; | |
143 | var [longName] = foo; | |
144 | var { a: [prop] } = {}; | |
145 | var { a: longName } = {}; | |
146 | ({ prop: obj.name } = {}); | |
147 | var data = { "x": 1 }; // excused because of quotes | |
148 | data["y"] = 3; // excused because of calculated property access | |
149 | ``` | |
150 | ||
151 | ### max | |
152 | ||
153 | Examples 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 | ||
159 | var reallyLongVarName = 5; | |
160 | function reallyLongFuncName() { return 42; } | |
161 | obj.reallyLongPropName = document.body; | |
162 | var foo = function (reallyLongArgName) { /* do stuff */ }; | |
163 | try { | |
164 | dangerousStuff(); | |
165 | } catch (reallyLongErrorName) { | |
166 | // ignore as many do | |
167 | } | |
168 | (reallyLongArgName) => { return !reallyLongArgName; }; | |
169 | var [reallyLongFirstElementName] = arr; | |
170 | ``` | |
171 | ||
172 | Examples 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 | ||
178 | var varName = 5; | |
179 | function funcName() { return 42; } | |
180 | obj.propName = document.body; | |
181 | var foo = function (arg) { /* do stuff */ }; | |
182 | try { | |
183 | dangerousStuff(); | |
184 | } catch (error) { | |
185 | // ignore as many do | |
186 | } | |
187 | (arg) => { return !arg; }; | |
188 | var [first] = arr; | |
189 | ``` | |
190 | ||
191 | ### properties | |
192 | ||
193 | Examples 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 | ||
199 | var myObj = { a: 1 }; | |
200 | ({ a: obj.x.y.z } = {}); | |
201 | ({ prop: obj.i } = {}); | |
202 | ``` | |
203 | ||
204 | ### exceptions | |
205 | ||
206 | Examples 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 | ||
212 | var x = 5; | |
213 | function x() { return 42; } | |
214 | obj.x = document.body; | |
215 | var foo = function (x) { /* do stuff */ }; | |
216 | try { | |
217 | dangerousStuff(); | |
218 | } catch (x) { | |
219 | // ignore as many do | |
220 | } | |
221 | (x) => { return x * x; }; | |
222 | var [x] = arr; | |
223 | const { x } = foo; | |
224 | const { a: x } = foo; | |
225 | ``` | |
226 | ||
6f036462 TL |
227 | ### exceptionPatterns |
228 | ||
229 | Examples 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 | ||
235 | var E = 5; | |
236 | function S() { return 42; } | |
237 | obj.x = document.body; | |
238 | var foo = function (x) { /* do stuff */ }; | |
239 | try { | |
240 | dangerousStuff(); | |
241 | } catch (x) { | |
242 | // ignore as many do | |
243 | } | |
244 | (y) => {return y * y}; | |
245 | var [E] = arr; | |
246 | const { y } = foo; | |
247 | const { 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) |