]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/sort-keys.md
4c90e6c9120be3cf88c535c164ef1aca382a2afb
[pve-eslint.git] / eslint / docs / src / rules / sort-keys.md
1 ---
2 title: sort-keys
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - sort-imports
7 - sort-vars
8 ---
9
10
11 When declaring multiple properties, some developers prefer to sort property names alphabetically to more easily find and/or diff necessary properties at a later time. Others feel that it adds complexity and becomes burden to maintain.
12
13 ## Rule Details
14
15 This rule checks all property definitions of object expressions and verifies that all variables are sorted alphabetically.
16
17 Examples of **incorrect** code for this rule:
18
19 ::: incorrect
20
21 ```js
22 /*eslint sort-keys: "error"*/
23 /*eslint-env es6*/
24
25 let obj = {a: 1, c: 3, b: 2};
26 let obj = {a: 1, "c": 3, b: 2};
27
28 // Case-sensitive by default.
29 let obj = {a: 1, b: 2, C: 3};
30
31 // Non-natural order by default.
32 let obj = {1: a, 2: c, 10: b};
33
34 // This rule checks computed properties which have a simple name as well.
35 // Simple names are names which are expressed by an Identifier node or a Literal node.
36 const S = Symbol("s")
37 let obj = {a: 1, ["c"]: 3, b: 2};
38 let obj = {a: 1, [S]: 3, b: 2};
39 ```
40
41 :::
42
43 Examples of **correct** code for this rule:
44
45 ::: correct
46
47 ```js
48 /*eslint sort-keys: "error"*/
49 /*eslint-env es6*/
50
51 let obj = {a: 1, b: 2, c: 3};
52 let obj = {a: 1, "b": 2, c: 3};
53
54 // Case-sensitive by default.
55 let obj = {C: 3, a: 1, b: 2};
56
57 // Non-natural order by default.
58 let obj = {1: a, 10: b, 2: c};
59
60 // This rule checks computed properties which have a simple name as well.
61 let obj = {a: 1, ["b"]: 2, c: 3};
62 let obj = {a: 1, [b]: 2, c: 3};
63
64 // This rule ignores computed properties which have a non-simple name.
65 let obj = {a: 1, [c + d]: 3, b: 2};
66 let obj = {a: 1, ["c" + "d"]: 3, b: 2};
67 let obj = {a: 1, [`${c}`]: 3, b: 2};
68 let obj = {a: 1, [tag`c`]: 3, b: 2};
69
70 // This rule does not report unsorted properties that are separated by a spread property.
71 let obj = {b: 1, ...c, a: 2};
72 ```
73
74 :::
75
76 ## Options
77
78 ```json
79 {
80 "sort-keys": ["error", "asc", {"caseSensitive": true, "natural": false, "minKeys": 2}]
81 }
82 ```
83
84 The 1st option is `"asc"` or `"desc"`.
85
86 * `"asc"` (default) - enforce properties to be in ascending order.
87 * `"desc"` - enforce properties to be in descending order.
88
89 The 2nd option is an object which has 3 properties.
90
91 * `caseSensitive` - if `true`, enforce properties to be in case-sensitive order. Default is `true`.
92 * `minKeys` - Specifies the minimum number of keys that an object should have in order for the object's unsorted keys to produce an error. Default is `2`, which means by default all objects with unsorted keys will result in lint errors.
93 * `natural` - if `true`, enforce properties to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.
94 * `allowLineSeparatedGroups` - if `true`, the rule allows to group object keys through line breaks. In other words, a blank line after a property will reset the sorting of keys. Default is `false`.
95
96 Example for a list:
97
98 With `natural` as true, the ordering would be
99 1
100 3
101 6
102 8
103 10
104
105 With `natural` as false, the ordering would be
106 1
107 10
108 3
109 6
110 8
111
112 ### desc
113
114 Examples of **incorrect** code for the `"desc"` option:
115
116 ::: incorrect
117
118 ```js
119 /*eslint sort-keys: ["error", "desc"]*/
120 /*eslint-env es6*/
121
122 let obj = {b: 2, c: 3, a: 1};
123 let obj = {"b": 2, c: 3, a: 1};
124
125 // Case-sensitive by default.
126 let obj = {C: 1, b: 3, a: 2};
127
128 // Non-natural order by default.
129 let obj = {10: b, 2: c, 1: a};
130 ```
131
132 :::
133
134 Examples of **correct** code for the `"desc"` option:
135
136 ::: correct
137
138 ```js
139 /*eslint sort-keys: ["error", "desc"]*/
140 /*eslint-env es6*/
141
142 let obj = {c: 3, b: 2, a: 1};
143 let obj = {c: 3, "b": 2, a: 1};
144
145 // Case-sensitive by default.
146 let obj = {b: 3, a: 2, C: 1};
147
148 // Non-natural order by default.
149 let obj = {2: c, 10: b, 1: a};
150 ```
151
152 :::
153
154 ### insensitive
155
156 Examples of **incorrect** code for the `{caseSensitive: false}` option:
157
158 ::: incorrect
159
160 ```js
161 /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
162 /*eslint-env es6*/
163
164 let obj = {a: 1, c: 3, C: 4, b: 2};
165 let obj = {a: 1, C: 3, c: 4, b: 2};
166 ```
167
168 :::
169
170 Examples of **correct** code for the `{caseSensitive: false}` option:
171
172 ::: correct
173
174 ```js
175 /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
176 /*eslint-env es6*/
177
178 let obj = {a: 1, b: 2, c: 3, C: 4};
179 let obj = {a: 1, b: 2, C: 3, c: 4};
180 ```
181
182 :::
183
184 ### natural
185
186 Examples of **incorrect** code for the `{natural: true}` option:
187
188 ::: incorrect
189
190 ```js
191 /*eslint sort-keys: ["error", "asc", {natural: true}]*/
192 /*eslint-env es6*/
193
194 let obj = {1: a, 10: c, 2: b};
195 ```
196
197 :::
198
199 Examples of **correct** code for the `{natural: true}` option:
200
201 ::: correct
202
203 ```js
204 /*eslint sort-keys: ["error", "asc", {natural: true}]*/
205 /*eslint-env es6*/
206
207 let obj = {1: a, 2: b, 10: c};
208 ```
209
210 :::
211
212 ### minKeys
213
214 Examples of **incorrect** code for the `{minKeys: 4}` option:
215
216 ::: incorrect
217
218 ```js
219 /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/
220 /*eslint-env es6*/
221
222 // 4 keys
223 let obj = {
224 b: 2,
225 a: 1, // not sorted correctly (should be 1st key)
226 c: 3,
227 d: 4,
228 };
229
230 // 5 keys
231 let obj = {
232 2: 'a',
233 1: 'b', // not sorted correctly (should be 1st key)
234 3: 'c',
235 4: 'd',
236 5: 'e',
237 };
238 ```
239
240 :::
241
242 Examples of **correct** code for the `{minKeys: 4}` option:
243
244 ::: correct
245
246 ```js
247 /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/
248 /*eslint-env es6*/
249
250 // 3 keys
251 let obj = {
252 b: 2,
253 a: 1,
254 c: 3,
255 };
256
257 // 2 keys
258 let obj = {
259 2: 'b',
260 1: 'a',
261 };
262 ```
263
264 :::
265
266 ### allowLineSeparatedGroups
267
268 Examples of **incorrect** code for the `{allowLineSeparatedGroups: true}` option:
269
270 ::: incorrect
271
272 ```js
273 /*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
274 /*eslint-env es6*/
275
276 let obj1 = {
277 b: 1,
278 c () {
279
280 },
281 a: 3
282 }
283
284 let obj2 = {
285 b: 1,
286 c: 2,
287
288 z () {
289
290 },
291 y: 3
292 }
293
294 let obj3 = {
295 b: 1,
296 c: 2,
297
298 z () {
299
300 },
301 // comment
302 y: 3,
303 }
304
305 let obj4 = {
306 b: 1
307 // comment before comma
308 , a: 2
309 };
310 ```
311
312 :::
313
314 Examples of **correct** code for the `{allowLineSeparatedGroups: true}` option:
315
316 ::: correct
317
318 ```js
319 /*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
320 /*eslint-env es6*/
321
322 let obj = {
323 e: 1,
324 f: 2,
325 g: 3,
326
327 a: 4,
328 b: 5,
329 c: 6
330 }
331
332 let obj = {
333 b: 1,
334
335 // comment
336 a: 4,
337 c: 5,
338 }
339
340 let obj = {
341 c: 1,
342 d: 2,
343
344 b () {
345
346 },
347 e: 3,
348 }
349
350 let obj = {
351 c: 1,
352 d: 2,
353 // comment
354
355 // comment
356 b() {
357
358 },
359 e: 4
360 }
361
362 let obj = {
363 b,
364
365 [foo + bar]: 1,
366 a
367 }
368
369 let obj = {
370 b: 1
371 // comment before comma
372
373 ,
374 a: 2
375 };
376
377 var obj = {
378 b: 1,
379
380 a: 2,
381 ...z,
382 c: 3
383 }
384 ```
385
386 :::
387
388 ## When Not To Use It
389
390 If you don't want to notify about properties' order, then it's safe to disable this rule.
391
392 ## Compatibility
393
394 * **JSCS:** [validateOrderInObjectKeys](https://jscs-dev.github.io/rule/validateOrderInObjectKeys)