]>
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() {} } | |
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 | ||
86 | ### min | |
87 | ||
88 | Examples 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 | ||
94 | var val = 5; | |
95 | obj.e = document.body; | |
96 | function foo (e) { }; | |
97 | try { | |
98 | dangerousStuff(); | |
99 | } catch (e) { | |
100 | // ignore as many do | |
101 | } | |
102 | var myObj = { a: 1 }; | |
103 | (val) => { val * val }; | |
104 | class x { } | |
105 | class Foo { x() {} } | |
106 | function foo(...x) { } | |
107 | var { x } = {}; | |
108 | var { prop: a} = {}; | |
109 | var [x] = arr; | |
110 | var { prop: [x]} = {}; | |
111 | ({ prop: obj.x } = {}); | |
112 | ``` | |
113 | ||
114 | Examples 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 | ||
120 | var value = 5; | |
121 | function func() { return 42; } | |
122 | obj.element = document.body; | |
123 | var foobar = function (event) { /* do stuff */ }; | |
124 | try { | |
125 | dangerousStuff(); | |
126 | } catch (error) { | |
127 | // ignore as many do | |
128 | } | |
129 | var myObj = { apple: 1 }; | |
130 | (value) => { value * value }; | |
131 | function foobar(value = 0) { } | |
132 | class MyClass { } | |
133 | class Foobar { method() {} } | |
134 | function foobar(...args) { } | |
135 | var { prop } = {}; | |
136 | var [longName] = foo; | |
137 | var { a: [prop] } = {}; | |
138 | var { a: longName } = {}; | |
139 | ({ prop: obj.name } = {}); | |
140 | var data = { "x": 1 }; // excused because of quotes | |
141 | data["y"] = 3; // excused because of calculated property access | |
142 | ``` | |
143 | ||
144 | ### max | |
145 | ||
146 | Examples 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 | ||
152 | var reallyLongVarName = 5; | |
153 | function reallyLongFuncName() { return 42; } | |
154 | obj.reallyLongPropName = document.body; | |
155 | var foo = function (reallyLongArgName) { /* do stuff */ }; | |
156 | try { | |
157 | dangerousStuff(); | |
158 | } catch (reallyLongErrorName) { | |
159 | // ignore as many do | |
160 | } | |
161 | (reallyLongArgName) => { return !reallyLongArgName; }; | |
162 | var [reallyLongFirstElementName] = arr; | |
163 | ``` | |
164 | ||
165 | Examples 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 | ||
171 | var varName = 5; | |
172 | function funcName() { return 42; } | |
173 | obj.propName = document.body; | |
174 | var foo = function (arg) { /* do stuff */ }; | |
175 | try { | |
176 | dangerousStuff(); | |
177 | } catch (error) { | |
178 | // ignore as many do | |
179 | } | |
180 | (arg) => { return !arg; }; | |
181 | var [first] = arr; | |
182 | ``` | |
183 | ||
184 | ### properties | |
185 | ||
186 | Examples 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 | ||
192 | var myObj = { a: 1 }; | |
193 | ({ a: obj.x.y.z } = {}); | |
194 | ({ prop: obj.i } = {}); | |
195 | ``` | |
196 | ||
197 | ### exceptions | |
198 | ||
199 | Examples 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 | ||
205 | var x = 5; | |
206 | function x() { return 42; } | |
207 | obj.x = document.body; | |
208 | var foo = function (x) { /* do stuff */ }; | |
209 | try { | |
210 | dangerousStuff(); | |
211 | } catch (x) { | |
212 | // ignore as many do | |
213 | } | |
214 | (x) => { return x * x; }; | |
215 | var [x] = arr; | |
216 | const { x } = foo; | |
217 | const { 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) |