]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/sort-vars.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / sort-vars.md
CommitLineData
eb39fafa
DC
1# Variable Sorting (sort-vars)
2
3When 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
7This rule checks all variable declaration blocks and verifies that all variables are sorted alphabetically.
8The default configuration of the rule is case-sensitive.
9
10Examples of **incorrect** code for this rule:
11
12```js
13/*eslint sort-vars: "error"*/
14
15var b, a;
16
17var a, B, c;
18
19var a, A;
20```
21
22Examples of **correct** code for this rule:
23
24```js
25/*eslint sort-vars: "error"*/
26
27var a, b, c, d;
28
29var _a = 10;
30var _b = 20;
31
32var A, a;
33
34var B, a, c;
35```
36
37Alphabetical 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
42var c, d, a, b;
43```
44
45But this one, will only produce one:
46
47```js
48/*eslint sort-vars: "error"*/
49
50var c, d, a, e;
51```
52
53## Options
54
55This rule has an object option:
56
57* `"ignoreCase": true` (default `false`) ignores the case-sensitivity of the variables order
58
59### ignoreCase
60
61Examples of **correct** code for this rule with the `{ "ignoreCase": true }` option:
62
63```js
64/*eslint sort-vars: ["error", { "ignoreCase": true }]*/
65
66var a, A;
67
68var a, B, c;
69```
70
71## When Not To Use It
72
73This 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)