eslint-plugin-perfectionist 3.5.0 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +41 -12
- package/dist/rules/sort-array-includes.js +118 -60
- package/dist/rules/sort-array-includes.mjs +106 -58
- package/dist/rules/sort-astro-attributes.js +26 -45
- package/dist/rules/sort-astro-attributes.mjs +17 -32
- package/dist/rules/sort-classes.js +63 -40
- package/dist/rules/sort-classes.mjs +60 -40
- package/dist/rules/sort-enums.js +73 -41
- package/dist/rules/sort-enums.mjs +71 -40
- package/dist/rules/sort-exports.js +88 -12
- package/dist/rules/sort-exports.mjs +85 -12
- package/dist/rules/sort-imports.js +14 -10
- package/dist/rules/sort-imports.mjs +13 -9
- package/dist/rules/sort-interfaces.js +98 -107
- package/dist/rules/sort-interfaces.mjs +76 -90
- package/dist/rules/sort-intersection-types.js +153 -114
- package/dist/rules/sort-intersection-types.mjs +149 -108
- package/dist/rules/sort-jsx-props.js +26 -45
- package/dist/rules/sort-jsx-props.mjs +17 -32
- package/dist/rules/sort-maps.js +78 -22
- package/dist/rules/sort-maps.mjs +74 -22
- package/dist/rules/sort-named-exports.js +87 -31
- package/dist/rules/sort-named-exports.mjs +81 -28
- package/dist/rules/sort-named-imports.js +87 -31
- package/dist/rules/sort-named-imports.mjs +81 -28
- package/dist/rules/sort-object-types.js +88 -120
- package/dist/rules/sort-object-types.mjs +80 -105
- package/dist/rules/sort-objects.js +121 -89
- package/dist/rules/sort-objects.mjs +118 -85
- package/dist/rules/sort-svelte-attributes.js +26 -45
- package/dist/rules/sort-svelte-attributes.mjs +17 -32
- package/dist/rules/sort-union-types.js +153 -114
- package/dist/rules/sort-union-types.mjs +149 -108
- package/dist/rules/sort-variable-declarations.js +97 -24
- package/dist/rules/sort-variable-declarations.mjs +95 -25
- package/dist/rules/sort-vue-attributes.js +26 -46
- package/dist/rules/sort-vue-attributes.mjs +17 -33
- package/dist/utils/get-comments-before.js +12 -0
- package/dist/utils/get-comments-before.mjs +10 -0
- package/dist/utils/get-node-range.js +24 -12
- package/dist/utils/get-node-range.mjs +22 -12
- package/dist/utils/is-partition-comment.js +3 -0
- package/dist/utils/is-partition-comment.mjs +3 -1
- package/dist/utils/sort-nodes-by-dependencies.js +16 -0
- package/dist/utils/sort-nodes-by-dependencies.mjs +16 -1
- package/dist/utils/sort-nodes-by-groups.js +22 -0
- package/dist/utils/sort-nodes-by-groups.mjs +20 -0
- package/package.json +4 -4
- package/dist/utils/get-comment-before.js +0 -23
- package/dist/utils/get-comment-before.mjs +0 -21
package/dist/index.d.ts
CHANGED
|
@@ -108,10 +108,13 @@ declare const _default: {
|
|
|
108
108
|
}
|
|
109
109
|
rules: {
|
|
110
110
|
'sort-variable-declarations': RuleModule<
|
|
111
|
-
'
|
|
111
|
+
| 'unexpectedVariableDeclarationsDependencyOrder'
|
|
112
|
+
| 'unexpectedVariableDeclarationsOrder',
|
|
112
113
|
[
|
|
113
114
|
Partial<{
|
|
114
115
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
116
|
+
partitionByComment: string[] | boolean | string
|
|
117
|
+
partitionByNewLine: boolean
|
|
115
118
|
order: 'desc' | 'asc'
|
|
116
119
|
ignoreCase: boolean
|
|
117
120
|
}>,
|
|
@@ -120,10 +123,12 @@ declare const _default: {
|
|
|
120
123
|
RuleListener
|
|
121
124
|
>
|
|
122
125
|
'sort-intersection-types': RuleModule<
|
|
123
|
-
'
|
|
126
|
+
| 'unexpectedIntersectionTypesGroupOrder'
|
|
127
|
+
| 'unexpectedIntersectionTypesOrder',
|
|
124
128
|
[
|
|
125
129
|
Partial<{
|
|
126
130
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
131
|
+
partitionByComment: string[] | boolean | string
|
|
127
132
|
groups: (
|
|
128
133
|
| (
|
|
129
134
|
| 'object'
|
|
@@ -156,6 +161,7 @@ declare const _default: {
|
|
|
156
161
|
| 'union'
|
|
157
162
|
)
|
|
158
163
|
)[]
|
|
164
|
+
partitionByNewLine: boolean
|
|
159
165
|
order: 'desc' | 'asc'
|
|
160
166
|
ignoreCase: boolean
|
|
161
167
|
}>,
|
|
@@ -164,7 +170,8 @@ declare const _default: {
|
|
|
164
170
|
RuleListener
|
|
165
171
|
>
|
|
166
172
|
'sort-svelte-attributes': RuleModule<
|
|
167
|
-
'
|
|
173
|
+
| 'unexpectedSvelteAttributesGroupOrder'
|
|
174
|
+
| 'unexpectedSvelteAttributesOrder',
|
|
168
175
|
[
|
|
169
176
|
Partial<{
|
|
170
177
|
customGroups: {
|
|
@@ -180,7 +187,7 @@ declare const _default: {
|
|
|
180
187
|
RuleListener
|
|
181
188
|
>
|
|
182
189
|
'sort-astro-attributes': RuleModule<
|
|
183
|
-
'unexpectedAstroAttributesOrder',
|
|
190
|
+
'unexpectedAstroAttributesGroupOrder' | 'unexpectedAstroAttributesOrder',
|
|
184
191
|
[
|
|
185
192
|
Partial<{
|
|
186
193
|
customGroups: {
|
|
@@ -196,7 +203,7 @@ declare const _default: {
|
|
|
196
203
|
RuleListener
|
|
197
204
|
>
|
|
198
205
|
'sort-vue-attributes': RuleModule<
|
|
199
|
-
'unexpectedVueAttributesOrder',
|
|
206
|
+
'unexpectedVueAttributesGroupOrder' | 'unexpectedVueAttributesOrder',
|
|
200
207
|
[
|
|
201
208
|
Partial<{
|
|
202
209
|
customGroups: {
|
|
@@ -223,6 +230,8 @@ declare const _default: {
|
|
|
223
230
|
Partial<{
|
|
224
231
|
groupKind: 'values-first' | 'types-first' | 'mixed'
|
|
225
232
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
233
|
+
partitionByComment: string[] | boolean | string
|
|
234
|
+
partitionByNewLine: boolean
|
|
226
235
|
order: 'desc' | 'asc'
|
|
227
236
|
ignoreAlias: boolean
|
|
228
237
|
ignoreCase: boolean
|
|
@@ -237,6 +246,8 @@ declare const _default: {
|
|
|
237
246
|
Partial<{
|
|
238
247
|
groupKind: 'values-first' | 'types-first' | 'mixed'
|
|
239
248
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
249
|
+
partitionByComment: string[] | boolean | string
|
|
250
|
+
partitionByNewLine: boolean
|
|
240
251
|
order: 'desc' | 'asc'
|
|
241
252
|
ignoreCase: boolean
|
|
242
253
|
}>,
|
|
@@ -245,7 +256,7 @@ declare const _default: {
|
|
|
245
256
|
RuleListener
|
|
246
257
|
>
|
|
247
258
|
'sort-object-types': RuleModule<
|
|
248
|
-
'unexpectedObjectTypesOrder',
|
|
259
|
+
'unexpectedObjectTypesGroupOrder' | 'unexpectedObjectTypesOrder',
|
|
249
260
|
[
|
|
250
261
|
Partial<{
|
|
251
262
|
groupKind: 'required-first' | 'optional-first' | 'mixed'
|
|
@@ -253,6 +264,7 @@ declare const _default: {
|
|
|
253
264
|
[x: string]: string | string[]
|
|
254
265
|
}
|
|
255
266
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
267
|
+
partitionByComment: string[] | boolean | string
|
|
256
268
|
groups: (string | string[])[]
|
|
257
269
|
partitionByNewLine: boolean
|
|
258
270
|
order: 'desc' | 'asc'
|
|
@@ -263,10 +275,11 @@ declare const _default: {
|
|
|
263
275
|
RuleListener
|
|
264
276
|
>
|
|
265
277
|
'sort-union-types': RuleModule<
|
|
266
|
-
'unexpectedUnionTypesOrder',
|
|
278
|
+
'unexpectedUnionTypesGroupOrder' | 'unexpectedUnionTypesOrder',
|
|
267
279
|
[
|
|
268
280
|
Partial<{
|
|
269
281
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
282
|
+
partitionByComment: string[] | boolean | string
|
|
270
283
|
groups: (
|
|
271
284
|
| (
|
|
272
285
|
| 'object'
|
|
@@ -299,6 +312,7 @@ declare const _default: {
|
|
|
299
312
|
| 'union'
|
|
300
313
|
)
|
|
301
314
|
)[]
|
|
315
|
+
partitionByNewLine: boolean
|
|
302
316
|
order: 'desc' | 'asc'
|
|
303
317
|
ignoreCase: boolean
|
|
304
318
|
}>,
|
|
@@ -319,7 +333,8 @@ declare const _default: {
|
|
|
319
333
|
RuleListener
|
|
320
334
|
>
|
|
321
335
|
'sort-interfaces': RuleModule<
|
|
322
|
-
'
|
|
336
|
+
| 'unexpectedInterfacePropertiesGroupOrder'
|
|
337
|
+
| 'unexpectedInterfacePropertiesOrder',
|
|
323
338
|
[
|
|
324
339
|
Partial<{
|
|
325
340
|
groupKind: 'optional-first' | 'required-first' | 'mixed'
|
|
@@ -327,6 +342,7 @@ declare const _default: {
|
|
|
327
342
|
[key: string]: string[] | string
|
|
328
343
|
}
|
|
329
344
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
345
|
+
partitionByComment: string[] | boolean | string
|
|
330
346
|
groups: (string | string[])[]
|
|
331
347
|
partitionByNewLine: boolean
|
|
332
348
|
ignorePattern: string[]
|
|
@@ -338,7 +354,7 @@ declare const _default: {
|
|
|
338
354
|
RuleListener
|
|
339
355
|
>
|
|
340
356
|
'sort-jsx-props': RuleModule<
|
|
341
|
-
'unexpectedJSXPropsOrder',
|
|
357
|
+
'unexpectedJSXPropsGroupOrder' | 'unexpectedJSXPropsOrder',
|
|
342
358
|
[
|
|
343
359
|
Partial<{
|
|
344
360
|
customGroups: {
|
|
@@ -355,13 +371,16 @@ declare const _default: {
|
|
|
355
371
|
RuleListener
|
|
356
372
|
>
|
|
357
373
|
'sort-classes': RuleModule<
|
|
358
|
-
|
|
374
|
+
| 'unexpectedClassesDependencyOrder'
|
|
375
|
+
| 'unexpectedClassesGroupOrder'
|
|
376
|
+
| 'unexpectedClassesOrder',
|
|
359
377
|
SortClassesOptions,
|
|
360
378
|
unknown,
|
|
361
379
|
RuleListener
|
|
362
380
|
>
|
|
363
381
|
'sort-imports': RuleModule<
|
|
364
382
|
| 'missedSpacingBetweenImports'
|
|
383
|
+
| 'unexpectedImportsGroupOrder'
|
|
365
384
|
| 'extraSpacingBetweenImports'
|
|
366
385
|
| 'unexpectedImportsOrder',
|
|
367
386
|
[
|
|
@@ -396,7 +415,10 @@ declare const _default: {
|
|
|
396
415
|
'unexpectedExportsOrder',
|
|
397
416
|
[
|
|
398
417
|
Partial<{
|
|
418
|
+
groupKind: 'values-first' | 'types-first' | 'mixed'
|
|
399
419
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
420
|
+
partitionByComment: string[] | boolean | string
|
|
421
|
+
partitionByNewLine: boolean
|
|
400
422
|
order: 'desc' | 'asc'
|
|
401
423
|
ignoreCase: boolean
|
|
402
424
|
}>,
|
|
@@ -405,7 +427,9 @@ declare const _default: {
|
|
|
405
427
|
RuleListener
|
|
406
428
|
>
|
|
407
429
|
'sort-objects': RuleModule<
|
|
408
|
-
'
|
|
430
|
+
| 'unexpectedObjectsDependencyOrder'
|
|
431
|
+
| 'unexpectedObjectsGroupOrder'
|
|
432
|
+
| 'unexpectedObjectsOrder',
|
|
409
433
|
[
|
|
410
434
|
Partial<{
|
|
411
435
|
customGroups: {
|
|
@@ -426,7 +450,7 @@ declare const _default: {
|
|
|
426
450
|
RuleListener
|
|
427
451
|
>
|
|
428
452
|
'sort-enums': RuleModule<
|
|
429
|
-
'unexpectedEnumsOrder',
|
|
453
|
+
'unexpectedEnumsDependencyOrder' | 'unexpectedEnumsOrder',
|
|
430
454
|
Options_2,
|
|
431
455
|
unknown,
|
|
432
456
|
RuleListener
|
|
@@ -442,6 +466,8 @@ declare const _default: {
|
|
|
442
466
|
[
|
|
443
467
|
Partial<{
|
|
444
468
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
469
|
+
partitionByComment: string[] | boolean | string
|
|
470
|
+
partitionByNewLine: boolean
|
|
445
471
|
order: 'desc' | 'asc'
|
|
446
472
|
ignoreCase: boolean
|
|
447
473
|
}>,
|
|
@@ -507,6 +533,8 @@ declare type Options = [
|
|
|
507
533
|
Partial<{
|
|
508
534
|
groupKind: 'literals-first' | 'spreads-first' | 'mixed'
|
|
509
535
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
536
|
+
partitionByComment: string[] | boolean | string
|
|
537
|
+
partitionByNewLine: boolean
|
|
510
538
|
order: 'desc' | 'asc'
|
|
511
539
|
ignoreCase: boolean
|
|
512
540
|
}>,
|
|
@@ -516,6 +544,7 @@ declare type Options_2 = [
|
|
|
516
544
|
Partial<{
|
|
517
545
|
type: 'alphabetical' | 'line-length' | 'natural'
|
|
518
546
|
partitionByComment: string[] | boolean | string
|
|
547
|
+
partitionByNewLine: boolean
|
|
519
548
|
forceNumericSort: boolean
|
|
520
549
|
order: 'desc' | 'asc'
|
|
521
550
|
sortByValue: boolean
|
|
@@ -3,7 +3,10 @@ Object.defineProperties(exports, {
|
|
|
3
3
|
__esModule: { value: true },
|
|
4
4
|
[Symbol.toStringTag]: { value: 'Module' },
|
|
5
5
|
})
|
|
6
|
+
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
7
|
+
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
6
8
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
9
|
+
const getLinesBetween = require('../utils/get-lines-between.js')
|
|
7
10
|
const getGroupNumber = require('../utils/get-group-number.js')
|
|
8
11
|
const getSourceCode = require('../utils/get-source-code.js')
|
|
9
12
|
const toSingleLine = require('../utils/to-single-line.js')
|
|
@@ -38,6 +41,29 @@ let jsonSchema = {
|
|
|
38
41
|
enum: ['mixed', 'literals-first', 'spreads-first'],
|
|
39
42
|
type: 'string',
|
|
40
43
|
},
|
|
44
|
+
partitionByComment: {
|
|
45
|
+
description:
|
|
46
|
+
'Allows you to use comments to separate the array members into logical groups.',
|
|
47
|
+
anyOf: [
|
|
48
|
+
{
|
|
49
|
+
type: 'array',
|
|
50
|
+
items: {
|
|
51
|
+
type: 'string',
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
type: 'boolean',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
type: 'string',
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
partitionByNewLine: {
|
|
63
|
+
description:
|
|
64
|
+
'Allows to use spaces to separate the nodes into logical groups.',
|
|
65
|
+
type: 'boolean',
|
|
66
|
+
},
|
|
41
67
|
},
|
|
42
68
|
additionalProperties: false,
|
|
43
69
|
}
|
|
@@ -61,6 +87,8 @@ const sortArrayIncludes = createEslintRule.createEslintRule({
|
|
|
61
87
|
order: 'asc',
|
|
62
88
|
ignoreCase: true,
|
|
63
89
|
groupKind: 'literals-first',
|
|
90
|
+
partitionByComment: false,
|
|
91
|
+
partitionByNewLine: false,
|
|
64
92
|
},
|
|
65
93
|
],
|
|
66
94
|
create: context => ({
|
|
@@ -88,69 +116,99 @@ let sortArray = (context, messageId, elements) => {
|
|
|
88
116
|
type: 'alphabetical',
|
|
89
117
|
ignoreCase: true,
|
|
90
118
|
order: 'asc',
|
|
119
|
+
partitionByComment: false,
|
|
120
|
+
partitionByNewLine: false,
|
|
91
121
|
})
|
|
92
122
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
93
|
-
let
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
accumulator.at(0).push({
|
|
102
|
-
name:
|
|
103
|
-
element.type === 'Literal'
|
|
104
|
-
? `${element.value}`
|
|
105
|
-
: sourceCode.text.slice(...element.range),
|
|
106
|
-
size: rangeToDiff.rangeToDiff(element.range),
|
|
107
|
-
type: element.type,
|
|
108
|
-
node: element,
|
|
109
|
-
group,
|
|
110
|
-
})
|
|
123
|
+
let partitionComment = options.partitionByComment
|
|
124
|
+
let formattedMembers = elements.reduce(
|
|
125
|
+
(accumulator, element) => {
|
|
126
|
+
var _a
|
|
127
|
+
if (element !== null) {
|
|
128
|
+
let group = 'unknown'
|
|
129
|
+
if (typeof options.groupKind === 'string') {
|
|
130
|
+
group = element.type === 'SpreadElement' ? 'spread' : 'literal'
|
|
111
131
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
132
|
+
let lastSortingNode =
|
|
133
|
+
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
|
|
134
|
+
let sortingNode = {
|
|
135
|
+
name:
|
|
136
|
+
element.type === 'Literal'
|
|
137
|
+
? `${element.value}`
|
|
138
|
+
: sourceCode.text.slice(...element.range),
|
|
139
|
+
size: rangeToDiff.rangeToDiff(element.range),
|
|
140
|
+
node: element,
|
|
141
|
+
group,
|
|
142
|
+
}
|
|
143
|
+
if (
|
|
144
|
+
(partitionComment &&
|
|
145
|
+
isPartitionComment.hasPartitionComment(
|
|
146
|
+
partitionComment,
|
|
147
|
+
getCommentsBefore.getCommentsBefore(element, sourceCode),
|
|
148
|
+
)) ||
|
|
149
|
+
(options.partitionByNewLine &&
|
|
150
|
+
lastSortingNode &&
|
|
151
|
+
getLinesBetween.getLinesBetween(
|
|
152
|
+
sourceCode,
|
|
153
|
+
lastSortingNode,
|
|
154
|
+
sortingNode,
|
|
155
|
+
))
|
|
156
|
+
) {
|
|
157
|
+
accumulator.push([])
|
|
158
|
+
}
|
|
159
|
+
accumulator.at(-1).push(sortingNode)
|
|
160
|
+
}
|
|
161
|
+
return accumulator
|
|
162
|
+
},
|
|
163
|
+
[[]],
|
|
164
|
+
)
|
|
165
|
+
for (let nodes of formattedMembers) {
|
|
166
|
+
pairwise.pairwise(nodes, (left, right) => {
|
|
167
|
+
let groupKindOrder = ['unknown']
|
|
168
|
+
if (typeof options.groupKind === 'string') {
|
|
169
|
+
groupKindOrder =
|
|
170
|
+
options.groupKind === 'literals-first'
|
|
171
|
+
? ['literal', 'spread']
|
|
172
|
+
: ['spread', 'literal']
|
|
173
|
+
}
|
|
174
|
+
let leftNum = getGroupNumber.getGroupNumber(groupKindOrder, left)
|
|
175
|
+
let rightNum = getGroupNumber.getGroupNumber(groupKindOrder, right)
|
|
176
|
+
if (
|
|
177
|
+
(options.groupKind !== 'mixed' && leftNum > rightNum) ||
|
|
178
|
+
((options.groupKind === 'mixed' || leftNum === rightNum) &&
|
|
179
|
+
isPositive.isPositive(compare.compare(left, right, options)))
|
|
180
|
+
) {
|
|
181
|
+
context.report({
|
|
182
|
+
messageId,
|
|
183
|
+
data: {
|
|
184
|
+
left: toSingleLine.toSingleLine(left.name),
|
|
185
|
+
right: toSingleLine.toSingleLine(right.name),
|
|
186
|
+
},
|
|
187
|
+
node: right.node,
|
|
188
|
+
fix: fixer => {
|
|
189
|
+
let sortedNodes =
|
|
190
|
+
options.groupKind !== 'mixed'
|
|
191
|
+
? groupKindOrder
|
|
192
|
+
.map(group => nodes.filter(n => n.group === group))
|
|
193
|
+
.map(groupedNodes =>
|
|
194
|
+
sortNodes.sortNodes(groupedNodes, options),
|
|
195
|
+
)
|
|
196
|
+
.flat()
|
|
197
|
+
: sortNodes.sortNodes(nodes, options)
|
|
198
|
+
return makeFixes.makeFixes(
|
|
199
|
+
fixer,
|
|
200
|
+
nodes,
|
|
201
|
+
sortedNodes,
|
|
202
|
+
sourceCode,
|
|
203
|
+
{
|
|
204
|
+
partitionComment,
|
|
205
|
+
},
|
|
206
|
+
)
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
}
|
|
154
212
|
}
|
|
155
213
|
}
|
|
156
214
|
exports.default = sortArrayIncludes
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { hasPartitionComment } from '../utils/is-partition-comment.mjs'
|
|
2
|
+
import { getCommentsBefore } from '../utils/get-comments-before.mjs'
|
|
1
3
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
4
|
+
import { getLinesBetween } from '../utils/get-lines-between.mjs'
|
|
2
5
|
import { getGroupNumber } from '../utils/get-group-number.mjs'
|
|
3
6
|
import { getSourceCode } from '../utils/get-source-code.mjs'
|
|
4
7
|
import { toSingleLine } from '../utils/to-single-line.mjs'
|
|
@@ -33,6 +36,29 @@ let jsonSchema = {
|
|
|
33
36
|
enum: ['mixed', 'literals-first', 'spreads-first'],
|
|
34
37
|
type: 'string',
|
|
35
38
|
},
|
|
39
|
+
partitionByComment: {
|
|
40
|
+
description:
|
|
41
|
+
'Allows you to use comments to separate the array members into logical groups.',
|
|
42
|
+
anyOf: [
|
|
43
|
+
{
|
|
44
|
+
type: 'array',
|
|
45
|
+
items: {
|
|
46
|
+
type: 'string',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
type: 'boolean',
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
type: 'string',
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
partitionByNewLine: {
|
|
58
|
+
description:
|
|
59
|
+
'Allows to use spaces to separate the nodes into logical groups.',
|
|
60
|
+
type: 'boolean',
|
|
61
|
+
},
|
|
36
62
|
},
|
|
37
63
|
additionalProperties: false,
|
|
38
64
|
}
|
|
@@ -56,6 +82,8 @@ const sortArrayIncludes = createEslintRule({
|
|
|
56
82
|
order: 'asc',
|
|
57
83
|
ignoreCase: true,
|
|
58
84
|
groupKind: 'literals-first',
|
|
85
|
+
partitionByComment: false,
|
|
86
|
+
partitionByNewLine: false,
|
|
59
87
|
},
|
|
60
88
|
],
|
|
61
89
|
create: context => ({
|
|
@@ -83,67 +111,87 @@ let sortArray = (context, messageId, elements) => {
|
|
|
83
111
|
type: 'alphabetical',
|
|
84
112
|
ignoreCase: true,
|
|
85
113
|
order: 'asc',
|
|
114
|
+
partitionByComment: false,
|
|
115
|
+
partitionByNewLine: false,
|
|
86
116
|
})
|
|
87
117
|
let sourceCode = getSourceCode(context)
|
|
88
|
-
let
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
accumulator.at(0).push({
|
|
97
|
-
name:
|
|
98
|
-
element.type === 'Literal'
|
|
99
|
-
? `${element.value}`
|
|
100
|
-
: sourceCode.text.slice(...element.range),
|
|
101
|
-
size: rangeToDiff(element.range),
|
|
102
|
-
type: element.type,
|
|
103
|
-
node: element,
|
|
104
|
-
group,
|
|
105
|
-
})
|
|
118
|
+
let partitionComment = options.partitionByComment
|
|
119
|
+
let formattedMembers = elements.reduce(
|
|
120
|
+
(accumulator, element) => {
|
|
121
|
+
var _a
|
|
122
|
+
if (element !== null) {
|
|
123
|
+
let group = 'unknown'
|
|
124
|
+
if (typeof options.groupKind === 'string') {
|
|
125
|
+
group = element.type === 'SpreadElement' ? 'spread' : 'literal'
|
|
106
126
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
127
|
+
let lastSortingNode =
|
|
128
|
+
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
|
|
129
|
+
let sortingNode = {
|
|
130
|
+
name:
|
|
131
|
+
element.type === 'Literal'
|
|
132
|
+
? `${element.value}`
|
|
133
|
+
: sourceCode.text.slice(...element.range),
|
|
134
|
+
size: rangeToDiff(element.range),
|
|
135
|
+
node: element,
|
|
136
|
+
group,
|
|
137
|
+
}
|
|
138
|
+
if (
|
|
139
|
+
(partitionComment &&
|
|
140
|
+
hasPartitionComment(
|
|
141
|
+
partitionComment,
|
|
142
|
+
getCommentsBefore(element, sourceCode),
|
|
143
|
+
)) ||
|
|
144
|
+
(options.partitionByNewLine &&
|
|
145
|
+
lastSortingNode &&
|
|
146
|
+
getLinesBetween(sourceCode, lastSortingNode, sortingNode))
|
|
147
|
+
) {
|
|
148
|
+
accumulator.push([])
|
|
149
|
+
}
|
|
150
|
+
accumulator.at(-1).push(sortingNode)
|
|
151
|
+
}
|
|
152
|
+
return accumulator
|
|
153
|
+
},
|
|
154
|
+
[[]],
|
|
155
|
+
)
|
|
156
|
+
for (let nodes of formattedMembers) {
|
|
157
|
+
pairwise(nodes, (left, right) => {
|
|
158
|
+
let groupKindOrder = ['unknown']
|
|
159
|
+
if (typeof options.groupKind === 'string') {
|
|
160
|
+
groupKindOrder =
|
|
161
|
+
options.groupKind === 'literals-first'
|
|
162
|
+
? ['literal', 'spread']
|
|
163
|
+
: ['spread', 'literal']
|
|
164
|
+
}
|
|
165
|
+
let leftNum = getGroupNumber(groupKindOrder, left)
|
|
166
|
+
let rightNum = getGroupNumber(groupKindOrder, right)
|
|
167
|
+
if (
|
|
168
|
+
(options.groupKind !== 'mixed' && leftNum > rightNum) ||
|
|
169
|
+
((options.groupKind === 'mixed' || leftNum === rightNum) &&
|
|
170
|
+
isPositive(compare(left, right, options)))
|
|
171
|
+
) {
|
|
172
|
+
context.report({
|
|
173
|
+
messageId,
|
|
174
|
+
data: {
|
|
175
|
+
left: toSingleLine(left.name),
|
|
176
|
+
right: toSingleLine(right.name),
|
|
177
|
+
},
|
|
178
|
+
node: right.node,
|
|
179
|
+
fix: fixer => {
|
|
180
|
+
let sortedNodes =
|
|
181
|
+
options.groupKind !== 'mixed'
|
|
182
|
+
? groupKindOrder
|
|
183
|
+
.map(group => nodes.filter(n => n.group === group))
|
|
184
|
+
.map(groupedNodes => sortNodes(groupedNodes, options))
|
|
185
|
+
.flat()
|
|
186
|
+
: sortNodes(nodes, options)
|
|
187
|
+
return makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
188
|
+
partitionComment,
|
|
189
|
+
})
|
|
190
|
+
},
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
})
|
|
194
|
+
}
|
|
147
195
|
}
|
|
148
196
|
}
|
|
149
197
|
export { sortArrayIncludes as default, jsonSchema, sortArray }
|