]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # enforce variables to be declared either together or separately in functions (one-var) |
2 | ||
3 | Variables can be declared at any point in JavaScript code using `var`, `let`, or `const`. There are many styles and preferences related to the declaration of variables, and one of those is deciding on how many variable declarations should be allowed in a single function. | |
4 | ||
5 | There are two schools of thought in this regard: | |
6 | ||
7 | 1. There should be just one variable declaration for all variables in the function. That declaration typically appears at the top of the function. | |
8 | 1. You should use one variable declaration for each variable you want to define. | |
9 | ||
10 | For instance: | |
11 | ||
12 | ```js | |
13 | // one variable declaration per function | |
14 | function foo() { | |
15 | var bar, baz; | |
16 | } | |
17 | ||
18 | // multiple variable declarations per function | |
19 | function foo() { | |
20 | var bar; | |
21 | var baz; | |
22 | } | |
23 | ``` | |
24 | ||
25 | The single-declaration school of thought is based in pre-ECMAScript 6 behaviors, where there was no such thing as block scope, only function scope. Since all `var` statements are hoisted to the top of the function anyway, some believe that declaring all variables in a single declaration at the top of the function removes confusion around scoping rules. | |
26 | ||
27 | ## Rule Details | |
28 | ||
29 | This rule enforces variables to be declared either together or separately per function ( for `var`) or block (for `let` and `const`) scope. | |
30 | ||
31 | ## Options | |
32 | ||
33 | This rule has one option, which can be a string option or an object option. | |
34 | ||
35 | String option: | |
36 | ||
37 | * `"always"` (default) requires one variable declaration per scope | |
38 | * `"never"` requires multiple variable declarations per scope | |
39 | * `"consecutive"` allows multiple variable declarations per scope but requires consecutive variable declarations to be combined into a single declaration | |
40 | ||
41 | Object option: | |
42 | ||
43 | * `"var": "always"` requires one `var` declaration per function | |
44 | * `"var": "never"` requires multiple `var` declarations per function | |
45 | * `"var": "consecutive"` requires consecutive `var` declarations to be a single declaration | |
46 | * `"let": "always"` requires one `let` declaration per block | |
47 | * `"let": "never"` requires multiple `let` declarations per block | |
48 | * `"let": "consecutive"` requires consecutive `let` declarations to be a single declaration | |
49 | * `"const": "always"` requires one `const` declaration per block | |
50 | * `"const": "never"` requires multiple `const` declarations per block | |
51 | * `"const": "consecutive"` requires consecutive `const` declarations to be a single declaration | |
52 | * `"separateRequires": true` enforces `requires` to be separate from declarations | |
53 | ||
54 | Alternate object option: | |
55 | ||
56 | * `"initialized": "always"` requires one variable declaration for initialized variables per scope | |
57 | * `"initialized": "never"` requires multiple variable declarations for initialized variables per scope | |
58 | * `"initialized": "consecutive"` requires consecutive variable declarations for initialized variables to be a single declaration | |
59 | * `"uninitialized": "always"` requires one variable declaration for uninitialized variables per scope | |
60 | * `"uninitialized": "never"` requires multiple variable declarations for uninitialized variables per scope | |
61 | * `"uninitialized": "consecutive"` requires consecutive variable declarations for uninitialized variables to be a single declaration | |
62 | ||
63 | ### always | |
64 | ||
65 | Examples of **incorrect** code for this rule with the default `"always"` option: | |
66 | ||
67 | ```js | |
68 | /*eslint one-var: ["error", "always"]*/ | |
eb39fafa DC |
69 | |
70 | function foo() { | |
71 | var bar; | |
72 | var baz; | |
73 | let qux; | |
74 | let norf; | |
75 | } | |
76 | ||
77 | function foo(){ | |
78 | const bar = false; | |
79 | const baz = true; | |
80 | let qux; | |
81 | let norf; | |
82 | } | |
83 | ||
84 | function foo() { | |
85 | var bar; | |
86 | ||
87 | if (baz) { | |
88 | var qux = true; | |
89 | } | |
90 | } | |
609c276f TL |
91 | |
92 | class C { | |
93 | static { | |
94 | var foo; | |
95 | var bar; | |
96 | } | |
97 | ||
98 | static { | |
99 | var foo; | |
100 | if (bar) { | |
101 | var baz = true; | |
102 | } | |
103 | } | |
104 | ||
105 | static { | |
106 | let foo; | |
107 | let bar; | |
108 | } | |
109 | } | |
eb39fafa DC |
110 | ``` |
111 | ||
112 | Examples of **correct** code for this rule with the default `"always"` option: | |
113 | ||
114 | ```js | |
115 | /*eslint one-var: ["error", "always"]*/ | |
eb39fafa DC |
116 | |
117 | function foo() { | |
118 | var bar, | |
119 | baz; | |
120 | let qux, | |
121 | norf; | |
122 | } | |
123 | ||
124 | function foo(){ | |
125 | const bar = true, | |
126 | baz = false; | |
127 | let qux, | |
128 | norf; | |
129 | } | |
130 | ||
131 | function foo() { | |
132 | var bar, | |
133 | qux; | |
134 | ||
135 | if (baz) { | |
136 | qux = true; | |
137 | } | |
138 | } | |
139 | ||
140 | function foo(){ | |
141 | let bar; | |
142 | ||
143 | if (baz) { | |
144 | let qux; | |
145 | } | |
146 | } | |
609c276f TL |
147 | |
148 | class C { | |
149 | static { | |
150 | var foo, bar; | |
151 | } | |
152 | ||
153 | static { | |
154 | var foo, baz; | |
155 | if (bar) { | |
156 | baz = true; | |
157 | } | |
158 | } | |
159 | ||
160 | static { | |
161 | let foo, bar; | |
162 | } | |
163 | ||
164 | static { | |
165 | let foo; | |
166 | if (bar) { | |
167 | let baz; | |
168 | } | |
169 | } | |
170 | } | |
eb39fafa DC |
171 | ``` |
172 | ||
173 | ### never | |
174 | ||
175 | Examples of **incorrect** code for this rule with the `"never"` option: | |
176 | ||
177 | ```js | |
178 | /*eslint one-var: ["error", "never"]*/ | |
eb39fafa DC |
179 | |
180 | function foo() { | |
181 | var bar, | |
182 | baz; | |
183 | const bar = true, | |
184 | baz = false; | |
185 | } | |
186 | ||
187 | function foo() { | |
188 | var bar, | |
189 | qux; | |
190 | ||
191 | if (baz) { | |
192 | qux = true; | |
193 | } | |
194 | } | |
195 | ||
196 | function foo(){ | |
197 | let bar = true, | |
198 | baz = false; | |
199 | } | |
609c276f TL |
200 | |
201 | class C { | |
202 | static { | |
203 | var foo, bar; | |
204 | let baz, qux; | |
205 | } | |
206 | } | |
eb39fafa DC |
207 | ``` |
208 | ||
209 | Examples of **correct** code for this rule with the `"never"` option: | |
210 | ||
211 | ```js | |
212 | /*eslint one-var: ["error", "never"]*/ | |
eb39fafa DC |
213 | |
214 | function foo() { | |
215 | var bar; | |
216 | var baz; | |
217 | } | |
218 | ||
219 | function foo() { | |
220 | var bar; | |
221 | ||
222 | if (baz) { | |
223 | var qux = true; | |
224 | } | |
225 | } | |
226 | ||
227 | function foo() { | |
228 | let bar; | |
229 | ||
230 | if (baz) { | |
231 | let qux = true; | |
232 | } | |
233 | } | |
609c276f TL |
234 | |
235 | class C { | |
236 | static { | |
237 | var foo; | |
238 | var bar; | |
239 | let baz; | |
240 | let qux; | |
241 | } | |
242 | } | |
eb39fafa DC |
243 | ``` |
244 | ||
245 | ### consecutive | |
246 | ||
247 | Examples of **incorrect** code for this rule with the `"consecutive"` option: | |
248 | ||
249 | ```js | |
250 | /*eslint one-var: ["error", "consecutive"]*/ | |
eb39fafa DC |
251 | |
252 | function foo() { | |
253 | var bar; | |
254 | var baz; | |
255 | } | |
256 | ||
257 | function foo(){ | |
258 | var bar = 1; | |
259 | var baz = 2; | |
260 | ||
261 | qux(); | |
262 | ||
263 | var qux = 3; | |
264 | var quux; | |
265 | } | |
609c276f TL |
266 | |
267 | class C { | |
268 | static { | |
269 | var foo; | |
270 | var bar; | |
271 | let baz; | |
272 | let qux; | |
273 | } | |
274 | } | |
eb39fafa DC |
275 | ``` |
276 | ||
277 | Examples of **correct** code for this rule with the `"consecutive"` option: | |
278 | ||
279 | ```js | |
280 | /*eslint one-var: ["error", "consecutive"]*/ | |
eb39fafa DC |
281 | |
282 | function foo() { | |
283 | var bar, | |
284 | baz; | |
285 | } | |
286 | ||
287 | function foo(){ | |
288 | var bar = 1, | |
289 | baz = 2; | |
290 | ||
291 | qux(); | |
292 | ||
293 | var qux = 3, | |
294 | quux; | |
295 | } | |
609c276f TL |
296 | |
297 | class C { | |
298 | static { | |
299 | var foo, bar; | |
300 | let baz, qux; | |
301 | doSomething(); | |
302 | let quux; | |
303 | var quuux; | |
304 | } | |
305 | } | |
eb39fafa DC |
306 | ``` |
307 | ||
308 | ### var, let, and const | |
309 | ||
310 | Examples of **incorrect** code for this rule with the `{ var: "always", let: "never", const: "never" }` option: | |
311 | ||
312 | ```js | |
313 | /*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/ | |
314 | /*eslint-env es6*/ | |
315 | ||
316 | function foo() { | |
317 | var bar; | |
318 | var baz; | |
319 | let qux, | |
320 | norf; | |
321 | } | |
322 | ||
323 | function foo() { | |
324 | const bar = 1, | |
325 | baz = 2; | |
326 | let qux, | |
327 | norf; | |
328 | } | |
329 | ``` | |
330 | ||
331 | Examples of **correct** code for this rule with the `{ var: "always", let: "never", const: "never" }` option: | |
332 | ||
333 | ```js | |
334 | /*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/ | |
335 | /*eslint-env es6*/ | |
336 | ||
337 | function foo() { | |
338 | var bar, | |
339 | baz; | |
340 | let qux; | |
341 | let norf; | |
342 | } | |
343 | ||
344 | function foo() { | |
345 | const bar = 1; | |
346 | const baz = 2; | |
347 | let qux; | |
348 | let norf; | |
349 | } | |
350 | ``` | |
351 | ||
352 | Examples of **incorrect** code for this rule with the `{ var: "never" }` option: | |
353 | ||
354 | ```js | |
355 | /*eslint one-var: ["error", { var: "never" }]*/ | |
356 | /*eslint-env es6*/ | |
357 | ||
358 | function foo() { | |
359 | var bar, | |
360 | baz; | |
361 | } | |
362 | ``` | |
363 | ||
364 | Examples of **correct** code for this rule with the `{ var: "never" }` option: | |
365 | ||
366 | ```js | |
367 | /*eslint one-var: ["error", { var: "never" }]*/ | |
368 | /*eslint-env es6*/ | |
369 | ||
370 | function foo() { | |
371 | var bar, | |
372 | baz; | |
373 | const bar = 1; // `const` and `let` declarations are ignored if they are not specified | |
374 | const baz = 2; | |
375 | let qux; | |
376 | let norf; | |
377 | } | |
378 | ``` | |
379 | ||
380 | Examples of **incorrect** code for this rule with the `{ separateRequires: true }` option: | |
381 | ||
382 | ```js | |
383 | /*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/ | |
384 | /*eslint-env node*/ | |
385 | ||
386 | var foo = require("foo"), | |
387 | bar = "bar"; | |
388 | ``` | |
389 | ||
390 | Examples of **correct** code for this rule with the `{ separateRequires: true }` option: | |
391 | ||
392 | ```js | |
393 | /*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/ | |
394 | /*eslint-env node*/ | |
395 | ||
396 | var foo = require("foo"); | |
397 | var bar = "bar"; | |
398 | ``` | |
399 | ||
400 | ```js | |
401 | var foo = require("foo"), | |
402 | bar = require("bar"); | |
403 | ``` | |
404 | ||
405 | Examples of **incorrect** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive" }` option: | |
406 | ||
407 | ```js | |
408 | /*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/ | |
409 | /*eslint-env es6*/ | |
410 | ||
411 | function foo() { | |
412 | let a, | |
413 | b; | |
414 | let c; | |
415 | ||
416 | var d, | |
417 | e; | |
418 | } | |
419 | ||
420 | function foo() { | |
421 | const a = 1, | |
422 | b = 2; | |
423 | const c = 3; | |
424 | ||
425 | var d, | |
426 | e; | |
427 | } | |
428 | ``` | |
429 | ||
430 | Examples of **correct** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive" }` option: | |
431 | ||
432 | ```js | |
433 | /*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/ | |
434 | /*eslint-env es6*/ | |
435 | ||
436 | function foo() { | |
437 | let a, | |
438 | b; | |
439 | ||
440 | var d; | |
441 | var e; | |
442 | ||
443 | let f; | |
444 | } | |
445 | ||
446 | function foo() { | |
447 | const a = 1, | |
448 | b = 2; | |
449 | ||
450 | var c; | |
451 | var d; | |
452 | ||
453 | const e = 3; | |
454 | } | |
455 | ``` | |
456 | ||
457 | Examples of **incorrect** code for this rule with the `{ var: "consecutive" }` option: | |
458 | ||
459 | ```js | |
460 | /*eslint one-var: ["error", { var: "consecutive" }]*/ | |
461 | /*eslint-env es6*/ | |
462 | ||
463 | function foo() { | |
464 | var a; | |
465 | var b; | |
466 | } | |
467 | ``` | |
468 | ||
469 | Examples of **correct** code for this rule with the `{ var: "consecutive" }` option: | |
470 | ||
471 | ```js | |
472 | /*eslint one-var: ["error", { var: "consecutive" }]*/ | |
473 | /*eslint-env es6*/ | |
474 | ||
475 | function foo() { | |
476 | var a, | |
477 | b; | |
478 | const c = 1; // `const` and `let` declarations are ignored if they are not specified | |
479 | const d = 2; | |
480 | let e; | |
481 | let f; | |
482 | } | |
483 | ``` | |
484 | ||
485 | ### initialized and uninitialized | |
486 | ||
487 | Examples of **incorrect** code for this rule with the `{ "initialized": "always", "uninitialized": "never" }` option: | |
488 | ||
489 | ```js | |
490 | /*eslint one-var: ["error", { "initialized": "always", "uninitialized": "never" }]*/ | |
491 | /*eslint-env es6*/ | |
492 | ||
493 | function foo() { | |
494 | var a, b, c; | |
495 | var foo = true; | |
496 | var bar = false; | |
497 | } | |
498 | ``` | |
499 | ||
500 | Examples of **correct** code for this rule with the `{ "initialized": "always", "uninitialized": "never" }` option: | |
501 | ||
502 | ```js | |
503 | /*eslint one-var: ["error", { "initialized": "always", "uninitialized": "never" }]*/ | |
504 | ||
505 | function foo() { | |
506 | var a; | |
507 | var b; | |
508 | var c; | |
509 | var foo = true, | |
510 | bar = false; | |
511 | } | |
512 | ||
513 | for (let z of foo) { | |
514 | doSomething(z); | |
515 | } | |
516 | ||
517 | let z; | |
518 | for (z of foo) { | |
519 | doSomething(z); | |
520 | } | |
521 | ``` | |
522 | ||
523 | Examples of **incorrect** code for this rule with the `{ "initialized": "never" }` option: | |
524 | ||
525 | ```js | |
526 | /*eslint one-var: ["error", { "initialized": "never" }]*/ | |
527 | /*eslint-env es6*/ | |
528 | ||
529 | function foo() { | |
530 | var foo = true, | |
531 | bar = false; | |
532 | } | |
533 | ``` | |
534 | ||
535 | Examples of **correct** code for this rule with the `{ "initialized": "never" }` option: | |
536 | ||
537 | ```js | |
538 | /*eslint one-var: ["error", { "initialized": "never" }]*/ | |
539 | ||
540 | function foo() { | |
541 | var foo = true; | |
542 | var bar = false; | |
543 | var a, b, c; // Uninitialized variables are ignored | |
544 | } | |
545 | ``` | |
546 | ||
547 | Examples of **incorrect** code for this rule with the `{ "initialized": "consecutive", "uninitialized": "never" }` option: | |
548 | ||
549 | ```js | |
550 | /*eslint one-var: ["error", { "initialized": "consecutive", "uninitialized": "never" }]*/ | |
551 | ||
552 | function foo() { | |
553 | var a = 1; | |
554 | var b = 2; | |
555 | var c, | |
556 | d; | |
557 | var e = 3; | |
558 | var f = 4; | |
559 | } | |
560 | ``` | |
561 | ||
562 | Examples of **correct** code for this rule with the `{ "initialized": "consecutive", "uninitialized": "never" }` option: | |
563 | ||
564 | ```js | |
565 | /*eslint one-var: ["error", { "initialized": "consecutive", "uninitialized": "never" }]*/ | |
566 | ||
567 | function foo() { | |
568 | var a = 1, | |
569 | b = 2; | |
570 | var c; | |
571 | var d; | |
572 | var e = 3, | |
573 | f = 4; | |
574 | } | |
575 | ``` | |
576 | ||
577 | Examples of **incorrect** code for this rule with the `{ "initialized": "consecutive" }` option: | |
578 | ||
579 | ```js | |
580 | /*eslint one-var: ["error", { "initialized": "consecutive" }]*/ | |
581 | ||
582 | function foo() { | |
583 | var a = 1; | |
584 | var b = 2; | |
585 | ||
586 | foo(); | |
587 | ||
588 | var c = 3; | |
589 | var d = 4; | |
590 | } | |
591 | ``` | |
592 | ||
593 | Examples of **correct** code for this rule with the `{ "initialized": "consecutive" }` option: | |
594 | ||
595 | ```js | |
596 | /*eslint one-var: ["error", { "initialized": "consecutive" }]*/ | |
597 | ||
598 | function foo() { | |
599 | var a = 1, | |
600 | b = 2; | |
601 | ||
602 | foo(); | |
603 | ||
604 | var c = 3, | |
605 | d = 4; | |
606 | } | |
607 | ``` | |
608 | ||
609 | ## Compatibility | |
610 | ||
611 | * **JSHint**: This rule maps to the `onevar` JSHint rule, but allows `let` and `const` to be configured separately. | |
612 | * **JSCS**: This rule roughly maps to [disallowMultipleVarDecl](https://jscs-dev.github.io/rule/disallowMultipleVarDecl). | |
613 | * **JSCS**: This rule option `separateRequires` roughly maps to [requireMultipleVarDecl](https://jscs-dev.github.io/rule/requireMultipleVarDecl). |