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