+652
-1
lines changedFilter options
+652
-1
lines changed Original file line number Diff line number Diff line change
@@ -92,6 +92,7 @@ The 2nd option is an object which has 3 properties.
92
92
* `caseSensitive` - if `true`, enforce properties to be in case-sensitive order. Default is `true`.
93
93
* `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.
94
94
* `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.
95
+
* `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
96
97
Example for a list:
97
98
@@ -263,6 +264,128 @@ let obj = {
263
264
264
265
:::
265
266
267
+
### allowLineSeparatedGroups
268
+
269
+
Examples of **incorrect** code for the `{allowLineSeparatedGroups: true}` option:
270
+
271
+
::: incorrect
272
+
273
+
```js
274
+
/*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
275
+
/*eslint-env es6*/
276
+
277
+
let obj1 = {
278
+
b: 1,
279
+
c () {
280
+
281
+
},
282
+
a: 3
283
+
}
284
+
285
+
let obj2 = {
286
+
b: 1,
287
+
c: 2,
288
+
289
+
z () {
290
+
291
+
},
292
+
y: 3
293
+
}
294
+
295
+
let obj3 = {
296
+
b: 1,
297
+
c: 2,
298
+
299
+
z () {
300
+
301
+
},
302
+
// comment
303
+
y: 3,
304
+
}
305
+
306
+
let obj4 = {
307
+
b: 1
308
+
// comment before comma
309
+
, a: 2
310
+
};
311
+
```
312
+
313
+
:::
314
+
315
+
Examples of **correct** code for the `{allowLineSeparatedGroups: true}` option:
316
+
317
+
::: correct
318
+
319
+
```js
320
+
/*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
321
+
/*eslint-env es6*/
322
+
323
+
let obj = {
324
+
e: 1,
325
+
f: 2,
326
+
g: 3,
327
+
328
+
a: 4,
329
+
b: 5,
330
+
c: 6
331
+
}
332
+
333
+
let obj = {
334
+
b: 1,
335
+
336
+
// comment
337
+
a: 4,
338
+
c: 5,
339
+
}
340
+
341
+
let obj = {
342
+
c: 1,
343
+
d: 2,
344
+
345
+
b () {
346
+
347
+
},
348
+
e: 3,
349
+
}
350
+
351
+
let obj = {
352
+
c: 1,
353
+
d: 2,
354
+
// comment
355
+
356
+
// comment
357
+
b() {
358
+
359
+
},
360
+
e: 4
361
+
}
362
+
363
+
let obj = {
364
+
b,
365
+
366
+
[foo + bar]: 1,
367
+
a
368
+
}
369
+
370
+
let obj = {
371
+
b: 1
372
+
// comment before comma
373
+
374
+
,
375
+
a: 2
376
+
};
377
+
378
+
var obj = {
379
+
b: 1,
380
+
381
+
a: 2,
382
+
...z,
383
+
c: 3
384
+
}
385
+
```
386
+
387
+
:::
388
+
266
389
## When Not To Use It
267
390
268
391
If you don't want to notify about properties' order, then it's safe to disable this rule.
Original file line number Diff line number Diff line change
@@ -105,6 +105,10 @@ module.exports = {
105
105
type: "integer",
106
106
minimum: 2,
107
107
default: 2
108
+
},
109
+
allowLineSeparatedGroups: {
110
+
type: "boolean",
111
+
default: false
108
112
}
109
113
},
110
114
additionalProperties: false
@@ -124,17 +128,21 @@ module.exports = {
124
128
const insensitive = options && options.caseSensitive === false;
125
129
const natural = options && options.natural;
126
130
const minKeys = options && options.minKeys;
131
+
const allowLineSeparatedGroups = options && options.allowLineSeparatedGroups || false;
127
132
const isValidOrder = isValidOrders[
128
133
order + (insensitive ? "I" : "") + (natural ? "N" : "")
129
134
];
130
135
131
136
// The stack to save the previous property's name for each object literals.
132
137
let stack = null;
138
+
const sourceCode = context.getSourceCode();
133
139
134
140
return {
135
141
ObjectExpression(node) {
136
142
stack = {
137
143
upper: stack,
144
+
prevNode: null,
145
+
prevBlankLine: false,
138
146
prevName: null,
139
147
numKeys: node.properties.length
140
148
};
@@ -159,10 +167,45 @@ module.exports = {
159
167
const numKeys = stack.numKeys;
160
168
const thisName = getPropertyName(node);
161
169
170
+
// Get tokens between current node and previous node
171
+
const tokens = stack.prevNode && sourceCode
172
+
.getTokensBetween(stack.prevNode, node, { includeComments: true });
173
+
174
+
let isBlankLineBetweenNodes = stack.prevBlankLine;
175
+
176
+
if (tokens) {
177
+
178
+
// check blank line between tokens
179
+
tokens.forEach((token, index) => {
180
+
const previousToken = tokens[index - 1];
181
+
182
+
if (previousToken && (token.loc.start.line - previousToken.loc.end.line > 1)) {
183
+
isBlankLineBetweenNodes = true;
184
+
}
185
+
});
186
+
187
+
// check blank line between the current node and the last token
188
+
if (!isBlankLineBetweenNodes && (node.loc.start.line - tokens[tokens.length - 1].loc.end.line > 1)) {
189
+
isBlankLineBetweenNodes = true;
190
+
}
191
+
192
+
// check blank line between the first token and the previous node
193
+
if (!isBlankLineBetweenNodes && (tokens[0].loc.start.line - stack.prevNode.loc.end.line > 1)) {
194
+
isBlankLineBetweenNodes = true;
195
+
}
196
+
}
197
+
198
+
stack.prevNode = node;
199
+
162
200
if (thisName !== null) {
163
201
stack.prevName = thisName;
164
202
}
165
203
204
+
if (allowLineSeparatedGroups && isBlankLineBetweenNodes) {
205
+
stack.prevBlankLine = thisName === null;
206
+
return;
207
+
}
208
+
166
209
if (prevName === null || thisName === null || numKeys < minKeys) {
167
210
return;
168
211
}
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4