]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/sort-vars.md
first commit
[pve-eslint.git] / eslint / docs / rules / sort-vars.md
1 # Variable Sorting (sort-vars)
2
3 When declaring multiple variables within the same block, some developers prefer to sort variable names alphabetically to be able to find necessary variable easier at the later time. Others feel that it adds complexity and becomes burden to maintain.
4
5 ## Rule Details
6
7 This rule checks all variable declaration blocks and verifies that all variables are sorted alphabetically.
8 The default configuration of the rule is case-sensitive.
9
10 Examples of **incorrect** code for this rule:
11
12 ```js
13 /*eslint sort-vars: "error"*/
14
15 var b, a;
16
17 var a, B, c;
18
19 var a, A;
20 ```
21
22 Examples of **correct** code for this rule:
23
24 ```js
25 /*eslint sort-vars: "error"*/
26
27 var a, b, c, d;
28
29 var _a = 10;
30 var _b = 20;
31
32 var A, a;
33
34 var B, a, c;
35 ```
36
37 Alphabetical list is maintained starting from the first variable and excluding any that are considered problems. So the following code will produce two problems:
38
39 ```js
40 /*eslint sort-vars: "error"*/
41
42 var c, d, a, b;
43 ```
44
45 But this one, will only produce one:
46
47 ```js
48 /*eslint sort-vars: "error"*/
49
50 var c, d, a, e;
51 ```
52
53 ## Options
54
55 This rule has an object option:
56
57 * `"ignoreCase": true` (default `false`) ignores the case-sensitivity of the variables order
58
59 ### ignoreCase
60
61 Examples of **correct** code for this rule with the `{ "ignoreCase": true }` option:
62
63 ```js
64 /*eslint sort-vars: ["error", { "ignoreCase": true }]*/
65
66 var a, A;
67
68 var a, B, c;
69 ```
70
71 ## When Not To Use It
72
73 This rule is a formatting preference and not following it won't negatively affect the quality of your code. If you alphabetizing variables isn't a part of your coding standards, then you can leave this rule off.
74
75 ## Related Rules
76
77 * [sort-keys](sort-keys.md)
78 * [sort-imports](sort-imports.md)